aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework 3/SinglyLinkedList.hpp
blob: e28c1421d67faabd40a4a979da42a0f3c9bba6f3 (plain) (blame)
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
#ifndef SINGLY_LINKED_LIST_HPP
#define SINGLY_LINKED_LIST_HPP

struct ListNode
{
	int _data{ 0 };
	ListNode* _next{ nullptr };
};

struct SinglyLinkedList
{
	size_t _size{ 0 };
	ListNode* _head{ nullptr };
};

bool Append(SinglyLinkedList* list, ListNode* node);
bool Prepend(SinglyLinkedList* list, ListNode* node);
bool RemoveFirst(SinglyLinkedList* list);
bool RemoveLast(SinglyLinkedList* list);
bool InsertAfter(SinglyLinkedList* list, const int data, ListNode* node);
bool InsertBefore(SinglyLinkedList* list, const int data, ListNode* node);
bool Clear(SinglyLinkedList* list);
ListNode* Extract(SinglyLinkedList* list, const int data);
bool Empty(SinglyLinkedList* list);
bool Remove(SinglyLinkedList* list, ListNode* node);

//code
bool Append(SinglyLinkedList* list, ListNode* node )
{
	//we have a list
	//we need to add a node to the end of the list

	//empty
	if (Empty(list)) 
	{
		list->_head = node;
		list->_size++;
		return true;
	}
	//Append head
	if(list->_size == 1)
	{
		list->_head->_next = node;
		list->_size++;
		return true;
	}
	//Down the list
	ListNode* travel = nullptr;
	for(travel = list->_head; travel->_next != nullptr;)
	{
		travel = travel->_next;
	}
	travel->_next = node;
	list->_size++;
	return true;
}

bool Prepend(SinglyLinkedList* list, ListNode* node) 
{
	if (Empty(list))
	{
		list->_head = node;
		list->_size++;
		return true;
	}

	node->_next = list->_head;
	list->_head = node;

	list->_size++;
	return true;
}

bool RemoveFirst(SinglyLinkedList* list)
{
	//check if zero size
	if (Empty(list)) return true;

	if(list->_size == 1)
	{

		list->_head = nullptr;
	}
	else
	{
		ListNode* prev = list->_head;
		list->_head = list->_head->_next;
		prev->_next = nullptr;
	}

	list->_size--;

	return true;
}

bool RemoveLast(SinglyLinkedList* list)
{
	if (Empty(list)) return true;

	if (list->_size == 1)
	{
		list->_head = nullptr;
		return true;
	}
	
	ListNode* travel = list->_head;
	while(travel->_next->_next != nullptr)
	{
		travel = travel->_next;
	}
	travel->_next = nullptr;
	list->_size--;
	return true;
	
}

bool InsertAfter(SinglyLinkedList* list, const int data, ListNode* node)
{
	//first to find data
	//thoughts? What if we are inserting at after end.
	ListNode* travel;
	for (travel = list->_head; travel->_data != data && travel->_next != nullptr; )
	{
		travel = travel->_next;
	}

	if (travel->_next == nullptr) 
	{
		return Append(list, node);
	}

	node->_next = travel->_next;
	travel->_next = node;
	list->_size++;

	//then insert after data
	return true;
}

bool InsertBefore(SinglyLinkedList* list, const int data, ListNode* node)
{
	if (list->_head->_data == data)
		return Prepend(list, node);

	ListNode* travel;
	for (travel = list->_head; travel->_next->_data != data;)
	{
		travel = travel->_next;
	}

	node->_next = travel->_next;
	travel->_next = node;
	list->_size++;

	return true;
	
}

bool Clear(SinglyLinkedList* list)
{
	//consider the head
	//consider the size
	//We have to detach every node from each other
	//start at the head
	//use a node* to track our position

	if (list->_size == 0) return true;

	ListNode* travel = list->_head;
	ListNode* prev = travel;

	do
	{
		prev = travel;
		travel = travel->_next;
		prev->_data = 0;
		prev->_next = nullptr;
		
	} while (travel != nullptr && travel->_next != nullptr);

	list->_size = 0;
	list->_head = nullptr;

	return true;
}

ListNode* Extract(SinglyLinkedList* list, const int data)
{
	//check if empty, shortcut return
	ListNode* temp = new ListNode{0, nullptr};
	ListNode* travel = nullptr;

	if (Empty(list))
	{
		return temp;
	}
	//Read through list
	for (travel = list->_head; travel->_data != data && travel->_next != nullptr; )
	{
		travel = travel->_next;
	}
	//find the data
		//pointer to the node
	temp->_data = travel->_data;
		//remove node from list
	if(Remove(list, travel))
	{
		return temp;
	}
	//or don't find data

	return nullptr;
}

bool Remove(SinglyLinkedList* list, ListNode* node)
{
	//find the node
	//Read through list
	ListNode* travel = list->_head;
	ListNode* prev = travel;

	while(travel != node)
	{
		prev = travel;

		travel = travel->_next;
	}
	//if first, remove first
	if(travel == list->_head)
	{
		return RemoveFirst(list);
	}
	//if last, remove last
	if(travel->_next == nullptr)
	{
		return RemoveLast(list);
	}
	//else in middle
	//remove travel's node
	//will need previous node
		//to set previous's next to travel's next
	prev->_next = travel->_next; //fixing pointer between nodes
									//removes dangling pointer from prev to travel

	//delete travel
	travel->_next = nullptr;
	travel->_data = 0;
	list->_size--;
}

bool Empty(SinglyLinkedList* list)
{
	return (list->_size == 0 && list->_head == nullptr);
}

#endif // !SINGLY_LINKED_LIST_HPP