Skip to content

Commit a304f18

Browse files
committed
adding deep copy and deep copy nodes
1 parent a8f2fe1 commit a304f18

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

OneWayLinkedList.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,42 @@ void OneWayLinkedList::deleteNodeBeforeIndex(int index) {
297297
}
298298
}
299299

300+
// Maloc a new node
301+
Node* OneWayLinkedList::mallocNode(int data) {
302+
Node* node = new Node(data);
303+
return node;
304+
}
305+
306+
// Deep copy
307+
// Buggy
308+
OneWayLinkedList* OneWayLinkedList::deepCopy() {
309+
OneWayLinkedList* newList = new OneWayLinkedList();
310+
Node* node = this->head;
311+
while (node != NULL) {
312+
newList->addEnd(node->data);
313+
node = node->next;
314+
}
315+
return newList;
316+
}
317+
318+
// Deep copy nodes
319+
Node* OneWayLinkedList::deepCopyNodes() {
320+
Node* node = this->head;
321+
Node* prev = NULL;
322+
Node* newHead = NULL;
323+
while (node != NULL) {
324+
Node* newNode = mallocNode(node->data);
325+
if (prev == NULL) {
326+
newHead = newNode;
327+
} else {
328+
prev->next = newNode;
329+
}
330+
prev = newNode;
331+
node = node->next;
332+
}
333+
return newHead;
334+
}
335+
300336
// Count number of nodes
301337
int OneWayLinkedList::count() {
302338
int count = 0;

OneWayLinkedList.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class OneWayLinkedList {
2929
// Add new node to the end of the list
3030
void addEnd(int data);
3131

32+
// Deep copy
33+
OneWayLinkedList* deepCopy();
34+
35+
// Deep copy nodes
36+
Node* deepCopyNodes();
37+
3238
// Print all nodes
3339
void print();
3440

@@ -53,6 +59,9 @@ class OneWayLinkedList {
5359
// Check there is a node with a specific data in the list or not (with a recursive function)
5460
int hasNodeRecursive(int data, Node* node);
5561

62+
// Maloc a new node
63+
Node* mallocNode(int data);
64+
5665
// Delete a node with a specific data
5766
void deleteNode(int data);
5867

example.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,29 @@ int main(int argc, char** argv) {
9797
cout << "Node not found" << endl;
9898
}
9999

100+
// Deep copy the list
101+
cout << "Deep copy the list" << endl;
102+
OneWayLinkedList* list2 = new OneWayLinkedList();
103+
list2 = list->deepCopy();
104+
105+
// Deep copy the nodes
106+
Node* node2 = list->deepCopyNodes();
107+
if (node2 == NULL) {
108+
cout << "Node not found" << endl;
109+
} else {
110+
cout << "Node data: " << node2->data << endl;
111+
}
112+
100113
// Delete all nodes
114+
cout << "Delete all nodes" << endl;
101115
list->deleteAll();
102116

103117
// Print all nodes
118+
cout << "Print the new list" << endl;
119+
list2->print();
120+
121+
// Print all nodes
122+
cout << "Print the first list" << endl;
104123
list->print();
105124

106125
// Print the size of the list

0 commit comments

Comments
 (0)