diff --git a/linked-lists/c-or-cpp/singly.cpp b/linked-lists/c-or-cpp/singly.cpp index 019e8174..64bd2f93 100644 --- a/linked-lists/c-or-cpp/singly.cpp +++ b/linked-lists/c-or-cpp/singly.cpp @@ -8,6 +8,7 @@ class Node { Node(T data) : data(data) , next(nullptr) {}; + Node(){} }; template @@ -34,6 +35,27 @@ class SinglyLinkedList { // change head point to new node this->head = temp; } + + void insertAtEnd(T data){ + + Node *newNode = new Node(data); + + // if head is not pointing to anything then simply point to new node + if(this->head == nullptr) { + this->head = newNode; + return; + } + + // if list is not empty, then traverse to the last node using `temp`, then put the address of the new node in the temp->next + // and finally, make the next of newNode = NULL + Node *temp = new Node(); + temp = head; + while(temp->next != NULL){ + temp = temp->next; + } + temp->next = newNode; + newNode->next= NULL; + } T removeAtHead() { // if list is empty we can't remove node @@ -73,6 +95,7 @@ int main() { l.insertAtHead(1); l.insertAtHead(2); l.insertAtHead(3); + l.insertAtEnd(5); l.removeAtHead(); l.printList(); return 0;