简体   繁体   English

确定循环双向链接列表的头是否是“跳转”周期的一部分

[英]Determing if the head of a circular doubly-linked list is part of a “jump” cycle

There's a doubly linked list which is also circled (ie, the last node's next pointer points to the head of the list and the head's prev pointer points to the last node). 有一个双向链表这也是盘旋(即最后一个节点的next指针指向列表的头部和头部的prev指针指向最后一个节点)。

The data in each node is an integer. 每个节点中的data都是整数。

A jump in a circular list is defined as follows for a node p : 对于节点p循环列表中的跳转定义如下:

  • if p.data is positive, we move p.data times using the next pointer 如果p.data为正,我们使用next指针移动p.data
  • if p.data is negative, we move p.data times using the prev pointer 如果p.data为负,我们使用prev指针移动p.data
  • if p.data is 0, we don't move at all. 如果p.data为0,则完全不移动。

I need to write a method that receives a pointer to the head of the list as a parameter and returns true if there's a jump path that starts and ends at the head node. 我需要编写一个方法,该方法接收指向列表开头的指针作为参数,如果存在在开头节点处开始和结束的跳转路径,则返回true

An example list: 示例列表:

node | data | next | prev
------|------|------|------
   0  |   2  |   1  |   5
   1  |  14  |   2  |   0
   2  |  -5  |   3  |   1
   3  |   1  |   4  |   2
   4  |  -4  |   5  |   3
   5  |   1  |   0  |   4

For this list, the method should return true . 对于此列表,该方法应返回true Starting at the head (node 0), we move forward twice to node 2, then back 5 times to node 3, forward once to node 4, and finally back 4 times, returning to node 0: 从头(节点0)开始,我们向前移动两次至节点2,然后向后移动5次至节点3,向前移动一次至节点4,最后向后移动4次,返回节点0:

node  | jumps | next node
-------|-------|-----------
   0   |   2   |     2
   2   |  -5   |     3
   3   |   1   |     4
   4   |  -4   |     0

My main problem is when I should return false . 我的主要问题是何时应该返回false

This is not homework (I'm working on different exercises in order to prepare my self for an exam) 这不是家庭作业(我正在做不同的练习,以便为考试做准备)

There are plenty of ways to do it. 有很多方法可以做到这一点。

For example you can return false if you didn't get back to the head after n jumps (n being the length of the list) as it means you are looping in another part of the list. 例如,如果您在n次跳跃之后没有回到头部(n是列表的长度),则可以返回false,因为这意味着您正在循环访问列表的另一部分。

You can also mark the visited nodes and return false if you visit twice any node (apart from the head). 您还可以标记访问过的节点,如果两次访问任何节点(头部除外),则返回false。

You also have to return false directly if you reach a 0 which is not the head of the list. 如果到达的不是列表的开头0,则还必须直接返回false。

First decide when the method will stop ie what is the halting condition as given your question above again the method will start moving and will return to 2 and this process will continue ever. 首先确定该方法何时停止,即再次出现上述问题时停止条件是什么,该方法将再次开始移动并返回2,此过程将继续进行。 The only halting condition specified above is the occurrence of zero in the zero otherwise this program will never halt. 上面指定的唯一停止条件是零中出现零,否则该程序将永不停止。

If coming to the head node can also result in halting of the program then the code can be completed in this way: 如果到达头节点也可能导致程序停止,则可以通过以下方式完成代码:

findHead(head,originalHead,calledCount){
    if(originalHead==null)
    {
        return false;
    }
    else if (head==originalHead && calledCount>1) 
    /*If while roaming gets to the head then returns true but except the first time where the head will always be equal to original head */
    {                   
        return true;
    }
    else {
        p=head;
        if(p.data>0)
        {
            steps=p.data;
            while(steps>0)
            {
                p=p->next;
                steps--;
            }
            findHead(p,originalHead,calledCount);
        }
        else if(p.data<0)
        {
            steps=p.data;
            while(steps<0)
            {
                p=p->prev;
                steps++;
            }
            findHead(p,originalHead,calledCount);
        }
        else    // If obtained 0 in the list
        {
            if (p==originalHead)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    calledCount++;
} 

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

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