简体   繁体   中英

A Circular linked list with no pointers

I am trying to complete a Circular doubly linked List programming project. We are suppose to create the Link List class with Node<E> Head as the only instant variable. The only instant variable in the inner class is Node current. The Node class had three instant variables, E data , Node<E> next , and Node<E> prev .

In a test program that uses our CDLL call we are suppose to use the next() and hasNext() method of the iterator inner class to loop through the entire list once printing out each node. The problem I am having is I can't figure out how the hasNext() method can know when one loop has been completed and every node has been printed.

In other words, how can I get hasNext() to return true when current==head on the first iteration of the loop and return false when current==head at the beginning of the second loop.

I greatly appreciate any insight that you may have.

With limitation of instance variables, it seems impossible.

But, if you do not link your tail node to head node, you can implement "hasNext".

Then it won't be circular anymore.

But you don't have to make the list actually circular.

if you found next node is null, get the head node instead.

Then it will be virtually circular.

EDIT:

In other words, how can I get hasNext() to return true when current==head on the first iteration of the loop and return false when current==head at the beginning of the second loop.

So your issue is that you thing current==head is what matters. That's not what matters, what matters in the circular case is the nextNode() - that is, what will be coming up next?

So Assuming HEAD is a reference to the start of your list, and that the last nodes .next() points to the HEAD node.

// psudocode.
boolean hasNext() 
   if current.next() == head 
       return false
   else
      return true

EDIT:

Ok here's a very simple version of how to do this iteration

public void iterateAndPrint(Node head) :
   Node current = head;
   // check for null case
   if(current == null)
       return
   // case of list where ther eis only one node - HEAD...
   if(!current.hasNext())  
       print current.name
       return
   // ok so the nodes aren't empty, and there are more than just the head node.
   while(true): 
      // as long as has next returns true, print the name and then set current to next node
      if(current.hasNext())
          print current.name
          current = current.next()
      // has next returned false - we're on the last node.  Make sure it's not equal to Head (redundant at this point)
      else if (!current.hasNext() && current!=head) 
          print current.name
          break
      // i don't think this is strictly nescesary, but better safe than sorry.
      else:
          break      
public void hasNext(Node current, Node head):
      if(current.next() == head):
          return false
      else:
          return true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM