c++ - 循环链表的循环起始节点

标签 c++ c algorithm data-structures

我正在尝试实现一个程序来查找循环链表的起始节点。我的代码是-

struct node
{
char data;
struct node *link;
} ;

char FindStartNode(struct node **q)
{
    struct node *r,*t;
 r = *q; 
 t = *q;
 while(t->link != NULL)
 {
     r = r->link;
     t = t->link->link;
     if(r == t)
     {
         break;
     }
 }
 if(t == NULL )
     return NULL;
 r = *q;
 while(r != t)
 {
     t = t->link;
     r = r->link;
 }
 return t->data;
}


int main()
{
struct node *p;
p = NULL;
char num;
Append(&p,'A');
Append(&p,'B');
Append(&p,'C');
Append(&p,'D');
Append(&p,'E');
Append(&p,'C');
Display(p);
 num = FindStartNode(&p);
printf("\nStarting node of the cycle linked list is:- %c",num);
_getch();
return 0;
}


int Append(struct node **q, char data)
{
struct node *r,*t;
r = (struct node *)malloc(sizeof(struct node));
r->data = data;
r->link = NULL;
if(*q == NULL)
    *q = r;
else
{
  t = *q;
  while(t->link != NULL)
  {
      t = t->link;
  }
  t->link = r;
    }
return 0;
}
int Display(struct node *q)
{
while(q != NULL)
{
    printf("%c\t",q->data);
    q = q->link;
}
return 0;
}

这是我的代码。我在 return t->data 部分没有得到任何值,或者我无法找到循环墨水列表的起始节点。有什么帮助吗?

最佳答案

 t = t->link->link; // t->link can be null 
   //so t = t->link->link can be a crash or illegal reference

将循环更改为:

 while(t != NULL)
 {
     r = r->link;
     t = t->link;
     if(t == NULL)
       break; // or return no circle
     else t = t->link;
     if(r == t)
     {
         break;
     }
 }

我已经检查了你的代码。对比算法讨论here好像没问题。但是你返回的是 char 为什么不返回一个指针,这样你就可以检查它是否为 NULL。如果它不为空,则发出 pt->tada。这更有意义。

我检查了你的代码,你似乎没有在 Append() 中正确实现循环链​​表。我在下面为您提供了一个有效的实现。看看我是如何修改 Append()

#include <stdio.h>
#include <stdlib.h>

struct node
{
char data;
struct node *link;
} ;

char FindStartNode(struct node **q)
{
    struct node *r,*t;
 r = *q;
 t = *q;
 while(t->link != NULL)
 {
     r = r->link;
     t = t->link->link;
     if(r == t)
     {
         break;
     }
 }
 if(t == NULL )
     return NULL;
 r = *q;
 while(r != t)
 {
     t = t->link;
     r = r->link;
 }
 return t->data;
}

int Append(struct node **q, char data);
int main()
{
struct node *p;
p = NULL;
char num;
Append(&p,'A');
Append(&p,'B');
Append(&p,'C');
Append(&p,'D');
Append(&p,'E');
Append(&p,'C');
//Display(p);
 num = FindStartNode(&p);
printf("\nStarting node of the cycle linked list is:- %c\n",num);
//_getch();
return 0;
}

int Append(struct node **q, char data)
{

struct node *r,*t, *startOfcycle=NULL;
r = (struct node *)malloc(sizeof(struct node));
r->data = data;


r->link = NULL;
if(*q == NULL)
    *q = r;
else
{
  t = *q;
  while(t->link != NULL)
  {
      if(t->data == data)
         startOfcycle = t;

      t = t->link;
  }


  if(startOfcycle == NULL)
  t->link = r;
  else {// there is a cycle point to the start of cycle
     t->link = startOfcycle;
     free(r);
  }
    }
return 0;
}
int Display(struct node *q)
{
while(q != NULL)
{
    printf("%c\t",q->data);
    q = q->link;
}

请注意 Display 函数也是错误的,因为链表的无限循环是循环的。我没有修改它,因为它与你的问题无关。谢谢。

关于c++ - 循环链表的循环起始节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14330064/

相关文章:

c++ - 查找符号时,程序不会从正确的库中搜索

c++ - 多个左值等价定义

c - 错误 : assignment of member ai_family in read-only object

c - getc 中的段错误

algorithm - Matlab 图像处理构建区域连接

algorithm - 三边测量算法将 3 个圆定位为尽可能靠近而不重叠

c++ - 初始化 vector 中的指针

c++ - 初始化一个结构,其成员是另一个结构的数组

c - 获取将文档打印到 IPP 打印机的真实用户

c++ - KMP算法的时间复杂度