Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

Deleting first node without head pointer in C

I am trying to make linked list code that makes deletion from first node.I created a struct a

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

Then added some nodes, without using head pointer, here is my main

int main(){
struct node *first = malloc(sizeof(struct node));
struct node *second = malloc(sizeof(struct node));
struct node *third = malloc(sizeof(struct node));
struct node *forth = malloc(sizeof(struct node));

first->data = 1; //assing data in first node
first -> next = second; // link first node with second node

second -> data = 2;
second -> next = third;

third -> data = 3;
third -> next = forth;

forth -> data = 4;
forth->next = NULL;

printList(first);

// deletefromBeg(first);

    //printList(first);

}

Here my printList code for printing the list

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

But this deletion code doesn't work. I just want to delete my first node and list starts second.

void deletefromBeg(struct node *first){
struct node *temp;
temp = first;
first = first->next;
printf("%d",temp->data);
free(temp);
}
question from:https://stackoverflow.com/questions/65918944/deleting-first-node-without-head-pointer-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function gets the pointer first by value. It means that the function deals with a copy of the value of the pointer first

void deletefromBeg(struct node *first){
struct node *temp;
temp = first;
first = first->next;
printf("%d",temp->data);
free(temp);
}

Changing a copy of the value does not influence on the original value stored in the pointer first.

The pointer first defined in main and the parameter first of the function are two-different objects. Within the function you are changing the parameter first that was initialized by a copy of the value of the pointer first defined in main.

You have two approaches.

Either you have to return the new value of the pointer obtained within the function and assign it to the pointer first in main like

struct node * deletefromBeg( struct node *first )
{
    if ( first != NULL )
    {
        struct node *temp = first;
        first = first->next;
        free( temp );
    }

    return first;
}

And in main you call the function like

first = deletefromBeg( first );

Or you should pass the pointer first by reference. In C passing an object by reference means passing the object indirectly through a pointer to the object.

void deletefromBeg( struct node **first )
{
    if ( *first != NULL )
    {
        struct node *temp = *first;
        *first = ( *first )->next;
        free( temp );
    }
}

And in main the function is called like

deletefromBeg( &first );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...