-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLAST2.cpp.txt
More file actions
128 lines (128 loc) · 2.51 KB
/
Copy pathLAST2.cpp.txt
File metadata and controls
128 lines (128 loc) · 2.51 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <string>
using namespace std;
struct Node {
int value;
Node* next, *prev;
};
struct deque {
Node* head = nullptr;
Node* tail = nullptr;
int counter = 0;
void push_back(int nummer) {
Node* element = new Node;
element->value = nummer;
counter++;
if (!head) {
head = element;
tail = head;
}
else {
element->prev = tail;
tail->next = element;
tail = element;
}
}
void push_front(int nummer) {
Node* element = new Node;
element->value = nummer;
counter++;
if (!head) {
head = element;
tail = head;
}
else {
element->next = head;
head->prev = element;
head = element;
}
}
void pop_back() {
if (counter != 0) {
cout << tail->value << endl;
if (counter > 1) {
Node* element = tail;
tail = tail->prev;
tail->next = nullptr;
delete element;
counter--;
}
else {
head = tail = 0;
counter--;
}
}
else cout << "error" << endl;
}
void pop_front() {
if (counter != 0) {
cout << head->value << endl;
if (head->next) {
Node* element = head;
head = head->next;
head->prev = nullptr;
delete element;
counter--;
}
else if (head == tail) {
head->next = nullptr;
head = nullptr;
delete head;
counter = 0;
}
}
else cout << "error" << endl;
}
void back() {
if (counter != 0)cout << tail->value << endl;
else cout << "error" << endl;
}
void front() {
if (counter != 0)cout << head->value << endl;
else cout << "error" << endl;
}
void size() {
cout << counter << endl;
}
void clear() {
counter = 0;
while (head)
{
tail = head->next;
delete head;
head = tail;
}
}
void exit(){}
};
int main()
{
deque deq;
string str;
while (cin >> str)
{
if (str == "push_front")
{
int num;
cin >> num;
deq.push_front(num);
}
if (str == "push_back")
{
int num;
cin >> num;
deq.push_back(num);
}
if (str == "pop_front")deq.pop_front();
if (str == "pop_back")deq.pop_back();
if (str == "clear")deq.clear();
if (str == "size")deq.size();
if (str == "front")deq.front();
if (str == "back")deq.back();
if (str == "exit") {
deq.exit();
break;
}
}
return 0;
}