Skip to content

Commit

Permalink
Better memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazako committed Mar 6, 2023
1 parent 7055748 commit 2e58239
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions double_linked_list/DoubleLinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int DoubleLinkedList::pop() {
if (size == 1) {
int data = head->data;
delete head;
head = nullptr;
return data;
}
Node* nodePtr = head;
Expand All @@ -41,6 +42,7 @@ int DoubleLinkedList::pop() {
int data = nodePtr->next->data;
delete nodePtr->next;
nodePtr->next = nullptr;
size--;
return data;
}

Expand All @@ -55,6 +57,9 @@ void DoubleLinkedList::printList() {

void DoubleLinkedList::printReversed() {
auto* nodePtr = head;
if (nodePtr == nullptr) {
return;
}
while (nodePtr->next != nullptr) {
nodePtr = nodePtr->next;
}
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void simpleDoubleLinkedListTest() {
list->pop();
list->printList();
list->printReversed();
delete list;
}

void simpleDynamicArrayTest() {
Expand Down

0 comments on commit 2e58239

Please sign in to comment.