顺序栈的C++实现,代码有所参考
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MaxSize 20
struct SeqStack{
int Data[MaxSize]; // 存储元素的数组
int Top; // 栈顶指针下标
SeqStack(){
Top = 0;
for(int i=0;i<MaxSize;i++)
Data[i] = 0;
}
};
bool Push(SeqStack &L, int add){
if(L.Top == MaxSize-1){
cout<<"栈空间已满"<<endl;
return 0;
}
L.Data[L.Top] = add;
L.Top++;
return 1;
}
int Pop(SeqStack &L){
if(L.Top==0){
cout<<"空栈"<<endl;
return 0;
}
int temp = L.Data[L.Top];
L.Data[L.Top] = 0;
L.Top--;
return temp;
}
bool isEmpty(SeqStack L){
if(L.Top == 0)
return 1;
return 0;
}
bool isFull(SeqStack L){
if(L.Top == MaxSize-1)
return 1;
return 0;
}
void display(SeqStack L){
for(int i=0;i<L.Top;i++)
cout<<L.Data[i]<<" ";
cout<<endl;
}
int main(){
SeqStack seq;
for(int i=0;i<10;i++)
Push(seq,i);
Pop(seq);
display(seq);
return 0;
}