java - Practicality of head and tail in a linked list -


from come understand head point first node in list. tail point last node in list.

questions:

1) in terms of practicality, why having tail useful?

i understand having head useful because need sentinel node contains null reference job of head.

2) matter if display list starting head or starting tail?

i seen linked list implementations tail , other implementations no tail.

public void insert(link link)     {             // executes once             // when linked list empty             if(head == null)             {                 // next point head                 link.next = head;                 // head point inserted link                 head = link;                 // tail point inserted link                 tail = link;             }             else             {                  // next point tail                 link.next = tail;                 // tail point inserted link                  tail = link;             }      }      public void display()     {         // display linked list starting tail head          while(tail != null)         {             system.out.println(tail.data);             tail = tail.next;          }     } 

at start of linked list,both head , tail point null. when new node added, both of them point new element. but, again,just new element added again, head points @ first element always,whereas, tail points new element added.

head points starting node of linked list, , tail points last node of linked list.

  1. a tail allows back-referencing faster. like, adding element last node, traversing reverse order of linked list. hence, tail plays major role. performing same operations using head pointer cumbersome.

head doesn't change position once assigned node, whereas, tail gets shifted till last node of linked last.

  1. well, display of linked list nodes display order on basis how added in list. so,yes,it matters , displaying head or tail different , vice-versa of 1 another.

i hope clears confusion.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -