如何开发我的代码以查找链接列表中的循环 [英] How Do I Develop My Code To Find A Loop In A Linked List

查看:50
本文介绍了如何开发我的代码以查找链接列表中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码中遇到问题。仅当循环包含链表的第二个元素时,它才有效。我无法理解。



 #include< stdio.h> 
#include< stdlib.h>
struct 节点
{
int 数据;
struct node * link;
} *开始;
void create( struct node * p);
void bug();
void debug();
void display();
int main()
{
int a;
printf( 创建带有循环的链表:);
start =( struct node *)malloc( sizeof struct node *));
create(start);
display();
bug();
printf( 按1进行调试:);
scanf( %d,& a);
if (a == 1
debug();
display();
return 0 ;
}
void create( struct node * start)
{
int a;
printf( \ n输入节点:);
scanf( %d,& start-> data);
printf( 按0结束列表:);
scanf( %d,& a);
if (a == 0
{
start- >链接= NULL;
}
其他
{
start-> link =( struct node *)malloc( sizeof struct node *));
create(start-> link);
}
}
void bug()
{
int i = 1 ,j = 1 ,m1,m2;
struct node * p1,* p2;
p1 =开始,p2 =开始;
printf( 输入要循环的位置:);
scanf( %d%d,& m1,& m2);
while (i< m1)
{
p1 = p1-> link;
i ++;
}
while (j< m2)
{
p2 = p2-> link;
j ++;
}
p2-> link = p1;
printf( \ n该错误已创建\ n);
}
void display()
{
struct node * p;
p = start;
while (p!= NULL)
{
printf( %d \ n,p-> data);
p = p-> link;
}
}
void debug()
{
struct node * temp1,* temp2;
int count1,count2 = 2 ;
temp1 = start;
while (temp1!= NULL)
{
temp2 = start;
count1 = 0 ;
while (temp2!= NULL)
{
count1 = count1 + 1;
if ((temp1-> link == temp2-> link)&&(temp1-> data!= temp2->数据))
{
printf( \ n在位置%d处创建错误%d \ n,count2,count1);
temp2-> link = NULL;
退出;
}
其他
temp2 = temp2-> link;
}
temp1 = temp1-> link;
count2 = count2 + 1;
}
}

解决方案

你的错误是误解了索引。索引全部从空或零开始。



因此将i和j设置为1是问题的开始

< pre lang =c ++> void bug()
{
int i = 1 ,j = 1 ,m1,m2; / / 缺陷



我称第一个元素是头(想象一条蛇)。这是一个完整的元素。

使用头部后你应该使用链接 - 不是之前。



另一个坏错是:

 start =(struct node *)malloc(sizeof(struct node *)); 



只分配指向struct的指针而不是struct !!!


I have problem in the following code. It works only if the loop contains the second element of the linked list. I couldn't figure it out.

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node*link;
}*start;
void create(struct node *p);
void bug();
void debug();
void display();
int main()
{
    int a;
    printf("Creation of linked list with a loop inside:");
    start=(struct node*)malloc(sizeof(struct node*));
    create(start);
    display();
    bug();
    printf("Press 1 for debugging it:");
    scanf("%d",&a);
    if(a==1)
    debug();
    display();
    return 0;
}
void create(struct node *start)
{
    int a;
    printf("\nEnter the node:");
    scanf("%d",&start->data);
    printf("Press 0 to end list:");
    scanf("%d",&a);
    if(a==0)
    {
        start->link=NULL;
    }
    else
    {
        start->link=(struct node*)malloc(sizeof(struct node*));
        create(start->link);
    }
}
void bug()
{
    int i=1,j=1,m1,m2;
    struct node *p1,*p2;
    p1=start,p2=start;
    printf("Enter the position you want to make a loop:");
    scanf("%d %d",&m1,&m2);
    while(i<m1)
    {
        p1=p1->link;
        i++;
    }
    while(j<m2)
    {
        p2=p2->link;
        j++;
    }
    p2->link=p1;
    printf("\nThe bug is created\n");
}
void display()
{
    struct node*p;
    p=start;
    while(p!=NULL)
    {
        printf("%d\n",p->data);
        p=p->link;
    }
}
void debug()
{
    struct node *temp1,*temp2;
    int count1,count2=2;
    temp1=start;
    while(temp1!=NULL)
    {
        temp2=start;
        count1=0;
        while(temp2!=NULL)
        {
            count1=count1+1;
            if((temp1->link==temp2->link)&&(temp1->data!=temp2->data))
            {
                printf("\nThe bug is created at position %d and %d\n", count2,count1);
                temp2->link=NULL;
                exit;
            }
            else
            temp2=temp2->link;
        }
    temp1=temp1->link;
    count2=count2+1;
    }
}

解决方案

Your bug is in misunderstand the index. Indexes all start at "Null" or "zero".

so setting i and j to 1 is the start of your problems

void bug()
{
    int i=1,j=1,m1,m2;//flaw


I call the first element "head" (in imagine a snake). It is a full element.
AFTER working with the head you should use the link - NOT before.

Another bad bug is:

start=(struct node*)malloc(sizeof(struct node*));


only allocates a pointer to the struct and NOT a struct!!!


这篇关于如何开发我的代码以查找链接列表中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆