#include <stdio.h>
#include <stdlib.h>
typedef int DataType;// come
typedef struct SeqStack{
int MAXNUM;
int top;
DataType *data;
}SeqStack,*PSeqStack;
//1.创建空栈
PSeqStack createEmptyStack_seq(int m){
PSeqStack pastack = (PSeqStack)malloc(sizeof(SeqStack));
if(pastack == NULL){printf("顺序栈内存分配失败"); return NULL;};
pastack->data = (DataType *)malloc(sizeof(DataType)*m);
if(pastack->data == NULL){printf("空间内存分配失败");return NULL;}
pastack->top = -1;
pastack->MAXNUM = m;
return pastack;
}
//2.判断是否为空
int isEmptyStack_seq(PSeqStack pastack){
return(pastack->top == -1);
}
//3.进栈
int push_seq(PSeqStack pastack, DataType x){
if (pastack->top >= pastack->MAXNUM-1)
{
printf("栈已满!");
return 0;
}
pastack->data[pastack->top+1] = x; //from
pastack->top ++;
return 1;
}
//4.出栈
int pop_seq(PSeqStack pastack){
if (pastack->top == -1)
{
printf("空栈操作失败!");
return 0;
}
pastack->top--;
return 1;
}
//5.取栈顶元素但不删除,返回栈顶数据;
DataType top_seq(PSeqStack pastack){
if (pastack->top == -1)
{
printf("空栈操作失败!");
return -1;
}
return pastack->data[pastack->top]; //alonep
}