#include<iostream>
#include"malloc.h"
#include<string>
#define MAX_LEN 100//初始最大容量
#define STACKINCREMENT 10//每次增加的容量
constexpr auto MAXSIZE = 50;
using namespace std;
typedef struct Stack
{
int data[MAXSIZE];
int top;
}SeqStack, * PSeqStack;
//初始化
PSeqStack Init_SeqStack(void)
{
PSeqStack S = (PSeqStack)malloc(sizeof(SeqStack));
if (S)
{
S->top = -1;
}
return S;
}
//判空
int empty(PSeqStack s)
{
if (s->top == -1)
{
return 1;
}
return 0;
}
//入栈
int Push(PSeqStack S, int x)
{
if (S->top == MAXSIZE - 1)
{
return 0;
}
else
{
S->top++;
S->data[S->top] = x;
return 1;
}
}
//出栈
int Pop_SeqStack(PSeqStack S, int* e)
{
if (empty(S))
{
return 0;
}
else
{
*e = S->data[S->top];
S->top--;
return 1;
}
}
//取栈顶元素
int GetTop_SeqStack(PSeqStack S, int* m)
{
if (empty(S))
{
return 0;
}
else
{
*m = S->data[S->top];
return 1;
}
}
//销毁栈
void Destroy_SeqStack(PSeqStack* S)
{
if (*S)
{
free(*S);
}
*S = NULL;
return;
}
int main()
{
PSeqStack S;
S = Init_SeqStack();
int n;
int a[MAXSIZE] = { 0 };
int e;
cout << "请输入入栈的元素个数n" << endl;
cin >> n;
cout << "请输入入栈的元素" << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
Push(S, a[i]);
}
for (int i = 0; i < 5; i++)
{
Pop_SeqStack(S, &e);
cout << "出栈元素为:" << e << endl;
}
}
顺序栈的相关操作
于 2022-03-26 01:55:46 首次发布