提供了初始化栈,判断栈空,入栈,出栈,读取栈顶元素。具体如下
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 100
#define ERROR -1
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Element;
typedef struct SeqStack{
Element data[MAXSIZE];
int top;
}SeqStack;
int InitStack(SeqStack *stack){
if(stack==NULL){
return ERROR;
}
stack->top=-1;
return OK;
}
int isEmpty(SeqStack *stack){
if(stack->top==-1)
return TRUE;
return FALSE;
}
int Push(SeqStack *stack,Element e){
if (NULL == stack || MAXSIZE == stack->top + 1) {
return ERROR;
}
stack->data[++(stack->top)]=e;
return 1;
}
void Pop(SeqStack *stack){
stack->data[(stack->top)--]=0;
}
int TopData(SeqStack *stack){
if(stack==NULL||stack->top==-1){
return ERROR;
}
return stack->data[stack->top];
}
int main()
{
Element e1,e2;
SeqStack stack;
scanf("%d",&e1);
InitStack(&stack);
printf("是否为空栈:%d\n",isEmpty(&stack));
Push(&stack,e1);
TopData(&stack);
printf("栈顶为:%d\n",TopData(&stack));
scanf("%d",&e2);
Push(&stack,e2);
printf("栈顶为:%d\n",TopData(&stack));
Pop(&stack);
printf("栈顶为:%d\n",TopData(&stack));
return 0;
}