简体   繁体   English

没有指针的循环链表

[英]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. 我们假设使用Node<E> Head作为唯一的即时变量来创建链接列表类。 The only instant variable in the inner class is Node current. 内部类中唯一的即时变量是Node current。 The Node class had three instant variables, E data , Node<E> next , and Node<E> prev . Node类具有三个即时变量,即E dataNode<E> nextNode<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. 在使用CDLL调用的测试程序中,我们假设使用迭代器内部类的next()hasNext()方法在打印出每个节点后遍历整个列表。 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. 我遇到的问题是我无法弄清楚hasNext()方法如何知道何时完成一个循环并已打印每个节点。

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. 换句话说,如何让hasNext()在循环的第一次迭代中在current==head时返回true,而在第二次循环的开始时在current==head时返回false。

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". 但是,如果不将尾节点链接到头节点,则可以实现“ 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. 换句话说,如何让hasNext()在循环的第一次迭代中在current == head时返回true,而在第二次循环的开始时在current == head时返回false。

So your issue is that you thing current==head is what matters. 所以您的问题是,重要的是current == head。 That's not what matters, what matters in the circular case is the nextNode() - that is, what will be coming up next? 没关系,在循环情况下,nextNode()才是重要的,也就是说,接下来会发生什么?

So Assuming HEAD is a reference to the start of your list, and that the last nodes .next() points to the HEAD node. 因此,假设HEAD是对列表开头的引用,并且最后一个节点.next()指向HEAD节点。

// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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