-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.cpp
188 lines (155 loc) · 3.29 KB
/
linked_list.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
#include <chrono> // for high_resolution_clock
#include <fstream> // for ofstream
#include <iostream> // for cout
#include <stdexcept> // for runtime_error
#include <vector> // for vector (exercise 1f)
using namespace std;
using namespace std::chrono;struct
Node{
int value;
Node *next = nullptr;
Node *prev = nullptr;
Node(int value) : value(value)
{
}
Node(int value, Node *next) : value(value), next(next)
{
}
};
class LinkedList
{
private:
Node *head = nullptr;
Node *tail = nullptr;
public:
LinkedList(){
}
void append(int value)
{
Node *node = new Node{value};
if (head==nullptr){
head = node;
return;
}
Node *current;
current = head;
while (current->next){
current = current->next;
}
current->next=node;
}
void print() {
Node* current = head;
cout << "[";
while (current->next != nullptr) {
cout << current->value;
cout << ", ";
current = current->next;
}
cout << current->value << "]" << endl;
}
~LinkedList() {
Node* current;
Node* next;
current = head;
while (current != nullptr) {
next = current->next;
delete current;
current = next;
}
}
int length() {
Node* current = head;
int count = 0;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
int& operator[](int index) {
return get_node(index)->value;
}
Node* get_node(int index) {
if (index < 0 or index >= length()) {
throw range_error("IndexError: Index out of range");
}
Node* current = head;
for (int i=0; i<index; i++) {
current = current->next;
}
return current;
}
void insert(int val, int index) {
if (index<length()){
Node* prev = get_node(index-1);
Node* next = prev->next;
prev->next = new Node(val, next);
}
}
void pop() {
Node* current;
Node* previous;
current = head;
while(current->next!=NULL)
{
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
void remove(int pos){
Node *current;
Node* previous;
current = head;
for(int i=0; i < pos; i++)
{
previous=current;
current=current->next;
}
previous->next=current->next;
delete current;
}
int pop(int pos){
int val = pos;
remove(pos);
return val;
}
};
//int main(){
// cout << "Linked Array list - insert front \n";
// ofstream ofs{"linked_list_insert.txt"};
// if (!ofs) {
// throw runtime_error("Unable to open file");
// }
// for (int N = 100; N < 1E6; N *= 10) {
// auto start = high_resolution_clock::now();
// LinkedList ll{};
// for (int i = 0; i < N; i++) {
// ll.insert(i, 0);
// }
//
// auto stop = high_resolution_clock::now();
// auto duration = duration_cast<microseconds>(stop - start);
// cout << N << " " << duration.count() / (double) N << "\n";
// ofs << N << " " << duration.count() / (double) N << "\n";
// }
// return 0;
//}
int main(){
LinkedList ll{};
ll.append(2);
ll.append(3);
ll.append(1);
ll.print();
ll.insert(77,2);
ll.print();
cout<< ll.pop(2) <<endl;
ll.remove(2);
ll.print();
ll.pop();
ll.print();
return 0;
}