Sunday, May 18, 2008

Adding new node to a Linked List at head/tail


Algorithm for adding a new node at head

  • Allocate memory for the new node
  • Assign the element of the new node with the required value
  • Assign the link of the new node with the current head of the list
  • Set the current head to the new node


Algorithm for adding a new node at tail

  • Allocate memory for the new node
  • Set the element of the new node to the required value
  • Set the link of the new node to 'null' (as it'll be the last node)
  • Check if the list is empty or not
  • If the list is empty, simply assign the new node as the head
  • Else if the list is non-empty, iterate to the current tail of the list
  • Set the link of the current tail to the new node



Share/Save/Bookmark


1 comment:

Anonymous said...

Hi,
Its a very concise definition & differences b/w Array & Linked list.
For this much concept, we would have to study atleast 2 pages each of Array & Linked List topic.
For interview purpose, its very very impressive.

Thanks~
Tarun