在这里和初学者分享下,不要觉得顺序存储比链式存储low,很多时候数值大小固定的时候,用顺序存储去解决更快,在刷题也是
#include<stdio.h>
#include<stdlib.h>
#define false -1
#define ture 1
struct SNode
{
int*Data;
int Top;
int Maxsize;
};
typedef struct SNode *Stack;
typedef Stack SS;
Stack CreateStack(int MaxSize);//生成空堆栈,其最大长度为Maxsizeb
int IsFull(Stack S);//判断堆栈S是否已满
int Push(Stack S,int X);//将元素item压入堆栈
int IsEmpty(Stack S);//判断堆栈S是否为空
int Pop(Stack S);//删除并返回栈顶元素
Stack CreateStack(int Maxsize)
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (int*)malloc(Maxsize * sizeof(int));
S->Top = -1;
S->Maxsize = Maxsize;
return S;
}
int IsFull(Stack S) {
return(S->Top == S->Maxsize-1);
}
/*入栈*/
int Push(Stack S, int X)
{
if (IsFull(S))
{
printf("栈满\n");
return false;
}
else
{
S->Data[++(S->Top)]=X;
return ture;
}
}
int IsEmpty(Stack S) {
return(S->Top == -1);
}
/*出栈*/
int Pop(Stack S)
{
if (IsEmpty(S)) {
printf("栈空\n");
return false;
}
else {
S->Data[--(S->Top)];
}
}
/*出栈打印*/
int Printstack(Stack S)
{
while (S->Top!=-1)
{
int m= S->Data[S->Top];
S->Top--;
printf("%d\n", m);
}
printf("输出完毕");
}
int main()
{
int m;
Stack S2 = CreateStack(5);
Push(S2, 10);
Push(S2, 9);
Push(S2, 8);
Push(S2, 7);
Printstack(S2);
return 0;
}