Skip to content

Instantly share code, notes, and snippets.

@easleyschool
Created October 10, 2019 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easleyschool/18d2f04cd32f3efbce228dda4f0491e8 to your computer and use it in GitHub Desktop.
Save easleyschool/18d2f04cd32f3efbce228dda4f0491e8 to your computer and use it in GitHub Desktop.
/*
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