data structures - Circular LinkedList in Java -


i brushing on data structures reading book , 1 of questions asks build circular single linked list not using "first" & "last" pointers, rather allow access using 1 reference "current". not sure understand question, thought needed @ least either first or last. here implementation has "first", not sure how around it. can please comment on how can adjust code eliminate reliance on first?

class link {     public int idata;                   public link next;                    public link(int id) { // constructor         idata = id;                              }                                public void displaylink() {         system.out.print(idata + " ");     } }  // end class link 

then here's list itself:

public class circularlinkedlist {     private link first;     private link current;          public link getcurrent(){         return current;     }      public void setcurrent(int data){      }      public void advance(){         current = current.next;     }      public void insert(int data) {         link newlink = new link(data);         if (first == null) {              first = current = newlink;         } else {             current.next = newlink;         }         current = newlink;         newlink.next = first;     }     ... } 

if have circular linked list, each node points next node in continuous loop. there no last , no first. in situation, need 1 pointer data structure (which question calling "current") because can walk whole list until started.

one node might this:

public class listnode<t> {     private t element;     private listnode<t> next; } 

so in environment, when add first node list, it's "next" pointer point itself. when add second node, each "next" pointer point other. when add third node, each point next one, , presumably being ordered in way. each additional node added inserted appropriate place in loop.

a usage data structure schedule gets repeated every day or every hour. of events can stored in circular list , "current" pointer points whichever scheduled next.

obviously, when searching list this, need keep reference first node. way can know if you've searched whole list since doesn't end null "next" pointer.

edit: discussed in (extensive) comments below, "tail" pointer seem idea here insert operations. here original method posted, has been renamed insertafter:

public void insertafter(int data) {     link newlink = new link(data);     if (current == null) {          newlink.next = newlink;         current = newlink;     } else {         newlink.next = current.next;         current.next = newlink;     } } 

a tail pointer point last node of list. node before first node, since list circular. sure maintain (tail.next == current) when manipulating pointers. here new insert method:

public void insert(int data) {     link newlink = new link(data);     if (current == null) {          current = newlink;         tail = newlink;         newlink.next = newlink; // tail.next = current!     } else {         tail.next = newlink; // tail.next = current!         newlink.next = current;         current = newlink; // tail unchanged, newlink current     } } 

inserting values should rightly put them out of order. maintain order when adding, add method should used:

public void add(int data) {     this.insert(data);     this.advance(); } 

here's code advance:

public void advance() {     if (current != null) {         tail = current;         current = tail.next; // tail.next = current!     } } 

when used create lists...

list1.add(1); list1.add(2); list1.add(3); list2.insert(3); list2.insert(2); list2.insert(1); 

...both lists ordered 1-2-3 1 current.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -