简体   繁体   中英

Deleting head on linked list

I'm new to c and trying to do my Homework. I need to create and insert data into linked list and then print it and delete one of the node inside the list.

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



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


    void printlist(node *head);
    void dele(int del,node * head);
    node * create(int n);



    int main(){
        int n=0,del;
        node * head=NULL;
        printf("Enter Number of Data: ");
        scanf("%d",&n);
        head=create(n);
        printf("Data in linked list are : ");
        printlist(head);
        printf("\nEnter Data yout want to delete : ");
        scanf("%d",&del);
        dele(del,head);
        printf("Data remained in linked list are : ");
        printlist(head);
        return 0;
    }

    node * create (int n){
        int i=0;
        node * head = NULL;
        node * temp = NULL;
        node * p = NULL;

        for(i=0;i<n;i++){
            temp = (node*)malloc(sizeof(node));
            printf("Enter Data%d : ",i+1);
            scanf("%d",&(temp->data));
            temp->next=NULL;

            if(head==NULL){
                head= temp;
            }
            else{
                p = head;
                while(p->next != NULL){
                    p=p->next;

                }
                p->next=temp;
            }
        }

        return head;
    }

    void printlist(node * head){
        node *n=head;
        while(n!=NULL){
        printf("%d ",n-> data);
        n=n->next;
        }
    }
    void dele(int del,node * head){
        int found = 0, position = 0, i;
      struct node *temp = head;
      struct node *prev = NULL;

      while(temp != NULL)
      {

        if(temp->data == del )
        {
            if(prev == NULL){ /*If the node is the head*/
                head = head->next;
                delete temp;
                return ;
            }else{
                prev->next = temp->next;
                delete temp;
                return ;
            }
        }
        prev = temp;
        temp = temp->next;
      }


    }

Everything is ok but there is a problem when I'm trying to delete the first node, the random number start showing up.

but when I try to delete other node it's show the result perfectly. Is there any solution to it?

For the deleting scenario, I am not familiar with delete tmp . But I'd suggest you freeing the memory by free(temp) after head point to the next. Also here:

head = head-> next

You must consider that the next may be NULL sometimes. Leading you to a segmentation fault .

==============================================

Edit:

I've analyzed and found the possible solution.

Consider changing: void dele(int del,node ** head); to void dele(int del,node ** head); , and call the function passing the memory address by using the address of operator & before head in the dele call, thus you will be deleting the node from the head itself, and not the reference only (that lead you to the 0): dele(del,&head); .

Thus you will be passing the address, of the head to the function, that will receive a pointer to a pointer. Remember to change the parameter in the function implementation itself also.

After that, the reference of the head to temp in your delete function must be passed as follow, so temp may point to the head pointer. Then, you can make the head point to the next element of temp, that stores the head pointer.

struct node *temp = *head;


if(prev == NULL){ /*If the node is the head*/
   *head = temp->next;
   free(temp);
   return ;
}

This may lead you to some understanding about pointer to point in C.

You must change the signature of dele this way:

void dele(int del, node ** head)

Then you must adjust the content accordingly, this lines are especially important for your issue:

if (prev == NULL){ /* If the node is the head */
    
    if (head) *head = (*head)->next;
}

Finally you call dele by passing a pointer to head :

dele(del, &head);

This way the new head will be visible outside the function (which it is not in your current codes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM