-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_2_1.cpp
81 lines (70 loc) · 1.04 KB
/
1_2_1.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
#include<iostream>
#define size 8
struct inode {
int data;
struct inode * link;
};
typedef struct inode node_t;
void inq(int);
int deq();
void print_list(node_t * head);
using namespace std;
node_t * head = NULL;
node_t * tail = NULL;
int main() {
int in = 0;
while (1) {
cout << "input number:";
cin >> in;
if (in > 0) {
inq(in);
}
else if (in == 0) {
int out = deq();
if (out != -1)
cout << "[" << out << "]" << endl;
}
else if (in < 0) {
cout << "program treminated..." << endl;
print_list(head);
return 0;
}
}
}
void inq(int x) {
node_t * n = new node_t;
n->data = x;
n->link = NULL;
if (head == NULL)
{
head = n;
tail = n;
}
else {
tail->link = n;
tail = n;
}
}
int deq() {
if (head == NULL)
{
cout << "queue is empty" << endl;
return -1;
}
node_t *tem = head;
int t = head->data;
head = head->link;
if (head == NULL)
tail = NULL;
delete tem;
return t;
}
void print_list(node_t * h) {
if (h != NULL)
{
cout << h->data << " ";
print_list(h->link);
}
else
cout << endl;
}