-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList
More file actions
109 lines (103 loc) · 2.19 KB
/
List
File metadata and controls
109 lines (103 loc) · 2.19 KB
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
#include <iostream>
#include <string>
using namespace std;
int count = 0;
struct node_t {
int value;
node_t * next;
};
void add(node_t * & head, int value) {
node_t * node = new node_t;
node->value = value;
node->next = nullptr;
if( head == nullptr) {
head = node;
}
else {
node_t * last = head;
while(last->next) {
last = last->next;
}
last->next = node;
}
count++;
}
void away(node_t * & list) {
node_t * first = list;
list = list->next;
delete first;
count--;
}
void reverse(node_t * list) {
node_t * begin = list;
node_t * last = begin;
for (int j = 0; j < count / 2 ; j++) {
for (int i = j; i < count - 1 - j; i++) {
last = last->next;
}
int saved = begin->value;
begin->value = last->value;
last->value = saved;
begin = begin->next;
last = begin;
}
}
void print(node_t * first, int count) {
node_t *print = first;
for (int i = 0; i < count; i++) {
cout << "+---+ ";
}
cout << endl;
for (int i = 0; i < count; i++) {
if (i != 0) {
cout << "--->";
}
cout << "| " << print->value << " |";
print = print->next;
}
cout << endl;
for (int i = 0; i < count; i++) {
cout << "+---+ ";
}
cout << endl;
}
void delet(node_t * head) {
while(head) {
delete head;
head = head->next;
}
}
int main()
{
int value;
char op;
node_t * head = nullptr;
while(cin >> op) {
switch (op) {
case '+' : {
cin >> value;
add(head, value);
print(head, count);
break;
};
case '-' : {
away(head);
print(head, count);
break;
};
case 'q' : {
exit(0);
};
case '=' : {
print(head, count);
break;
};
case '/' : {
reverse(head);
print(head, count);
break;
};
}
}
delet(head);
}