数组堆栈
#include<stdio.h>
struct SNode{
struct SNode *Data;
int top;
int MaxSize;
};
typedef struct SNode *Stack;
Stack CreateStack( int MaxSize )
{
Stack S=(struct SNode *)malloc(sizeof(struct SNode));
S->Data=(int*)malloc(MaxSize*sizeof(int);
S->Top=-1;
S->MaxSize=MaxSize;
return S;
}
bool Push( Stack S, int X)
{
if(S->top==MaxSize-1)
return false;
S->Data[++S->top]=X;
return true;
}
int Pop( Stack S)
{
if(S->top==-1)
return false;
return S->Data[S->top--];
}
int main()
{
return 0;
}
链栈
#include<stdio.h>
#include<stdlib.h>
struct SNode{
int Data;
struct SNode *next;
};
typedef struct SNode* Stack;
Stack CreateStack()
{
Stack S;
S=(struct SNode*)malloc(struct SNode);
S->next=NULL;
return S;
}
bool Push(Stack S,int e)
{
struct SNode *p=(struct SNode*)malloc(sizeof(struct SNode));
p->Data=e;
p->next=S->next;
S->next=p;
return true;
}
int Pop(Stack S)
{
if(S->next==NULL)
{
printf("空");
return false;
}
else
{
struct SNode *p=S->next;
int x;
x=S->Data;
S->next=p->next;
free(p);
return x;
}
}
int main()
{
return 0;
}```
```c
输入与输出
Operation GetOp()
{
char Push[]="Push";
char Pop[]="Pop";
char End[] = "End";
char s[100];
scanf("%s", s);
if (strcmp(Push, s) == 0)return push;
if (strcmp(Pop, s) == 0)return pop;
if (strcmp(End, s) == 0)return end;
}
void PrintStack( Stack S )
{
int cnt=0;
while (S->Top != -1){
if(cnt++) printf(" ");
printf("%d", S->Data[S->Top--]);
}
}