Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
#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;
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 21-Sep-14 14:44pm    
The graphs having loops are not called "linked lists"; they are called "graphs". A linked list does not have it, by definition. Just a note...
Is it a homework?
—SA
PIEBALDconsult 21-Sep-14 14:54pm    
Well, a circular linked list would have one loop containing all the elements. :shrug:
Sergey Alexandrovich Kryukov 21-Sep-14 18:57pm    
Oh... well, such thing is also considered "linked list"; thank you for correcting me.
This is a trivial case though; and the same task on an arbitrary graph is quite non-trivial, especially if the performance is concerned.
—SA
PIEBALDconsult 21-Sep-14 15:11pm    
In the debug routine, I suspect you need another way to exit the inner while loop.

1 solution

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
C++
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!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900