Thread: question on linked lists(stack with linked lists)

  1. #1
    Registered User
    Join Date
    Apr 2004

    question on linked lists(stack with linked lists)

    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 ....
    * lifo-list.c */
    #include <stdio.h>
    #include <stdlib.h>
    struct node {
    	int data;
    	struct node *next;
    void Push(struct node **headRef, int newData) 
    	struct node *newNode = (struct node*)malloc(sizeof(struct node)); 
       newNode->data = newData; 
    	newNode->next = (*headRef);  
       (*headRef) = newNode; 
    int Pop(struct node **headRef) 
    	struct node *head; 
    	int result;
       head = *headRef;
    	if(head != NULL) {
    		result = head->data; 
    		*headRef = head->next;  
    	} else {
    		printf("Pop() - Underflow !!\n");
    int Length(struct node *head) 
    	int count = 0;
    	struct node* current = head;
       while(current != NULL) {
    		current = current->next;
    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));

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    They're being passed a pointer to the list itself, so they can update the head of the list. Remember, any time you want to change something in a function, you need a pointer to it. Thus, if you want to change the pointer that stored your list, you need a pointer to that list so it can be changed inside the function call itself. (You can of course do it this way...)
    Node *popanddestroy( Node *list )
        Node *n;
        n = list->next; /* point to the top->next node so we can keep track of it */
        free( list ); /* free the top node */
        return n; /* return the new "top" */
    Node *list = constructabiglistfunction( );
    list = popanddestroy( list ); /* get rid of the top one, assign the list the next one */
    This would inside the function, strip the top node of the list. It then returns the "next" one, which is assigned via a return value to the "list" pointer.

    Or, by using a pointer to a pointer, you do it in one shot:
    Node *list = makeabiglist( );
    pop( &list ); /* strips the top one off, does something to it, updates the list itself */
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding comparing two linked lists
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 04:28 AM
  2. Linked Lists Question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 01:33 AM
  3. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 12:03 PM
  4. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM