2 回答

TA貢獻1829條經驗 獲得超4個贊
//LStack.h
//堆棧的鏈式存儲結構
#include<malloc.h>
#include<process.h>
//定義結點結構體
typedef struct StackNode
{
ElemType data; //數據域
struct StackNode *next; //指針域
}StackNode;
//定義棧頂指針
typedef struct
{
StackNode *top; //棧頂指示器
}LinkStack;
//初始化
void InitStack(LinkStack &S)
{
if(NULL==(S.top=(StackNode *)malloc(sizeof(StackNode)))) //申請棧頂結點空間
exit(0); //申請失敗推出程序
S.top->next=NULL; //申請成功棧頂結點指針域初始化為空
S.top->data=NULL; //數據域初始化為NULL
}
//判斷棧是否為空
int isEmpty(LinkStack &S)
{
if(NULL==S.top->next&&NULL==S.top->data) //棧頂沒有后繼結點表明為空棧
return 0; //返回0
return 1; //非空時返回1
}
//入棧
int Push(const ElemType &item,LinkStack &S) //此處最好把item定義為抽象數據類型ElemType
{
StackNode *snp;
if(NULL==(snp=(StackNode *)malloc(sizeof(StackNode)))) //申請新結點空間
return 0;
snp->data=item;
snp->next=S.top;
S.top=snp;
return 1;
}
//出棧
int Pop(LinkStack &S)
{
ElemType reData;
StackNode *indexp;
if(!isEmpty(S)) //??胀顺?br/>return 0;
reData=S.top->data; //取出數據域存儲的數據
indexp=S.top; //保存當前棧頂結點
S.top=S.top->next; //棧頂變為top的后繼結點
free(indexp); //釋放原棧頂結點空間
return reData; //返回棧頂數據
}
//取棧頂數據
int GetTop(LinkStack &S)
{
ElemType reData;
if(!isEmpty(S)) //??胀顺?br/>return 0;
reData=S.top->data; //取出數據
return reData;
}
//清空棧
void MakeEmpty(LinkStack &S)
{
StackNode *p,*pp;
p=S.top;
while(p!=NULL)
{
pp=p;
p=p->next;
free(pp);
}
}
LStack.cpp
#include<stdio.h>
typedef int ElemType;
#include "LStack.h"
int main() //測試堆棧的鏈式存儲結構
{
LinkStack T;
int a[10]={1,3,5,7,9,11,13,15,17,19};
InitStack(T); //初始化棧
for(int i=0;i<10;i++)
Push(a[i],T); // 入棧
printf("棧頂數據=%d\n",GetTop(T)); //取棧頂數據
printf("出棧:");
while(isEmpty(T)) //棧不空時繼續出棧
{
printf("%-4d",Pop(T)); //出棧
}
MakeEmpty(T); //清空棧
}

TA貢獻2021條經驗 獲得超8個贊
/// 按照你的要求寫出來了,并且改了兩個你的錯誤,,
/// 給分吧,,呵呵,祝你學習愉快
// 剛剛把2個C++的內存分配改成c的了
#include <stdio.h>
#include <malloc.h>
#define ElemType int
typedef struct StackNode
{
ElemType data; //存放數據
struct StackNode * next; //指向下一個結點
}StackNode;
typedef struct LinkStack
{
struct StackNode * top; // 棧頂指針
}LinkStack;
void InitStack(LinkStack &S); // 棧的初始化
void Push(const int &item,LinkStack &S); // 入棧
int Pop(LinkStack &S); // 出棧
int GetTop(LinkStack &S); // 取棧頂元素
int IsEmpty(LinkStack &S); // 判斷棧是否為空
void MakeEmpty(LinkStack &S); // 清空棧
// 打印堆棧內容
void PrintStack(LinkStack &S)
{
StackNode *pSN = S.top;
while (pSN->next != 0)
{
printf("%d " , pSN->data);
pSN = pSN->next;
}
printf("\n");
}
void main()
{
LinkStack Stack; // 棧頂
int iItem = 0 ,i = 0;
/// 初始化
InitStack(Stack);
// 壓入內容
for (i = 0 ; i < 10 ; i ++)
{
Push(i , Stack);
}
printf("棧內內容為:");
PrintStack(Stack);
// 彈出
iItem = Pop(Stack);
printf("\n彈出%d:" , iItem);
printf("\n棧內內容為:");
PrintStack(Stack);
// 彈出
iItem = Pop(Stack);
printf("\n彈出%d:" , iItem);
printf("\n棧內內容為:");
PrintStack(Stack);
//清空棧
MakeEmpty(Stack);
}
// 棧的初始化
void InitStack(LinkStack &S)
{
S.top = (struct StackNode*)malloc(sizeof(StackNode));
S.top->data = 0;
S.top->next = 0;
}
// 入棧
void Push(const int &item , LinkStack &S)
{
StackNode *pSN = (struct StackNode*)malloc(sizeof(StackNode));
pSN->data = item;
pSN->next = S.top;
S.top = pSN;
}
// 出棧
int Pop(LinkStack &S)
{
if (IsEmpty(S))
{
return 0;
}
StackNode *pSN = S.top;
int iRet = pSN->data;
S.top = pSN->next;
delete pSN;
return iRet;
}
// 取棧頂元素
int GetTop(LinkStack &S)
{
if (IsEmpty(S))
{
return 0;
}
return S.top->data;
}
// 判斷棧是否為空
int IsEmpty(LinkStack &S)
{
StackNode *pSN = S.top->next;
if (0 == pSN->next)
{
return 1;
}
return 0;
}
// 清空棧
void MakeEmpty(LinkStack &S)
{
while (!IsEmpty(S))
{
Pop(S);
}
if (S.top != 0)
{
delete S.top;
}
S.top = 0;
}
添加回答
舉報