问题描述:

I am trying to delete from a Singly Linked List, however, when I try to delete from the first element, it prints garbage. I think the problem comes from the delete_node function, however, I tried everything and I cannot figure it out.

 #include <stdio.h>//prinf

#include <stdlib.h>//alloc mallco callo

typedef struct node node;

struct node{

int number;

node *next;

};

node *new_node(int num){

node *n= (node*) malloc(sizeof(node));

n->number=num;

n->next=NULL;

return n;

}

void node_free_all(node *n){

if(n != NULL){

node_free_all(n->next);

free(n);

}

}

void print_nodes(node *n){

if(n != NULL){

print_nodes(n->next);

printf("Number is: %d\n",n->number);

}

}

void delete_node(node *n, int num){

node *rmNode= (node*)malloc(sizeof(node));

//delete first

if( n!= NULL && n->number==num){

rmNode = n;

n=n->next;

free(rmNode);

}

//all but first

while(n != NULL){

if(n->next != NULL && n->next->number == num){

rmNode= n->next;

n->next = rmNode->next;

free(rmNode);

break;

}

n=n->next;

}

}

int main(){

int i;

node *head= (node*) malloc(sizeof(node));

node *curr;

head=NULL;

for(i=1;i<=10;i++) {

curr = new_node(i);

curr->next =head;

head=curr;

}

printf("Everything:\n");

print_nodes(head);

printf("Deleting 1:\n");

delete_node(head,1);

print_nodes(head);

printf("Deleting 5:\n");

delete_node(head,5);

print_nodes(head);

printf("Deleting 2:\n");

delete_node(head,2);

print_nodes(head);

printf("Deleting 3:\n");

delete_node(head,3);

print_nodes(head);

printf("Deleting 10:\n");

delete_node(head,10);

print_nodes(head);

printf("Deleting 9:\n");

delete_node(head,9);

print_nodes(head);

node_free_all(head);

// node_free_all(list);

return 0;

}

What am I doing wrong?

网友答案:

You don't need to allocate memory to rmNode. Plus you need to pass the reference of head pointer to the function delete_node because every time you are updating the list and if you have to delete first element of list, then in this case head pointer also got updated.

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

void delete_node(struct node** head_ref, int num)
{
    struct node* temp;
    struct node* current = (*head_ref);


    //delete first

    if( current != NULL && current->number == num)
    {
        temp = current;
        current = current->next;
        free(temp);
        (*head_ref) = current;
    }
    else
    {
        //all but first
        while(current != NULL)
        {
            if(current->next != NULL && current->next->number == num)
            {
                temp = current->next;
                current->next = temp->next;
                free(temp);
                break;
            }
            current = current->next;
        }
    }
}
网友答案:

here's a version that removes all items that matches num, and always update the head item.

void delete_node(node** head_ref, int num)
{
    node* temp;
    node* last = 0;
    node* current = *head_ref;

    while(current)
    {       
        if( current->number == num )
        {
            temp = current;
            if( current == *head_ref )
                current = (*head_ref) = current->next;
            else
                current = last->next = current->next;
            free(temp);
        } else {
            last = current;
            current = current->next;
        }
    }
}

You have to call it like delete_node(&head,1);
because it needs an address to the head item to be able to change it

相关阅读:
Top