0

This is push function.

void push(struct node **head)
{
    struct node *temp;
    temp = new node;
    cout<<"enter the value";
    cin>>temp->data;
    temp->link=NULL;
    if(*head==NULL)
    {   
        *head=new node;
        *head=temp;
    }
    else{
        temp->link=*head;
        *head=temp;
}
}    

this is how i am calling push.

struct node *start=NULL;
push(&start);

this is node

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

now the problem: i don't think the list is updating. The start always remains the null. Don't know why.

edit:

void display(struct node **head)
{
    struct node *temp;
    temp=*head;
    if(*head==NULL){
        cout<<"\nthe head is NULL\n";
    }
    while(temp!=NULL)
    {
        cout<<temp->data;
        temp=temp->link;
    }

}

int main() {
    struct node *start=NULL;
    push(&start);
    push(&start);
    push(&start);
    push(&start);
    push(&start);
    display(&start);
    return 0; 
}

input:

1

2

3

4

5

now display out should have been 5 4 3 2 1 but there is some mistake.

red5pider
  • 351
  • 1
  • 9
  • 24
  • 2
    If only there were a way to avoid that C abomination that is emulating pass-by-ref by using pointers. If only C++ had *true* references. That would be a feature worth considering, yes? :-) – paxdiablo Feb 06 '17 at 13:51
  • Not your problem probably, but `*head=new node;` is superfluous. – πάντα ῥεῖ Feb 06 '17 at 13:52
  • 2
    A [mcve] would increase the likelihood and quality of answers. – nwp Feb 06 '17 at 13:53
  • @πάντα ῥεῖ can you please explain why *head=new node; is superfluous? – red5pider Feb 06 '17 at 13:55
  • 1
    @SamarYadav Because you assign `temp` in the next line anyways. The allocated memory is leaked. – πάντα ῥεῖ Feb 06 '17 at 13:56
  • 1
    Could not reproduce: http://coliru.stacked-crooked.com/a/7663fe1a219ced89 – eerorika Feb 06 '17 at 13:58
  • @nwp please tell me where i am lacking in A Minimal, Complete, and Verifiable example? – red5pider Feb 06 '17 at 13:58
  • @SamarYadav See my link for a code that is closer to a mcve. Note the lack of user input that has no effect on outcome (more minimal than your code) and the fact that it compiles (includes and main) and could potentially therefore be verifiable. However, despite that, the example is not complete because it does not reproduce the problem that you describe. – eerorika Feb 06 '17 at 14:01
  • It does not compile. When I copy/paste your code to for example http://ideone.com/ it should show the exact issue you are having, instead I have to include some headers and order the snippets correctly and write a main function and people are lazy and move on instead. – nwp Feb 06 '17 at 14:01
  • @user2079303 thanks but the problem is if i want to push another node. it does not connect with the older node. can you please try adding 5 nodes and displaying them. – red5pider Feb 06 '17 at 14:02
  • @SamarYadav no. You do that, and show us how it goes wrong. – eerorika Feb 06 '17 at 14:02
  • okay wait a min – red5pider Feb 06 '17 at 14:03
  • @user2079303 i've added edit part just for the code you provided – red5pider Feb 06 '17 at 14:12
  • if the code you posted is literally the same that you use, then try a clean rebuild. The code as you posted it should not result in an empty list. – Sander De Dycker Feb 06 '17 at 14:37

2 Answers2

3

The answer is mentioned by paxdiablo in the comments: C++ has pass by reference. Example:

#include <iostream>

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

void push(node*& head)
{
    struct node *temp = new node;
    std::cout << "enter the value";
    std::cin >> temp->data;
    temp->link = head;
    head = temp;
}

int main()
{
    node *start = NULL;
    push(start);
    return 0;
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
0

Alternative implementation:

void push(struct node** head_reference, int new_data)
{
    struct node* a_node = new node;
    a_node->data  = new_data;
    a_node->link = (*head_reference);    
    (*head_reference)    = a_node;
}

int main() {
    struct node* head = NULL;
    push(&head, 10);
    // rest of your code here
    return 0;
}
Rishi
  • 1,387
  • 10
  • 14