-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkStack.cpp
120 lines (94 loc) · 2.24 KB
/
LinkStack.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "iostream"
using namespace std;
#include "stdlib.h"
#define null 0
#define Null 0
typedef struct LinkStack{
int data;
struct LinkStack *next;
}Stack;
//=================create a linkstack=======================
void createStack(Stack *&s)
{
s=(Stack *)malloc(sizeof(Stack)); //the first node
s->next=null;
}
//================push a element to the stack=============
int Push(Stack *&s,int val)
{
Stack *new_node;
new_node=(Stack *)malloc(sizeof(Stack));
new_node->data=val;
new_node->next=s->next; //create a new node
s->next=new_node;
// s=new_node; we did not do this ,always make 's' ponit to the head node.....
//because first in l ast out....always insert new node after head node
}
//notice: in LinkList,insert after head node is similar with this!!!!!!!!
//=============Pop a val========================
int Pop(Stack *&s)
{
Stack *p;
int temp;
if(s->next==null)
{
cout<<"Empty Stack!"<<endl;
}
else
{
temp=s->next->data;
cout<<"Pop: "<<temp<<endl;
s->next=s->next->next; //head node point to the third node
free(p);
return temp;
}
}
//=====================display all============================
int displayStack(Stack *&s)
{
Stack *p=s->next; //use 'p' to Traversal the Stack
int len=0;
if(s->next==null)
{
cout<<"Empty stack!"<<endl;
return 0;
}
else{
while(p!=null)
{
int temp=p->data;
cout<<temp<<" ";
p=p->next;
len++; // when a node traversaled finished, len+1
}
cout<<endl<<"Stack Length is: "<<len<<endl;
return len;
}
}
//===========================destroy Stack============================
void destroyStack(Stack *s)
{
Stack *p=s->next;
while(p!=null)
{
free(s);
s=p;
p=p->next;
}
free(s);
cout<<"Stack Destroy finished."<<endl;
}
//============================
int main()
{
Stack *s; //this pointer , we use it as the top pointer
createStack(s);
for(int i=0;i<20;i++)
{
Push(s,i+1);
}
displayStack(s);
// destroyStack(s);
// displayStack(s);
return 0;
}