Pointer is not modifying in insert in C

若如初见. 提交于 2019-12-14 03:30:02

问题


Program for priority queue, I have doubt in pointer, I am passing head to insert but its not modifying in insert, as you can see I am printing the head

  • in insert and main
  • in insert it is printing something non zero
  • and in head it is zero
    #include<stdio.h>
    #include<stdlib.h>
    struct node{
        int data;
        int priority;
        struct node* next;
    };
    struct node* getnewnode(int data,int priority){
        struct node* newnode=malloc(sizeof(struct node));
        newnode->data=data;
        newnode->next=NULL;
        newnode->priority=priority;
        return newnode;
    }
    void insert(struct node* head,int data,int priority){

        struct node* newnode=getnewnode(data,priority);
        if(head==NULL){
            head=newnode;
            printf("head in insert is %d",head);
            return;
        }

        if(head->priority > newnode->priority){
            newnode->next=head;
            head=newnode;
            return;
        }
        if(head->priority <= newnode->priority ){
            struct node* temp=head;
            while(temp->priority <= newnode->priority ){
                temp=temp->next;
            }
            newnode->next=temp->next;
            temp->next=newnode;
            return;
        }
    }
    int removee(struct node* head){
        if(head==NULL)
            return -1;
        int temp=head->data;
        head=head->next;
        return temp;
    }
    int main(){
        struct node* head=NULL;
        insert(head,3,5);
        printf("\n head in main is %d",head);

    }

回答1:


head in insert is local to insert function, any modification to it will not effect the head in main and will be destroyed once control exits insert function.

Solution:

You need to pass the pointer of head in main to insert.

Hence your insert will become like below.

 void insert(struct node **head,int data,int priority){

    struct node* newnode=getnewnode(data,priority);
    if(*head==NULL){
        *head=newnode;
        printf("head in insert is %d",*head);
        return;
    }

    if((*head)->priority > newnode->priority){
        newnode->next=*head;
        *head=newnode;
        return;
    }
    if((*head)->priority <= newnode->priority ){
        struct node* temp=*head;
        while(temp->priority <= newnode->priority && temp->next != NULL){
            temp=temp->next;
        }
        newnode->next=temp->next;
        temp->next=newnode;
        return;
    }
}

and you call insert from main as below.

insert(&head,3,5);



回答2:


Super-simplified version without any if() statements:


void insert(struct node **head, int data, int priority){

    struct node *newnode = getnewnode(data, priority);

    for( ; *head && (*head)->priority <= newnode->priority; head = &(*head)->next)
        {;}

    newnode->next = *head;
    *head = newnode;

    return;

}

... if you don't like empty loops, you could add a if(...) break;:


void insert(struct node **head, int data, int priority){

    struct node *newnode = getnewnode(data, priority);

    for( ; *head ; head = &(*head)->next) {
        if( (*head)->priority > newnode->priority) break;
        }

    newnode->next = *head;
    *head = newnode;

    return;

}


来源:https://stackoverflow.com/questions/52625172/pointer-is-not-modifying-in-insert-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!