0%

链表实现栈

链表实现栈

以下为数据结构作业
如有错误,请指出,谢谢!

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#define OK 1;
#define ERROR 0;
typedef int Status;
typedef int SElemType;
typedef int ElemType;
using namespace std;
typedef struct StackNode
{
ElemType data;
struct StackNode *next;
}StackNode,*LinkStack;
Status InitStack(LinkStack &S)
{
S=NULL;
return OK;
}
Status Push(LinkStack &S,SElemType e)
{
LinkStack p=new StackNode;
p->data=e;
p->next=S;
S=p;
return OK;
}
Status Pop(LinkStack &S,SElemType &e)
{
if(S==NULL)return ERROR;
e=S->data;
LinkStack p=S;
S=S->next;
delete p;
return OK;
}
SElemType GetTop(LinkStack S)
{
if(S!=NULL)
return S->data;
}
int main()
{
LinkStack S;
InitStack(S);
int N,ch;
printf("1.入栈\n2.出栈\n3.取栈顶元素\n0.退出\n");
while(scanf("%d",&ch)!=EOF)
{
ElemType e;
if(ch==1)
{
printf("请输入入栈数目!\n");
scanf("%d",&N);
printf("请输入%d个数按顺序入栈!\n",N);
while(N--)
{
scanf("%d",&e);
Push(S,e);
}
printf("OK!\n");
}
else if(ch==2)
{
if(!Pop(S,e))printf("栈为空!\n");
else printf("出栈元素为%d\n",e);
}
else if(ch==3)printf("栈顶元素为%d!\n",GetTop(S));
else break;
}
return 0;
}
赏点呗!