//将123456依次入栈,将45出栈,再将67进栈最后全部出栈依次输出
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define maxsize 100
using namespace std;
typedef struct node
{
int stack[maxsize];
int top;
}seqstack;
void init(seqstack *s)
{
s->top=0;
}
int empty(seqstack s)
{
if(s.top==0)
return 1;
else
return 0;
}
int get(seqstack s,int *e)
{
if(s.top<=0)
{
cout<<"栈已空!";
return 0;
}
else
{
*e=s.stack[s.top-1];
return 1;
}
}
int push(seqstack *s,int e)
{
if(s->top>=maxsize)
{
cout<<"栈已满!";
return 0;
}
else
{
s->stack[s->top]=e;
s->top++;
return 1;
}
}
int pop(seqstack *s,int *e)
{
if(s->top<=0)
{
cout<<"栈已空!";
return 0;
}
else
{
s->top--;
*e=s->stack[s->top];
return 1;
}
}
int length(seqstack s)
{
return s.top;
}
void clear(seqstack *s)
{
s->top==0;
}
int main()
{
seqstack s;
int i;
int a[]={1,2,3,4,5};
int e;
init(&s);
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
if(push(&s,a[i])==0)
{
cout<<"不能入栈";
return 0;
}
}
cout<<"出栈的元素为:";
if(pop(&s,&e)==1)
cout<<e;
if(pop(&s,&e)==1)
cout<<e;
cout<<endl;
cout<<"栈顶元素为:";
if(get(s,&e)==0)
{
cout<<"栈为空";
return 0;
}
else
{
cout<<e;
cout<<endl;
}
if(push(&s,6)==0)
{
cout<<"栈已满";
return 0;
}
if(push(&s,7)==0)
{
cout<<"栈已满";
return 0;
}
cout<<"栈中元素个数为:"<<length(s);
cout<<endl;
cout<<"出栈元素的序列:";
while(!empty(s))
{
pop(&s,&e);
cout<<e;
}
cout<<endl;
}