-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path740.cpp
More file actions
145 lines (141 loc) · 2.34 KB
/
Copy path740.cpp
File metadata and controls
145 lines (141 loc) · 2.34 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<iostream>
using namespace std;
class Node {
private:
Node* previous;
Node* next;
int data;
public:
Node(int t) {
data = t;
}
void setNext(Node* t) {
next = t;
}
void setPrevious(Node* t) {
previous = t;
}
Node* getNext() {
return next;
}
Node* getPrevious() {
return previous;
}
int getData() {
return data;
}
};
class List {
private:
Node* first;
int count = 0;
public:
List() {
first = new Node(233);
first->setNext(new Node(233));
first->setPrevious(new Node(233));
}
void insert(Node* t, int pos) {
Node* p = first;
for (int i = 0; i < pos; i++) {
p = p->getNext();
}
t->setPrevious(p);
t->setNext(p->getNext());
t->getNext()->setPrevious(t);
p->setNext(t);
}
void remove(int pos) {
Node* p = first;
for (int i = 0; i < pos; i++) {
p = p->getNext();
}
p->getPrevious()->setNext(p->getNext());
p->getPrevious()->getNext()->setPrevious(p->getPrevious());
}
void reverse(int s, int t) {
Node* p = first->getNext();
Node* tnode;
Node* start = first;
for (int i = 1; i < t; i++) {
if (i == s - 1) {
start = p;
}
if (i >= s) {
tnode = p->getPrevious();
p->setPrevious(p->getNext());
p->setNext(tnode);
p = p->getPrevious();
}
else {
p = p->getNext();
}
}
p->getNext()->setPrevious(start->getNext());
start->getNext()->setNext(p->getNext());
p->setNext(p->getPrevious());
p->setPrevious(start);
start->setNext(p);
}
void print(int pos) {
Node* p = first;
for (int i = 0; i < pos; i++) {
p = p->getNext();
}
cout << p->getData() << endl;
}
void printAll() {
cout << "Print: ";
Node* p = first->getNext();
while (p->getData() != 233) {
cout << p->getData() << " ";
p = p->getNext();
}
cout << endl;
}
};
int main() {
int n;
cin >> n;
List* ls = new List();
for (int i = 0; i < n; i++) {
int t;
cin >> t;
ls->insert(new Node(t), i);
//ls->printAll();
}
cin >> n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
switch (t) {
case 1: {
int pos, data;
cin >> pos >> data;
ls->insert(new Node(data), pos);
break;
}
case 2: {
int pos;
cin >> pos;
ls->remove(pos);
break;
}
case 3: {
int s, e;
cin >> s >> e;
ls->reverse(s, e);
break;
}
case 4: {
int pos;
cin >> pos;
ls->print(pos);
break;
}
}
//ls->printAll();
}
system("pause");
return 0;
}