#include
using namespace std;
typedef struct linknode
{
int data;
struct linknode *next;
struct node *top;
}LiStack;
void showmenu();
void InitStack(LiStack *&s);
void DestroyStack(LiStack *&s);
bool StackEmpty(LiStack *&s);
void Push(LiStack *&s);
void Pop(LiStack *&s);
bool GetTop(LiStack *s);
int main()
{int select;
LiStack *s;
char ok='y';
while(ok=='y')
{
showmenu();
cout<<"請選擇操作(1-7),選0退出系統"<
cin>>select;
switch(select)
{
case 1:InitStack(s);break;
case 2:DestroyStack(s);break;
case 3:StackEmpty(s);break;
case 4:Push(s);break;
case 5:Pop(s);break;
case 6:GetTop(s);break;
default:cout<<"輸入錯 ";
}
cout<<"是否繼續(y/n)";
cin>>ok;
}
return 0;
}
void showmenu()
{
cout<<"菜單"<
cout
cout
cout
cout
cout
cout
}
void InitStack(LiStack *&s)//----------------初始化棧
{s=(LiStack *)malloc(sizeof(LiStack));
s->top=NULL;
}
void DestroyStack(LiStack *&s)//-----------銷毀棧
{LiStack *p=s,*q=s->next;
while(q!=NULL)
{
p=q;
q=p->next;
free(p);
}}
bool StackEmpty(LiStack *s) //-----------------判斷棧空
{return(s->next==NULL);
}
void Push(LiStack *&s,int e) //-------------進棧
{LiStack *p;
p=(LiStack *)malloc(sizeof(LiStack));
e=p->data;
cout<<"棧內容為:"<
while(p=NULL)
{cin>>p->data;
p=p->next;}
cout<
void Pop(LiStack *&s)//------------出棧
{struct linknode *p;
if(s->top==NULL)
cout<<"此棧為空"<
else{
p=s->next;
cout<<"棧內容為:"<
while(p!=NULL)
{coutdata;
p=p->next;}
}
free(p);}
bool GetTop(LiStack *s) //-----------取棧頂
{int e;
if(s->next==NULL)
return false;
else {e=s->next->data;
return true;}
}
http://www.cnblogs.com/liangchao/p/4274967.html