数组
#include <bits/stdc++.h>
using namespace std;class sss{
private:int a[1000];int curr = -1;
public:void push(int);void pop();int top();bool empyt();int size();
};int main()
{sss n;while(true){int a;cout<<"1.添加+\n2.删除-\n3.显示栈顶\n4.储存情况\n5.栈的大小\n6.退出"<<endl;cin>>a;if(a==1){int sh;cin>>sh;n.push(sh);}else if(a==2)n.pop();else if(a==3)cout<<n.top()<<endl;else if(a==4){if(n.empyt()==1)cout<<"栈为空(×o×)";else cout<<"栈非空(^-^)";}else if(a==5)cout<<n.size()<<endl;else if(a==6)break;system("pause");system("cls");}return 0;
}
void sss::push(int s)
{if(curr<1000){a[++curr]=s;}else cout<<"栈已满(×o×)\n";return;
}
void sss::pop()
{if(curr>-1){curr--;}else cout<<"栈为空(×o×)\n";return;
}
int sss::top()
{if(curr>-1) return a[curr];else cout<<"栈为空(×o×)\n";return -1;
}
bool sss::empyt()
{if(curr==-1) return true;else return false;
}
int sss::size()
{return curr+1;
}
链表
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;struct sss
{int vl;sss* next;sss* prev;sss(){}sss(int v){v=vl;next=NULL;prev=NULL;}
};
class ss
{
private:sss* root=NULL;sss* ptop=NULL;int n=0;
public:void push(int);void pop();int top();bool empyt();int size();
};int main()
{ss n;while(true){int t;cout<<"1.添加+\n2.删除-\n3.显示栈顶\n4.储存情况\n5.栈的大小\n6.退出"<<endl;cin>>t;if(t==1){int sh;cin>>sh;n.push(sh);}else if(t==2)n.pop();else if(t==3)cout<<n.top()<<endl;else if(t==4){if(n.empyt()==1)cout<<"栈为空(×o×)";else cout<<"栈非空(^-^)";}else if(t==5)cout<<n.size()<<endl;else if(t==6)break;system("pause");system("cls");}return 0;
}
void ss::push(int s)
{sss*curr=new sss(s);if(root==NULL){root=curr;ptop=curr;return;}ptop->next=curr;curr->prev=ptop;ptop=curr;n++;
}
void ss::pop()
{if(root!=ptop){sss*curr=ptop;ptop=ptop->prev;ptop->next=NULL;delete curr;n--;}else if(root==ptop&&root!=NULL){sss*curr=ptop;delete curr;n--;root=NULL;ptop=NULL;}else cout<<"栈为空(×o×)"<<endl;
}
int ss::top()
{return ptop->vl;
}
bool ss::empyt()
{if(root==NULL)return true;else return false;
}
int ss::size()
{return n;
}