Created
October 10, 2019 04:31
-
-
Save easleyschool/18d2f04cd32f3efbce228dda4f0491e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Inserts the element at the end | |
@param: n as a pointer to the node to add | |
*/ | |
int LinkedList :: addAtEnd(node *n) { | |
//If list is empty | |
if(head == NULL) { | |
//making the new Node as Head | |
head = n; | |
//making the next pointe of the new Node as Null | |
n->next = NULL; | |
} | |
else { | |
//getting the last node | |
node *n2 = getLastNode(); | |
n2->next = n; | |
} | |
} | |
/* | |
gets the last node | |
@returns: the pointer to the last node | |
*/ | |
node* LinkedList :: getLastNode() { | |
//creating a pointer pointing to Head | |
node* ptr = head; | |
//Iterating over the list till the node whose Next pointer points to null | |
//Return that node, because that will be the last node. | |
while(ptr->next!=NULL) { | |
//if Next is not Null, take the pointer one step forward | |
ptr = ptr->next; | |
} | |
return ptr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment