Creating push & pop functions... decrementing/tracking?

This is a discussion on Creating push & pop functions... decrementing/tracking? within the C Programming forums, part of the General Programming Boards category; I'm using a "stack" in a separate function (the reason I quote stack is that for my program, it's just ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Creating push & pop functions... decrementing/tracking?

    I'm using a "stack" in a separate function (the reason I quote stack is that for my program, it's just an array that I will treat like a stack)... How can I keep track of where I am in my "stack" ?

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Stack 'pointer'.
    That is what a microprocessor uses and they seem to manage OK

    Push = put value at pointer and increment
    pop = take value at pointer minus one and decrement
    or vice versa depending on which direction your stack goes.

    Code:
    // untested :)
    
    int stack[100];
    int sp=0; //stack pointer
    
    push(x){
    stack[sp]=x;
    sp++;
    
    }
    
    pop(x){
    x=stack[sp-1];
    sp--;
    
    }
    Last edited by esbo; 12-14-2008 at 06:46 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by esbo View Post
    Stack 'pointer'.
    That is what a microprocessor uses and they seem to manage OK

    Push = put value at pointer and increment
    pop = take value at pointer minus one and decrement
    or vice versa depending on which direction your stack goes.
    What am I decrementing exactly? and how?

    Pop can only return one value, which is the value on the top of the stack that I want to work with... How can it decrement something?

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Sparrowhawk View Post
    What am I decrementing exactly? and how?

    Pop can only return one value, which is the value on the top of the stack that I want to work with... How can it decrement something?
    It decrements the stack pointer.

    I have updated my earlier post.
    Looks quite nice, I may use it my self
    Last edited by esbo; 12-14-2008 at 06:47 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    hmm I need to do this without using a global variable

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Sparrowhawk View Post
    hmm I need to do this without using a global variable
    It's not a global variable; it belongs to your stack object, and is defined in your struct. (That is to say, your stack is more than just an array, right? Right?) If somehow you do just have an array, then every function will just take an extra argument that is your current stack index.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Sparrowhawk,

    I just finished up my first Comp Sci class where we covered different implementations of stacks using arrays as well as data structures. It's not really practical IMHO to do it with just an array, but see for yourself... Check out Lecture #22, "Stacks and Queues."

    http://www.cs.umbc.edu/courses/under...ll08/lectures/

    -Dave

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    It's not a global variable; it belongs to your stack object, and is defined in your struct. (That is to say, your stack is more than just an array, right? Right?) If somehow you do just have an array, then every function will just take an extra argument that is your current stack index.
    Yes it is just an array, no extra functions... Is having 2 args the only way? I seem to remember my prof saying pop took (1 arg, or none ?) while push took (1 or 2).

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Note the stack pointer always points to an empty place.
    If it was a stack of trays it would point to one above the stack of trays.
    Well it does in my example anyway, you could have it poiint to the top
    tray but then usually you start with no trays.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Quote Originally Posted by Sparrowhawk View Post
    Yes it is just an array, no extra functions... Is having 2 args the only way? I seem to remember my prof saying pop took (1 arg, or none ?) while push took (1 or 2).
    That's great because that's just what lecture #22 addresses. Check it out I'd like to know what you think..

    -Dave

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Sparrowhawk View Post
    hmm I need to do this without using a global variable

    Why?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by esbo View Post
    Why?
    Because the variable belongs to the stack, not to the universe?

    Edit: I seem to recall (in the spring time, I believe, but I'm not quite bored enough to do a search) someone here who had implemented a stack with a global variable just as you describe. Except he had two stacks. Everything seemed to work fine, until he actually tried to use that second stack....
    Last edited by tabstop; 12-14-2008 at 07:28 PM.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by mutandis View Post
    That's great because that's just what lecture #22 addresses. Check it out I'd like to know what you think..

    -Dave
    Ha, I actually found what I needed in there... It was a very simple solution to fix my pop too... Just make a temp value and copy the current value of top to it, use that to return then decrement top itself.

    That seems very obvious all of the sudden. Thanks.

    [edit] - The problem I was having was trying to figure out how to return the value while also changing the position without having to take two args.

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You could have a single function with a static variable and a direction indicator.

    Code:
    //untested
    Stackit (x, direction)
    {
      static int stack[100];
      static int sp=0; // can you initialie statics like this???
    
      if  (direction==PUSH){
        stack[sp]=x;
        sp++;
      }
    
      else {//POP
        x=stack[sp-1];
        sp--;
       }
    }
    Of course the function is going to be global so your someone can screw your stack up anyway.
    I would probably use the first method myself, it seems less messy.
    Last edited by esbo; 12-14-2008 at 07:40 PM.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i Thought push is assembly ?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 04:26 AM
  2. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 09:56 AM
  3. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 11:50 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Replies: 5
    Last Post: 09-17-2001, 06:18 AM

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