#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 255
using namespace std;
/**
*栈也是一种线性表,不过只能对栈顶进行插入和删除操作
*本文用顺序表实现栈,即顺序栈
*/
typedef int ElemType;
typedef struct SeqStack{
ElemType elements[MAX_SIZE];//顺序栈中用来存放数据元素的数组
int top; //栈顶(数组中的下标),如果为-1,则证明栈为空
int length; //当前栈的元素个数
}SeqStack;
void InitSeqStack(SeqStack *seqStack)
{
seqStack->top = -1;
seqStack->length = 0;
}
void Push(SeqStack *seqStack,ElemType element)//向栈中压入元素
{
if(seqStack->top == MAX_SIZE-1)//栈满
{
cout << "满栈,入栈失败!"<<endl;
// return 0;
}
seqStack->top++; //栈顶向上移
seqStack->elements[seqStack->top] = element;
seqStack->length++;
cout << element << "入栈成功!" << endl;
//return 1;
}
ElemType Pop(SeqStack *seqStack)
{
if(seqStack->top == -1)
{
cout << "出栈失败,栈为空!" << endl;
return 0;
}
else
{
ElemType temp = seqStack->elements[seqStack->top];
seqStack->top--;
seqStack->length--;
return temp;
}
}
void ClearStack(SeqStack * seqStack)
{
seqStack->top = -1;
seqStack->length = 0;
}
int main()
{
ElemType datas[] = {4,2,3,8,10};
SeqStack * seqStack = (SeqStack*)malloc(sizeof(SeqStack));
InitSeqStack(seqStack);
for(int i = 0;i < 5;i++)
{
Push(seqStack,datas[i]);
}
cout << "\n";
for(int i = 0;i < 5;i++)
cout << Pop(seqStack) << "出栈成功"<<endl;
return 0;
}