-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
268 lines (242 loc) · 6.48 KB
/
main.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <iostream>
#include <ctime>
#include <useful.cpp>
using namespace std;
class IndexOutOfBound:public exception{
public:
string cause;
string solution;
time_t when;
IndexOutOfBound(string cause, string solution, time_t when){
this->cause = cause;
this->solution = solution;
this->when = when;
}
const char* what() const throw(){
return "something";
}
void show(){
cout<<"-------------------------"<<endl;
cout<<"Cause : "<<cause<<endl;
cout<<"Solution : "<<solution<<endl;
cout<<"When : "<<ctime(&when)<<endl;
}
};
class Node{
public:
int key;
int value;
Node* prev;
Node* next;
Node(){};
Node(int x){
value = x;
prev = nullptr;
next = nullptr;
}
Node(int k, int v){
key = k;
value = v;
prev = nullptr;
next = nullptr;
}
};
class LinkedList{
private:
Node* head = nullptr;
Node* tail = nullptr;
int length = 0;
public:
LinkedList(){}
LinkedList(LinkedList& ll){
Node* temp = ll.head;
for (int i = 0; i < ll.length; i++)
{
temp = temp->next;
ll.push_back(temp->value);
}
}
~LinkedList(){
if(head != nullptr){
while(head!=tail){
head=head->next;
delete head->prev;
}
delete head;
}
}
LinkedList& operator=(LinkedList& ll){
Node* temp = ll.head;
for (int i = 0; i <ll.length; i++)
{
ll.push_back(temp->value);
temp = temp->next;
}
return *this;
}
void insert(int x, int index){
Node* inserted = new Node(x);
Node* temp = head;
if(is_empty()){
head = tail =inserted;
length++;
return;
}
if(index==0){
push_front(x);
}else if(index==length-1){
push_back(x);
}else{
for (int i = 0; i !=index; i++){
temp=temp->next;
}
temp->prev->next=inserted;
inserted->prev=temp->prev;
temp->prev=inserted;
inserted->next=temp;
}
}
void push_back(int x){
Node* temp = new Node(x);
if(head == nullptr){
head = tail = temp;
}else{
tail-> next = temp;
temp -> prev =tail;
tail = temp;
}
length++;
}
void push_front(int x){
Node* temp = new Node(x);
if(head == nullptr){
head = tail = temp;
}else{
temp ->next = head;
head-> prev =temp;
head = temp;
}
length++;
}
void print(){
for(Node* temp = head;temp != nullptr;temp = temp-> next){
cout<<temp->value<<" ";
}
}
void print_reverse(){
for(Node* temp = tail;temp != nullptr;temp = temp-> prev){
cout<<temp->value<<" ";
}
}
int pop_back(){
int x = tail->value;
tail = tail->prev;
delete tail->next;
tail->next = nullptr;
length--;
return x;
}
int pop_front(){
int x = head->value;
head = head->next;
delete head->prev;
head->prev = nullptr;
length--;
return x;
}
int pop(int n){
if(is_empty()){
return -1;
}
if(n==0){
return pop_front();
}
if(n==length-1){
return pop_back();
}
Node* temp = get_node(n);
int x = temp->value;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
delete temp;
--length;
return x;
}
bool is_empty(){
return !head;
}
bool operator==(LinkedList& second){
if(length != second.size()){
return false;
}
Node* temp1 = head;
Node* temp2 = second.head;
for (int i = 0; i < length-1; i++)
{
if(temp1->value!=temp2->value){
return false;
}
temp1 = temp1->next;
temp2 = temp2->next;
}
return true;
}
int operator[](int n){
if(n>=length){
IndexOutOfBound x("The number is bigger than length","Write number from 0 to (length-1)",time(0));
throw x;
}
Node* node = head;
for(int i=0;i<n;i++){
node = node->next;
}
return node->value;
}
Node* get_node(int n){
Node* node = head;
for(int i=0;i<n;i++){
node = node->next;
}
return node;
}
bool find(int x){
Node* node = head;
for(int i=0;i<length;i++){
if(node->value==x){
return true;
}
node = node->next;
}
return false;
}
void insertion_sort(){
int temp;
int n = 0;
for (Node* a = head; a->next!=nullptr; a = a->next){
if(a->value > a->next->value){
temp = a->next->value;
pop(n+1);
insert(temp,n);
}
n++;
}
}
int size(){
return length;
}
};
int main()
{
LinkedList ll;
ll.push_back(12);
ll.push_back(14);
ll.push_back(13);
ll.push_back(10);
ll.push_back(11);
ll.push_back(333);
ll[168];
ll.insertion_sort();
ll.print();
cout<<endl;
return 0;
}