Home > front end >  Can someone help me in debugging this code on linked list
Can someone help me in debugging this code on linked list

Time:08-05

So, I tried to write a code for handling all basic operations of linked list. But some functions are not working properly and are exiting the code without showing any error. I have tried to do create, display, search, insert at end, insert at index, delete at data, delete at node. //create #include <stdio.h> #include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

void Display(struct node *header)
{
    struct node *p;
    p = header;
    int i = 1;
    while (p->next != NULL)
    {
        printf("Data in Node %d is: %d\n", i  , p->data);
        p = p->next;
    }
}

void search(struct node *ptr, int q,int p)
{
    int i = 1;
    while (ptr->data != q)
    {
        ptr = ptr->next;
        i  ;
        if(i==p)
        {
            printf("Invalid");
            break;
        }
    }
    printf("Node %d is %d\n", i, ptr->data);
}

struct node *insert_end(struct node *head, int p,int q)  //error
{
    int i=1;
    struct node *last;
    last->next = NULL;
    last->data = p;
    while(i<q)
    {
        head=head->next;
    }
    head->next = last;
    return last;
    
}

void insert_index(struct node *head, int p,int q, int a)  //error
{
    int i=1;
    struct node *new = (struct node *)malloc(sizeof(struct node));
    if(q>a)
    printf("Invalid");
    else{
    while(i<q-1)
    {
        head=head->next;
        i  ;
        printf("!");
    }
    new->data=p;
    new->next=head->next;
    head->next=new;}
    
}

void delete_data(struct node *head,int x)  //error
{
    struct node *ty;
    while(head->data!=x)
    {
        head=head->next;
    }
    ty=head->next;
    head->next=ty->next;
    
}

void delete_node (struct node *head, int index)
{
    int i = 1;
    while (i <= index - 2)
    {
        head = head->next;
        i  ;
    }
    struct node *ptr = head->next;
    head->next = ptr->next;
    free(ptr);
}

    int main()
{
    int n,t=1;
    struct node *ptr=(struct node *)malloc(sizeof(struct node));
    struct node *new1 = (struct node *)malloc(sizeof(struct node));
    struct node *start=ptr;
    printf("Enter Data[Input -1 to stop]:");
    while(n!=-1)
    {        
        scanf("%d",&n);
        ptr->data=n;
        if(n!=-1)
        ptr=ptr->next;
        t  ;
    }
    ptr->next=NULL;
    int z,r,s;
    pq:
    printf("Enter 1 to Display Elements:\n");
    printf("Enter 2 to Search for a Data:\n");
    printf("Enter 3 to Insert Data at the End:\n");
    printf("Enter 4 to Insert Data at any Index:\n");
    printf("Enter 5 to Delete a data:\n");
    printf("Enter 6 to Delete a node:\n");
    printf("Enter -1 to Exit:");
    scanf("%d",&z);

    switch(z)
    {
        case 1: Display(start); break;  //display
        case 2:
            Display(start);
            printf("Enter data to Search:"); // search for a data
            scanf("%d", &r);
            search(start, r,t);
            goto pq;
        case 3:
            Display(start);
            printf("Enter data:"); // insert at end
            scanf("%d", &r);
            new1=insert_end(start, r,t);
            Display(start);
            goto pq;
        case 4:
            Display(start);
            printf("Enter data:"); // insert with index
            scanf("%d", &r);
            printf("Enter Index:");
            scanf("%d", &s);
            insert_index(start, r, s, t);
            Display(start);
            goto pq;
        case 5:
            Display(start);  //deleting with given data
            printf("Enter data to delete:");
            scanf("%d",&r);
            delete_data(start,r);
            Display(start);
            goto pq;
        case 6: Display(start);  //deleting a node
            printf("Enter Index:");
            scanf("%d", &s);
            delete_node (start, s);
            Display(start);
            goto pq;
        case -1: printf("Exiting");
        default:printf("Invalid");
                break;
    }
        
    return 0;
}

Functions insert_end, insert_index and delete_data are not working.

CodePudding user response:

on insert_index you are setting the new node to set it's next field to the head node, and then also telling the head node to set it's next field to the new node. effectively pointing to each other.

On delete, you are changing the literal value of the head pointer. See below for another way. index will work the same as insert_end if you specify an index thats larger than the total size.

void insert_index(struct node *head, int data, int index)
{
    int i = 1;
    struct node *new = (struct node *)malloc(sizeof(struct node));
    struct node *temp = head;
    while(temp->next)
    {
        if (i == index)
        {
            break;
        }
        i  ;
        temp = temp->next;
    }
    new->data = p;
    new->next = NULL;
    if (i == index)
    {
        new->next = temp->next->next;
    }
    temp->next=new;
}

void delete_data(struct node *head, int value)
{
    struct node *temp = head;
    while(temp)
    {
        if(temp->data == value)
        {
            temp->data = 0;
        }
        temp=temp->next;
    }
    
}

CodePudding user response:

insert_end function does not malloc for last:

struct node *insert_end(struct node *head, int p,int q)  //error
{
    int i=1;
    struct node *last;
    last->next = NULL;
    last->data = p;

CodePudding user response:

Before inserting a node, you need to initialize the node, including allocating memory for the node pointer and initializing the members of the node.

struct node *insert_end(struct node *head, int p,int q)
{
    int i=1;
    struct node *last;
    last = malloc (sizeof(struct node));
    last->next = NULL;
    last->data = p;
}

The p and q parameters of insert_index are not initialized, and you need to explicitly locate the insertion position according to q. The function delete_data needs to determine whether the head is NULL before comparing the data, and the node memory needs to be released after deleting the node.

  • Related