More Stacks

This is a discussion on More Stacks within the C Programming forums, part of the General Programming Boards category; I have one question about stack size Code: int StackSize(Stack *s) { int count=0; if(Push(item, *s)) count++; else if (Pop(*item,*s)) ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    More Stacks

    I have one question about stack size
    Code:
    int StackSize(Stack *s)
    {
      
      int count=0;
      if(Push(item, *s))
    	  count++;
      else if (Pop(*item,*s))
    	  count--;
      return count;
    }
    i have other errors in my code, so i dont know if this is legal, the prototype for push is
    int Push(StackE item, Stack *s)
    int Pop(StackE *item, Stack *s)
    or should do something with my top var to get the size?

    also for my Push

    Code:
    int Push(StackEntry item, Stack *s) {
    
      Node *nptr = MakeNode(item);
      {
    	*nptr = s->top;
    	if(StackFull(s)) return 1;
    	else
    	{
    		*item = nptr->entry;
    		s->top++ = nptr->next;
    		return 0;
    	}
    	
      }

    Thanks for the help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, for a 'pop' statement, you should not pass it any arguments, unless you have multiple stacks, and you're passing it the stack to pop from. Example:
    Code:
    Data pop( Stack **s )
    {
        Stack *temp;
        Data d;
    
        temp = *s->next;
        d = *s->data;
        free( *s );
        *s = temp;
    
        return d;
    }
    What we do is this:
    1) pass a pointer to the stack so we can update it
    2) make a temp variable hold the next item on the stack
    3) copy by value the data from the top of the stack
    4) free the top item on the stack
    5) update the stack pointer to point to the new "top"
    6) return by value the data that was on the top


    Now, this is much simpler if you just have a single global stack:
    Code:
    Data pop( void )
    {
        Stack *s;
        Data d;
    
        s = ourStack;
        ourStack = ourStack->next;
        d = s->data;
        free( s );
        return d;
    }
    Basicly it's the same, but you don't have to screw with pointers to pointers. Note: I left out error checking, you can do that on your own.

    Push works basicly the same way. You can either push to a global stack, by just passing the data, or you can pass both the stack to push to, and the data to push.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but you don't have to screw with pointers to pointers
    But what fun is that?

    I go play in the C++ boards and you take over back here, I suppose I should keep a better watch

    -Prelude
    My best code is written with the delete key.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't worry about it. You're not missing much. 90% of the posts are from lavon, who is just posting the same thing over and over anyway... Heh.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I noticed, we've both told him to remove the &'s from printf and he still hasn't done it yet it appears :P

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 05:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM

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