-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.cpp
181 lines (170 loc) · 5.53 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
// linked_list.cc -- functions for linked_list lab (cs221)
#include "linked_list.h"
/**
* Inserts a new Node (with key=newKey) at the head of the linked_list.
* PRE: head is the first node in a linked_list (if NULL, linked_list is empty)
* PRE: newKey is the value for the key in the new Node
* POST: the new Node is now the head of the linked_list
*/
void insert(Node*& head, int newKey) {
Node * curr = new Node;
curr->key = newKey;
curr->next = head;
head = curr;
}
/**
* Print the keys of a linked_list in order, from head to tail
* PRE: head is the first node in a linked_list (if NULL, linked_list is empty)
*/
void print(Node* head) {
std::cout << "[";
for (Node* curr = head; curr != NULL; curr = curr->next){
std::cout << curr->key;
if (curr->next != NULL) std::cout << ", ";
}
std::cout << "]" << std::endl;
}
/**
* Returns the size (number of Nodes) of the linked_list
* PRE: head is the first node in a linked_list (if NULL, linked_list is empty)
*/
int size(Node* head){
if (! head) return 0;
return 1 + size(head->next);
}
/**
* Copies the keys in a linked list to a vector.
* PRE: head is the first node in a linked_list (if NULL, linked_list is empty)
* POST: a new vector<int> containing the keys in the correct order has been returned.
*/
std::vector<int> to_vector(Node* head) {
std::vector<int> result;
for (Node* curr = head; curr != NULL; curr = curr->next){
result.push_back(curr->key);
}
return result;
}
/**
* Delete the last Node in the linked_list
* PRE: head is the first Node in a linked_list (if NULL, linked_list is empty)
* POST: the last Node of the linked_list has been removed
* POST: if the linked_list is now empty, head has been changed
* POST: else head remains the first Node in the linked_list
*/
void delete_last_element(Node*& head){
if(head == NULL) {
return;
}
if(head->next == NULL) {
delete head;
head = NULL;
return;
}
Node* curr = head;
while(curr->next->next != NULL) {
curr = curr -> next;
}
delete curr->next;
curr->next = NULL;
curr = NULL;
}
/**
* Removes an existing Node (with key=oldKey) from the linked_list.
* PRE: head is the first node in a linked_list (if NULL, linked_list is empty)
* PRE: oldKey is the value of the key in the Node to be removed
* PRE: if no Node with key=oldKey exists, the linked_list has not changed
* POST: if a Node with key=oldKey was found, then it was deleted
* POST: other Nodes with key=oldKey might still be in the linked_list
* POST: head is the new first Node of the linked_list, if updated
*/
void remove(Node*& head, int oldKey) {
if(head == NULL) {
return;
}
if(head->key == oldKey) {
Node* temp = head->next;
delete head;
head = temp;
temp = NULL;
return;
}
Node* curr = head;
while(curr->next->key != oldKey && curr->next->next != NULL) {
curr = curr->next;
}
if(curr->next->key == oldKey) {
Node* temp = curr->next;
curr->next = curr->next->next;
delete temp;
temp = NULL;
}
curr = NULL;
}
/**
* Insert a new Node (with key=newKey) after an existing Node (with key=oldKey)
* If there is no existing Node with key=oldKey, then no action.
* PRE: head is the first Node in a linked_list (if NULL, linked_list is empty)
* PRE: oldKey is the value to look for (in the key of an existing Node)
* PRE: newKey is the value of the key in the new Node (that might be inserted)
* POST: if no Node with key=oldKey was found, then the linked_list has not changed
* POST: else a new Node (with key=newKey) is right after the Node with key = oldKey.
*/
void insert_after(Node* head, int oldKey, int newKey){
if(head == NULL) {
return;
}
Node* curr = head;
while(curr->key != oldKey && curr->next != NULL) {
curr = curr->next;
}
if(curr->key == oldKey) {
Node* temp = curr->next;
curr->next = new Node;
curr->next->key = newKey;
curr->next->next = temp;
temp = NULL;
}
curr = NULL;
}
/**
* Create a new linked_list by merging two existing linked_lists.
* PRE: list1 is the first Node in a linked_list (if NULL, then it is empty)
* PRE: list2 is the first Node in another linked_list (if NULL, then it is empty)
* POST: A new linked_list is returned that contains new Nodes with the keys from
* the Nodes in list1 and list2, starting with the key of the first Node of list1,
* then the key of the first Node of list2, etc.
* When one list is exhausted, the remaining keys come from the other list.
* For example: [1, 2] and [3, 4, 5] would return [1, 3, 2, 4, 5]
*/
Node* interleave(Node* list1, Node* list2){
Node* head = new Node;
Node* curr = head;
while(list1 != NULL && list2 != NULL) {
curr->next = new Node;
curr->next->key = list1->key;
curr->next->next = NULL;
curr = curr->next;
list1 = list1->next;
curr->next = new Node;
curr->next->key = list2->key;
curr->next->next = NULL;
curr = curr->next;
list2 = list2->next;
}
while(list1 != NULL) {
curr->next = new Node;
curr->next->key = list1->key;
curr->next->next = NULL;
curr = curr->next;
list1 = list1->next;
}
while(list2 != NULL) {
curr->next = new Node;
curr->next->key = list2->key;
curr->next->next = NULL;
curr = curr->next;
list2 = list2->next;
}
curr = NULL;
return head->next;
}