实现代码
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
typedef struct linknode {
ElemType data;
struct linknode *next;
} LinkStNode;
void InitStack(LinkStNode *&s){
s = (LinkStNode *)malloc(sizeof(LinkStNode));
s -> next = NULL;
}
void DestroyStack(LinkStNode *&s){
LinkStNode *h=s , *p = s->next;
while(p!=NULL){
free(h);
h=p;
p=p->next;
}
free(h);
}
bool StackEmpty(LinkStNode *&s){
return (s->next==NULL);
}
void Push(LinkStNode *&s , ElemType e){
LinkStNode *p;
p = (LinkStNode *)malloc(sizeof(LinkStNode));
p->data=e;
p->next=s->next;
s->next=p;
}
bool Pop(LinkStNode *&s , ElemType &e){
LinkStNode *p;
if(s->next==NULL)
return false;
p=s->next;
e=p->data;
s->next=p->next;
free(p);
}
bool GetTop(LinkStNode *&s , ElemType &e){
if(s->next == NULL)
return false;
e=s->next->data;
return true;
}
int main(){
LinkStNode *s;
InitStack(*&s);
Push(*&s , 1);
Push(*&s , 2);
Push(*&s , 3);
Push(*&s , 4);
Push(*&s , 8);
ElemType a;
GetTop(*&s , a);
cout << "stack TOP :" << a << endl;
ElemType e1,e2,e3,e4,e5;
Pop(*&s , e1);
Pop(*&s , e2);
Pop(*&s , e3);
Pop(*&s , e4);
Pop(*&s , e5);
cout << e1 << e2 << e3 << e4 <<e5 <<endl;
}