pop function

This is a discussion on pop function within the C Programming forums, part of the General Programming Boards category; prototype: Code: struct stack pop (struct stack ** tos); In main: Code: if (tos == NULL) { printf ("stack empty!\n"); ...

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

    pop function

    prototype:
    Code:
    struct stack pop (struct stack ** tos);
    In main:
    Code:
    				if (tos == NULL) 
    				{
    					printf ("stack empty!\n");
    				}else
    				{
    					struct stack temp = pop(&tos);
    					printf("%d popped.\n", temp.sdata);
    				}
    function definition:
    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 = (*tos)->next;
    	
    	//get data value
    	temp = **freep;
    	//free head node
    	free(*freep);
    
    	return temp;
    
     }
    Where did I go wrong with this? I can't quite figure it.

  2. #2
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    What I've done is quite a bit different to what you've done, but this is a stack program which does contain push and pop that I did ages ago.
    Attached Files Attached Files
    Ramble on...

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I would like to know how to do this function with a pointer to a pointer so that the stack is updated in main automatically. I already know how to do other stack function. I'm not asking how to write a stack, but instead how to use this method of updating the stack in main through a pointer to a pointer to the stack object. Thanks anyway.

  4. #4
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    Oic...well gimme a little bit and I'll see what I can come up with.
    Ramble on...

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    BTW here is the push function if needed:
    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;
    	}
    
    }
    The structure is:

    Code:
    struct stack
    {
       struct stack *next;
       int sdata;
    };
    But my problem is the pop function:

    And in main the definition:
    Code:
    int main()
    {
        struct stack *tos = NULL;
        ...
        return 0;
    }
    Last edited by Troll_King; 10-15-2001 at 03:09 AM.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I don't think I'm missing much, something small, but this moment I'm forced to study systems analysis because I have a test in 6 hrs. It would be great if someone can spot the error. I tried but could not figure it out.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    I have posted my code before,u should read it carefully.

    [1]->[2]->[3]->NULL

    what will happen after free([3])?
    [2]->next still point to [3] !!!!!

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    if((*tos)->next != NULL) *tos = (*tos)->next;

    I don't go there if it is NULL.
    Yet it's true that tos is than freed. Not sure what to do at that point. Maybe allocate a new node. hmm

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The only way your pop would make sense to me is if your stack was circular.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    hmm, but it make the same mistake.
    if the linked list has only one item.
    Like
    [1]->NULL

    >if((*tos)->next != NULL) *tos = (*tos)->next;
    >I don't go there if it is NULL.

    so after free(*freep), *tos point to a unavail item.

    BTW: temp=**freep; can't work
    replace it with memcpy(&temp,*freep,sizeof(struct stack));

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    temp=**freep;

    This did work. But I also have your code now. Yes, I am slightly stuck. I'll keep working on it. I might have to consult a text book or something. I want to use the pointer to pointer. That is my goal. To pass the structure like so:

    struct stack *tos;

    struck stack temp = pop(&tos);

    I can kind of see how your code works if the stack is circular but I don't quite get it.

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

    FINALLY!!!!!!!

    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;
    
     }
    I got it!

  13. #13
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    Sorry I didn't get back sooner well it looks like you've worked it out anyway. I did do something pretty similar (albeit with some minor differences in structure), but I won't bother posting the code now. Good job Troll_King.
    Ramble on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21