Thread: Stack Program Here

  1. #1
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Stack Program Here

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    
    struct stack {
        int  sdata;
    	struct stack *next;
    } ;
    
    struct stack *Push (struct stack *llist, int data);
    struct stack Pop (struct stack ** llist);
    void ReleaseStack(struct stack * llist);
    
    int main () {
        struct  stack  *llist = NULL;
        int  data, choice;
    
        do 
    	{
    		printf("\nEnter choice (1: Push) (2: Pop) (3: end) : ");
    		scanf("%d",&choice);
    		while(getchar() != '\n') continue;
    
    		switch (choice) 
    		{
    			case 1: 
    				printf ("Integer to Push onto the stack: ");
    				scanf ("%d", &data);
    				//clear memory buffer
    				while(getchar() != '\n') continue;
    				llist = Push(llist,data);
    				break;
    			case 2: 
    				if (llist == NULL) 
    				{
    					printf ("stack empty!\n");
    				}else
    				{
    					struct stack temp = Pop(&llist);
    					printf("%d Popped.\n", temp.sdata);
    				}
    
    			case 3: 
    				break;
    
    		   default: 
    				printf ("Invalid menu choice - try again\n");
    				break;
    		}
    
        } while (choice != 3);
    
        ReleaseStack(llist);
    
    	return 0;
    }	
    
    
    
    struct stack * Push(struct stack * llist, int data) 
    {
    	struct stack *newp = NULL;
    
    	if(llist == NULL)
    	{
    		newp = (struct stack  *) malloc (sizeof (struct stack));
    		newp->next = NULL;
    		newp->sdata = data;
    		llist = newp;
    		return llist;
    	}else
    	{
    		newp = (struct stack  *) malloc (sizeof (struct stack));
    		newp->sdata = data;
    		newp->next = llist;
    		return newp;
    	}
    
    }
    
    struct stack Pop (struct stack ** llist) 
    {
    	//holds the return value (data)
    	struct stack temp;
    	//use to free the head node
    	struct stack *freep = *llist; 
    
    	//reposition the head of the stack for post Pop
    	if((*llist)->next == NULL) 
    	{
    		*llist = NULL;
    		temp = *freep;
    		free(freep);
    	}else
    	{
    		*llist = (*llist)->next;
    	
    		//get data value
    		temp = *freep;
    
    		//free head node
    		free(freep);
    	}
    
    	return temp;
    
     }
    
    void ReleaseStack(struct stack * llist) 
    {
    	while(llist != NULL)
    	{
    		printf("releasing %d\n", Pop(&llist));
    	}
    }
    Here is a stack program. It seems to be the topic of the day. This is the stack program that I wrote this morning.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Ohh goody - here's my attempt.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stack {
        int          data;
        struct stack *next;
    };
    
    // push to front
    // way simpler than push to rear
    void push ( struct stack **head, int val ) {
        struct stack *node = malloc( sizeof(struct stack) );
        node->data = val;
        node->next = *head;
        *head = node;
    }
    int pop ( struct stack **head ) {
        struct stack *node = *head;
        int result = node->data;
        *head = (*head)->next;
        free( node );
        return result;
    }
    
    int main ( ) {
        struct stack *s = NULL;     // its empty
        int i;
        for ( i = 0 ; i < 10 ; i++ ) {
            push( &s, i );
        }
        for ( i = 0 ; i < 10 ; i++ ) {
            printf( "%d\n", pop(&s) );
        }
        printf( "The stack is %sempty\n", s == NULL ? "" : "not " );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    If you push and it is empty than it crashes. I should have also used a pointer to a pointer to update main for my push function. I did that for my pop function. Why didn't I do it for the push? Also you add the node to the head of the stack, ofcourse, this is true for a stack.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > If you push and it is empty than it crashes.

    > *head = node;
    this updates the variable s in main, not what s points to.

    I put this line of code in the push loop, and it al looks good.
    printf( "%p %p\n", s, s->next );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Actually your code is very clear. I understand it perfectly. This is similar to my code, for the pop function. I was just saying that if you pop too many times than the program crashes. There is no error checking. I had a hard time this morning making mine bulletproof. I have it now but I could revise the push function so that it is automatically updated. I wasn't used to using pointers to pointers. When you pop the last node in the stack, I didn't know where to put the head pointer until I realized I should set it to NULL. I was also having difficulties freeing the poped node but I finally figured it out.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I was just saying that if you pop too many times than the program crashes.
    Well I was confused when you said push in the previous message.

    > There is no error checking.
    As they say in books "left as an easy exercise for the reader" .
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    For some reason this was not a simple exercise for me this morning. It took me a few hours to figure it out.

  8. #8
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    I'll post mine again. It does use pointers, but rather differently to what has been done here. It is using a simple contiguos stack.
    Ramble on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 10:52 AM
  2. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  3. linked list stack question
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2003, 05:05 AM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  5. Accessing data in the program stack
    By dajur in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 10:15 PM