-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0_6_4.cpp
73 lines (59 loc) · 884 Bytes
/
0_6_4.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
#include<iostream>
#define size 8
using namespace std;
struct inode {
int data;
struct inode * link;
};
typedef struct inode node_t;
void push(int);
int pop();
node_t * bot = NULL;
node_t * top = NULL;
int main() {
int in = 0;
while (1) {
cout << "input number:";
cin >> in;
if (in > 0) {
push(in);
}
else if (in == 0) {
int out = pop();
if (out != -1)
cout << "[" << out << "]" << endl;
}
else if (in < 0) {
cout << "program treminated..." << endl;
return 0;
}
}
}
void push(int x) {
node_t * n = new node_t;
n->data = x;
n->link = NULL;
if (bot == NULL)
{
bot = n;
top = n;
}
else {
n->link = top;
top = n;
}
}
int pop() {
if (top == NULL)
{
cout << "stack is empty" << endl;
return -1;
}
node_t *tem = top;
int t = top->data;
top = top->link;
if (top == NULL)
bot = NULL;
delete tem;
return t;
}