顺序栈通过数组实现,遵循先进先出
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define Stack_Size 50
typedef int StackElementType;
typedef struct{
StackElementType elem[Stack_Size];
int top;
}SeqStack;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void InitStack(SeqStack *S){
S->top=-1;
}
int IsEmpty(SeqStack *S){
if(S->top==-1){
return TRUE;
}
else return FALSE;
}
int IsFull(SeqStack *S){
if(S->top==Stack_Size-1){
return TRUE;
}
else return FALSE;
}
int setStack(SeqStack *S){
int n,i;
if(S->top<Stack_Size-1){
for(i=0;i<5;i++){
S->top++;
S->elem[i]=i+1;
// printf("进栈元素为:",S->elem[i]);
}
}
printf("进栈元素为:");
for(i=S->top;i>=0;i--){
printf("%d",S->elem[i]);
}
printf("\n");
return 0;
}
//进栈
int Push(SeqStack *S,StackElementType x){
int i;
if(S->top<Stack_Size-1){
S->top++;
S->elem[S->top]=x;
}
printf("进栈后元素为:");
for(i=S->top;i>=0;i--){
printf("%d",S->elem[i]);
}
printf("\n");
return TRUE;
}
//出栈
int Pop(SeqStack *S,StackElementType *x){
int i;
IsEmpty(S);
*x=S->elem[S->top];
S->top--;
printf("出栈后元素为:");
for(i=S->top;i>=0;i--){
printf("%d",S->elem[i]);
}
printf("\n");
return 0;
}
void menu(){
printf("");
}
int main() {
StackElementType x,a;
SeqStack client;
client.top=-1;
setStack(&client);
printf("输入x值:");
scanf("%d",&x);
Push(&client,x);
Pop(&client,&a);
return 0;
}