Thursday, 15 May 2014

c++ - Pop and push functions at stack Linked list -



c++ - Pop and push functions at stack Linked list -

hello have created 2 functions pop , push. problem programme crashes after pop function , can not figure why. happy if can help me.

struct node { int num; node *next; }; void push(node** top, int num) { node* tmp = *top; node* newnode = new node; newnode->num = num; *top = newnode; newnode->next = tmp;

}

int pop(node* top) { node *poppedlink = top; top = poppedlink->next; int ans = poppedlink->num; delete poppedlink; homecoming ans;

}

you delete node outer code doesn't aware of , maintain using old pointer "top" (which dead already). have utilize double-pointer input param happens in push().

something (sorry, didn't test on compiler):

int pop(node** top) { node *poppedlink = *top; *top = poppedlink->next; int ans = poppedlink->num; delete poppedlink; homecoming ans; }

c++ pointers linked-list stack

No comments:

Post a Comment