forked from ialbluwi/psut-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_sll.h
213 lines (157 loc) · 3.16 KB
/
int_sll.h
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
#pragma once
#include<iostream>
using std::cout;
using std::string;
class Node {
public:
int val;
Node* next;
Node(int val, Node* next) {
this->val = val;
this->next = next;
}
};
class List {
public:
List();
~List();
Node* head_node() const;
Node* tail_node() const;
bool is_empty() const;
bool contains(int val) const;
int get_at(int index) const;
void add_to_head(int val);
void add_to_tail(int val);
void remove_head();
void remove_tail();
bool remove(int val);
void print() const;
private:
Node* head;
Node* tail;
};
List::List()
{
head = nullptr;
tail = nullptr;
}
List::~List()
{
Node* curr = head;
Node* del_node;
while (curr != nullptr) {
del_node = curr;
curr = curr->next;
delete del_node;
}
}
bool List::is_empty() const
{
return head == nullptr;
}
Node* List::head_node() const
{
return head;
}
Node* List::tail_node() const
{
return tail;
}
void List::add_to_head(int val)
{
Node* new_node = new Node(val, head);
head = new_node;
if (tail == nullptr)
tail = new_node;
}
void List::add_to_tail(int val)
{
Node* new_node = new Node(val, nullptr);
if (tail == nullptr)
head = new_node;
else
tail->next = new_node;
tail = new_node;
}
void List::remove_head()
{
if (is_empty())
return;
Node* temp = head;
head = head->next;
delete temp;
if (head == nullptr)
tail = nullptr;
}
void List::remove_tail()
{
if (head == tail) {
remove_head();
return;
}
Node* prev = head;
while (prev->next != tail)
prev = prev->next;
delete tail;
tail = prev;
tail->next = nullptr;
}
bool List::remove(int val)
{
if (is_empty())
return false;
if (val == head->val) {
remove_head();
return true;
}
if (val == tail->val) {
remove_tail();
return true;
}
Node* node = head->next;
Node* pred = head;
while (node != nullptr) {
if (node->val == val) {
pred->next = node->next;
delete node;
return true;
}
node = node->next;
pred = pred->next;
}
return false;
}
bool List::contains(int val) const
{
for (Node* curr = head; curr != nullptr; curr = curr->next)
if (curr->val == val)
return true;
return false;
}
int List::get_at(int index) const
{
if (is_empty() || index < 0)
throw string("ERROR: Invalid argument in function get_at(int)");
int count = 0;
Node* curr = head;
while (count != index && curr != nullptr) {
curr = curr->next;
count++;
}
if (curr != nullptr)
return curr->val;
else
throw string("ERROR: Invalid argument in function get_at(int)");
}
void List::print() const
{
cout << "[";
Node* curr = head;
while (curr != nullptr) {
cout << curr->val;
if (curr->next != nullptr)
cout << ", ";
curr = curr->next;
}
cout << "]";
}