Home > Blockchain >  Why is this c linked list deletion at head program not printing the desired output?
Why is this c linked list deletion at head program not printing the desired output?

Time:10-16

// c program to delete first node..
// input size:3
// input elements:1,2,3
// desired output:2->3->NULL
// actual output:1->NULL

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

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

void create(struct node *p)
{
    int x;
    
    printf("Enter the element :");
    scanf("%d",&x);
    
    struct node *nn = malloc(sizeof(struct node));
    nn->data = x;
    
    if (p->next==NULL){
        nn->next = p->next;
        p->next = nn;
    }
    else {
        while(p->next!=NULL){
            p = p->next;
        }
        nn->next = p->next;
        p->next =nn;
    }
}

void FirstNodeDeletion(struct node *head)
{
    struct node *temp;
    
    if(head == NULL)
    {
        printf(" There are no node in the list.");
    }
    else {
        temp = head;
        head = head->next;
        printf("\n Data of node 1 which is being deleted is :  %d\n", temp->data);
        free(temp);  
    }
}

int search(struct node *pp,int data)
{
    struct node *p = pp;
    
    while(p->next!=NULL){
        if (p->data ==data){
            return 1;
        }
        p = p->next;
    }
    
    return 0;
}

void display(struct node *ptr)
{
    while (ptr->next!=NULL){
        ptr = ptr->next;
        printf("%d->",ptr->data);
    }
    
    printf("NULL");
}

int main() {
    
    struct node *head;
    
    head = malloc(sizeof(struct node));
    head->next=NULL;
    
    int n,i;
    
    printf("Enter the size :");
    scanf("%d",&n);
    
    for(i=0;i<n;i  ){
        create(head);
    }
    
    display(head);
    FirstNodeDeletion(head);
    display(head);
    return 0;
}

CodePudding user response:

FirstNodeDeletion(head); cannot change head because arguments are passed by value. You must rewrite FirstNodeDeletion to take a pointer to the head pointer and to use that pointer to get and to change head.

CodePudding user response:

For starters it is a bad approach when it is necessary to allocate the first node bypassing the corresponding function create. Moreover the function shall be able to deal with empty lists.

As for your problem then it can be resolved very simply by introducing one more structure as for example

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

struct list
{
    struct node *head;
};

In this case in main you could write

int main( void )
{
    struct list list = { .head = NULL };
    //...

and for example the function FirstNodeDeletion could look the following way

int FirstNodeDeletion( struct list *list )
{
    int present = list->head != NULL;

    if ( present )
    {
        struct node *current = list->head;
        list->head = list->head->next;
        free( current );
    }

    return present;
}

and the function is called like

FirstNodeDeletion( &list );

All other functions should be changed in a similar way.

  • Related