#include<iostream>
using namespace std;
typedef struct Linknode {
int data;
struct Linknode* next;
}*LiStack;
bool InitLiStack(LiStack& S) {
S = new Linknode;
S->next = NULL;
S->data = NULL;
return true;
}
bool push(LiStack& S,int x) {
if (S->data==NULL) {
S->data = x;
S->next = NULL;
return true;
}
else {
Linknode* p = new Linknode;
p->data = x;
p->next = S;
S = p;
return true;
}
return false;
}
bool pop(LiStack& S,int &x) {
if (S->data == NULL)
return false;
else {
x = S->data;
S = S->next;
return true;
}
return false;
}
void printStack(LiStack S) {
Linknode* p = S;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int gettop(LiStack S) {
if (S->data == NULL)
return 0;
else {
return S->data;
}
}
void destoryStack(LiStack& S) {
while (S != NULL) {
Linknode* p = S;
S = S->next;
delete p;
}
}
int main() {
LiStack s;
InitLiStack(s);
push(s, 10);
push(s, 11);
push(s, 12);
push(s, 13);
push(s, 14);
printStack(s);
int x;
pop(s,x);
cout << x << endl;
printStack(s);
x=gettop(s);
cout << x << endl;
printStack(s);
destoryStack(s);
return 0;
}