-
Notifications
You must be signed in to change notification settings - Fork 267
/
SinglyLinkedList.cpp
174 lines (123 loc) · 2.63 KB
/
SinglyLinkedList.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
#include <iostream>
using namespace std;
struct Node {
int val;
Node *next;
Node(int val) : val(val), next(nullptr) {}
Node(int val, Node *next) : val(val), next(next) {}
};
void print_list(Node *head) {
auto aux = head;
cout << '[';
while (aux) {
cout << aux->val;
if (aux->next) {
cout << ", ";
}
aux = aux->next;
}
cout << ']' << endl;
}
void push_back(Node **head, int val) {
Node *newNode = new Node(val);
if (*head == nullptr) {
*head = newNode;
}
else {
auto aux = *head;
while (aux->next) {
aux = aux->next;
}
aux->next = newNode;
}
}
void push_front(Node **head, int val) {
Node *newNode = new Node(val, *head);
*head = newNode;
}
void insert_in_position(Node **head, int val, int pos) {
if (pos == 0) {
push_front(head, val);
return;
}
Node *newNode = new Node(val);
auto prev = *head;
auto curr = *head;
while (curr->next && pos) {
prev = curr;
curr = curr->next;
pos--;
}
prev->next = newNode;
newNode->next = curr;
}
void pop_back(Node **head) {
if (*head == nullptr) {
return;
}
auto prev = *head;
auto curr = prev->next;
while (curr->next) {
prev = prev->next;
curr = curr->next;
}
prev->next = nullptr;
delete (curr);
}
void pop_front(Node **head) {
if (*head == nullptr) {
return;
}
auto aux = *head;
*head = aux->next;
delete (aux);
}
void remove_from_position(Node **head, int pos) {
if (*head == nullptr) {
return;
}
if (pos == 0) {
pop_front(head);
return;
}
auto prev = *head;
auto curr = *head;
while (curr->next && pos) {
prev = curr;
curr = curr->next;
pos--;
}
if (pos == 0) {
prev->next = curr->next;
delete (curr);
}
}
int main() {
Node *head = nullptr;
cout << "Inserting the elements at the end [1, 2, 3]:" << endl;
push_back(&head, 1);
push_back(&head, 2);
push_back(&head, 3);
print_list(head);
cout << "Inserting the elements at the beginning [0, -1, -2]:" << endl;
push_front(&head, 0);
push_front(&head, -1);
push_front(&head, -2);
print_list(head);
cout << "Inserting in positions 3, 4, 5 the elements [997, 998, 999] "
"respectively:"
<< endl;
insert_in_position(&head, 997, 3);
insert_in_position(&head, 998, 4);
insert_in_position(&head, 999, 5);
print_list(head);
cout << "Removing last element:" << endl;
pop_back(&head);
print_list(head);
cout << "Removing first element:" << endl;
pop_front(&head);
print_list(head);
cout << "Removing element in position 2:" << endl;
remove_from_position(&head, 2);
print_list(head);
}