#include
using namespace std;
typedef int Elemtype;
#define Maxsize 10
#define ERROR 0
#define OK 1
typedef struct Linknode
{
Elemtype data;
struct Linknode *next;
}Linknode,* LinkStack;
void InitStack(LinkStack& L)
{
L = (LinkStack)malloc(sizeof(Linknode));
L->next = NULL;
}
bool StackEmpty(LinkStack L)
{
if (L->next == NULL)
{
cout<<"栈为空"<<endl;
return ERROR;
}
else
{
cout << "栈不为空" << endl;
return OK;
}
}
bool Push(LinkStack &L, Elemtype e)
{
Linknode *s = (LinkStack)malloc(sizeof(Linknode));
s->data = e;
s->next = L->next;
L->next = s;
return OK;
}
bool Pop(LinkStack& L, Elemtype &e)
{
if (L->next == NULL)
{
cout << "栈为空" << endl;
return ERROR;
}
Linknode* p = L->next;
e = p->data;
L->next = p->next;
cout << "出栈元素值为: " << e << endl;
free(p);
return OK;
}
bool GetTop(LinkStack& L, Elemtype& e)
{
if (L->next == NULL)
{
cout << "栈为空" << endl;
return ERROR;
}
e = L->next->data;
cout << "栈顶元素值为: " << e << endl;
return OK;
}
void PrintStack(LinkStack L)
{
Linknode* p= L->next;
while (p != NULL)
{
cout << "|______" << p->data << "______|" << endl;
p = p->next;
}
}
int main(void)
{
LinkStack S = NULL;
InitStack(S);
StackEmpty(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
Push(S, 4);
StackEmpty(S);
PrintStack(S);
int e = 0, x = 0;
Pop(S, e);
PrintStack(S);
GetTop(S, x);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98