Thread: stack problem

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Question stack problem

    hi friends

    i am stuck in a stack program in which pop function is not ok.

    its removing element first time only, then showing error.

    dont know why free is not working

    i am attaching my program. any help would be appreciated.

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Give me a few minutes and I'll post an answer if this stinking website hold up.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    ur pop function should be:

    struct stack *pop (struct stack * tos) {
    struct stack *temp=tos;
    struct stack *pre=NULL;

    while (tos->next != NULL) {
    pre=tos;
    tos = tos -> next;
    }
    printf ("element removed is %d\n",tos -> sdata);
    free(tos);

    if(pre==NULL)temp=NULL;
    else pre->next=NULL;

    return temp;
    }

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    This is a stack, that's not a pop function. It's part of a push function. I'm stuck on one thing. Need some more time to figure it out.
    Last edited by Troll_King; 10-15-2001 at 12:31 AM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    It is a stack.
    It is a linked list (but not a queue) stack.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    So why did you traverse the list to pop an item?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    while (tos->next != NULL) {
    pre=tos;
    tos = tos -> next;
    }

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay, bad mornig. You add nodes to the top, yeah that's right, but I don't understand why you traversed the list.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    struct stack * push(struct stack * tos, int data) {

    struct stack *ptr = tos;
    if (tos != NULL) {
    while (tos -> next != NULL)
    tos = tos -> next;
    /* ~~~~~~~~~~~~~~~~~~~*/
    tos -> next = (struct stack *) malloc (sizeof (struct stack));
    tos = tos -> next;
    tos -> next = NULL;
    tos -> sdata = data;
    return ptr;
    }
    else {
    tos = (struct stack *) malloc (sizeof (struct stack));
    tos -> next = NULL;
    tos -> sdata = data;
    return tos;
    }
    }

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I may just be having a bad morning. Sorry if I didn't read the code correctly. I guess I just need more time this morning.

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    struct stack * push(struct stack * tos, int data) 
    {
    	struct stack *newp = NULL;
    
    	if(tos == NULL)
    	{
    		newp = (struct stack  *) malloc (sizeof (struct stack));
    		newp->next = NULL;
    		newp->sdata = data;
    		tos = newp;
    		return tos;
    	}else
    	{
    		newp = (struct stack  *) malloc (sizeof (struct stack));
    		newp->sdata = data;
    		newp->next = tos;
    		return newp;
    	}
    
    }
    Okay, yes this is the right push. I confused myself. Actually I'm still a little lost with your pop function. I'm looking into it.

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    pop function called as:
    Code:
    				if (tos == NULL) 
    				{
    					printf ("stack empty!\n");
    				}else
    				{
    					struct stack temp = pop(&tos);
    					printf("%d popped.\n", temp.sdata);
    				}
    				break;
    Defined like:
    Code:
    struct stack pop (struct stack ** tos) 
    {
    	//holds the return value (data)
    	struct stack temp;
    	//use to free the head node
    	struct stack *freep = *tos; 
    
    	//reposition the head of the stack for post pop
    	if((*tos)->next == NULL) 
    	{
    		*tos = NULL;
    		temp = *freep;
    		free(freep);
    	}else
    	{
    		*tos = (*tos)->next;
    	
    		//get data value
    		temp = *freep;
    		//memcpy(&temp,*freep,sizeof(struct stack));
    		//free head node
    		free(freep);
    	}
    
    	return temp;
    
     }
    Took a long time to figure out.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    got an errorfree stack

    finally i could get an errorfree program with ur efforts.

    thnax guys ,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Problem?
    By audinue in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2009, 11:28 AM
  2. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Having problem deleting node in middle of stack
    By sballew in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 11:00 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM