#include
using namespace std;
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct stack
{
Node *bottom;
Node *top;
}MyStack;
MyStack *CreatMyStack()//creat a empty stack
{
MyStack *q=new MyStack;
q->bottom=NULL;
q->top=NULL;
return q;
}
MyStack *PushMyStack(MyStack *q,int data)
{
Node *temp=NULL;
temp=new Node;
temp->data=data;
temp->next=NULL;
if(q->bottom==NULL)//if is a empty stack
q->bottom=q->top=temp;
else
{
q->top->next=temp;
q->top=temp;
}
return q;
}
MyStack *PopMyStack(MyStack *q)
{
if(q->bottom==NULL)//empty stack
cout<<"empty stack";
else
{
if(q->bottom==q->top)//only one Node
{
q->bottom=NULL;
q->top=NULL;
}
else
{
Node *temp=NULL;
temp=q->bottom;
while((temp->next)!=(q->top))temp=temp->next;
q->top=temp;
q->top->next=NULL;
}
}
return q;
}
int PrintMyStack(MyStack *q)
{
Node *temp=q->bottom;
if(temp==NULL)
{
cout<<"empty stack"< return 1;
}
cout<<"data are:"<
while(temp!=q->top)
{
cout<data< temp=temp->next;
}
cout<data< return 0;
}
int main(void)
{
MyStack *tp=CreatMyStack();
PushMyStack(tp,1);
PushMyStack(tp,2);
PushMyStack(tp,3);
PrintMyStack(tp);
PopMyStack(tp);
PopMyStack(tp);
PrintMyStack(tp);
return 0;
}
阅读(979) | 评论(1) | 转发(1) |