#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
typedef int ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
}LiStack;
void InitStack(LiStack *&s)
{
s=(LiStack *)malloc(sizeof(LiStack));
s->next=NULL;
}
void DestroyStack(LiStack *&s)
{
LiStack *p,*r;
p=s;
r=s->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
free(p);
}
bool StackEmpty(LiStack *s)
{
return (s->next==NULL);
}
void Push(LiStack *&s,ElemType e)
{
LiStack *p=(LiStack *)malloc(sizeof(LiStack));
p->data=e;
p->next=s->next;
s->next=p;
}
bool Pop(LiStack *s,ElemType &e)
{
LiStack *p=s->next;
if(s->next==NULL)
return false;
e=s->next->data;
s->next=s->next->next;
free(p);
return true;
}
bool GetTop(LiStack *s,ElemType &e)
{
if(s->next==NULL)
return false;
e=s->next->data;
return true;
}
void main()
{
LiStack *s;
int i,e;
InitStack(s);
Push(s,1);
Push(s,2);
Push(s,3);
Push(s,4);
Push(s,5);
GetTop(s,e);
cout<<"栈顶元素为"<<e<<endl;
while(!StackEmpty(s))
{
Pop(s,i);
cout<<i<<endl;
}
}
数据结构教程第四版(李春葆)(P68~70)//栈的链式存储结构
最新推荐文章于 2022-02-14 22:28:56 发布