-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.cc
184 lines (145 loc) · 2.8 KB
/
List.cc
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
#include <iostream>
#include "List.h"
List::~List() { delete head; }
void List::print() {
Node *cur = head;
std::cout << "The list is: " << std::endl;
while(cur) {
std::cout << cur->data << " ";
cur = cur->next;
}
std::cout << std::endl;
}
// O(1)
void List::addToFront(int n) {
head = new Node{ n,head };
}
// O(n)
int List::at(int i) {
Node* cur = head;
if(!cur) {
// std::cout << "List empty";
throw std::out_of_range{"f"};
}
for (int j = 0; j < i; ++j) {
cur = cur->next;
}
return cur->data;
}
void List::insert_at_end(int x) {
Node* cur = head;
// Head is nullptr
if(!head) {
head = new Node{x,nullptr};
return;
}
while (cur->next) {
cur = cur->next;
}
Node* insert_node= new Node{ x,nullptr };
cur->next= insert_node;
return;
}
void List::insert(int data,int pos) {
if (pos == 0) {
addToFront(data);
return;
}
Node* cur = head;
// Previous Node
for (int i = 0; i < pos - 1; i++) {
cur = cur->next;
}
if (cur) {
Node* next_node = cur->next;
cur->next = new Node{ data,next_node };
}
else {
Node* insert_node= new Node{ data,nullptr };
//cur = insert_node;
cur->next = insert_node;
}
}
void List::delete_node(int pos) {
Node* cur = head;
for (int i = 0; i < pos - 1; i++) {
cur = cur->next;
}
if(cur->next) {
if (pos == 0) {
head = cur->next;
}
else {
//std::cout << cur->data;
Node* to_be_deleted = cur->next;
//std::cout << to_be_deleted->data;
cur->next = to_be_deleted->next;
//cur = to_be_deleted->next;
to_be_deleted->next = nullptr;
delete to_be_deleted;
}
}
}
void List::reverse() {
Node* cur = head;
int i = 0;
Node* temp;
Node* prev = nullptr;
while (cur != nullptr) {
temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
head = prev;
}
Node* reverse_r_acc(Node* cur,Node* prev,Node* temp) {
if (cur != nullptr) {
temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
return reverse_r_acc(cur, prev, temp);
}
else {
return prev;
}
}
void List::reverse_r() {
Node* prev = reverse_r_acc(head, nullptr, head->next);
head = prev;
}
void print_rev(Node* p) {
if (p == NULL) {
return;
}
print_rev(p->next);
std::cout << p->data << std::endl;
}
void List::reverse_print() {
print_rev(head);
}
void List::delete_at_end() {
Node* cur = head;
// Head is nullptr
if(!head) {
std::cout << "Cannot delete" << std::endl;
return;
}
if(!head->next) {
delete head->next;
head = nullptr;
return;
}
Node *prev;
while (cur->next) {
prev = cur;
cur = cur->next;
}
// prev points at the previous node that is not be deleted
// prev->next points at the next node to be deleted
Node* to_be_deleted = prev->next;
prev->next = nullptr;
delete to_be_deleted;
return;
}