Hi Guys.I have some questions for this program.
This program uses linked lists to construct a stack (lifo)
I dont understand the bold part of the code:
In general i dont understand why
Pop() and Push() use a double pointer ....![]()
Code:* lifo-list.c */ #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; };Code:void Push(struct node **headRef, int newData) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = newData; newNode->next = (*headRef); (*headRef) = newNode; return; }Code:int Pop(struct node **headRef) { struct node *head; int result; head = *headRef; if(head != NULL) { result = head->data; *headRef = head->next; free(head); return(result); } else { printf("Pop() - Underflow !!\n"); return(0); } }Code:int Length(struct node *head) { int count = 0; struct node* current = head; while(current != NULL) { count++; current = current->next; } return(count); }Code:int main() { struct node *stack = NULL; int x, i, n; printf("\n Enter sequence of integers (EOF to stop): "); while(scanf("%d", &x) == 1) { printf(" %d values\n",&stack); Push(&stack, x); } n = Length(stack); printf("\t Stack contains %d values\n", n); printf("\n Stack values (in reverse order) :\n"); for(i = 0; i < n; i++) { printf("\t value #%d: [%d]\n", i, Pop(&stack)); } printf("\n Calling Pop() with empty stack: value=%d\n", Pop(&stack)); return(0); }



LinkBack URL
About LinkBacks



