`stack

This is a discussion on `stack within the C Programming forums, part of the General Programming Boards category; Does anybody know a good tutorial on stack functions, or can anyone explain them and how they are used. Thanx ...

  1. #1
    jpre
    Guest

    `stack

    Does anybody know a good tutorial on stack functions, or can anyone explain them and how they are used.
    Thanx Jpre

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    A stack is just a data structure. Imagine a stack of papers. You can add another sheet to the top or you can take the top sheet off.....

    add a sheet........void push(sheet)
    take a sheet from top of stack..... sheet pop()

    sheet is used to denote any generic type.

    now there are two more functions needed for a stack normally...
    These are :-

    bool isempty()
    bool isfull()

    I think you can work out those on your own!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm... maybe I should write one to just let off some frustration... I'm not very happy with how my data structures class is teaching this...

    The best way to understand stacks, IMO, is to look at how it's handled by the processor. There are -two- commands...

    void push elem;
    void pop elem;

    here's some example code of how they work...

    Code:
    main ()
    {
     int a = 2, b = 3;
     // stack is empty.
     push a;  // stack is {2}
     push b; // stack is {2, 3}
     pop a;   // stack is {2}... a is now 3, and b is still 3.
     pop b;  // stack is {}... a is now 3, b is now 2.
     pop a; // if there was another value in stack, it would 
                // be stored in a, but there isn't so your program 
                // now has a deathwish.
     return; // Get out, before it blows!
    }
    The primary purpose of stacks is saving data when using recursion. Notice that the stack is not a queue... the first element in the stack is the last element out of the stack. Here's a more practical example of using a stack...
    Code:
    #include <stdio.h>
    main ()
    {
     int a = 3;
     push a; // Hmm.. why do this?
     scanf ("%d", &a); // Because I'm gonna destroy a!
     printf ("\nYou typed in %d...\n", a);
     pop a;  // And the original value of a returns!
     printf ("But that's a shoddy replacement for %d!\n", a);
     return;
    }
    Now, how do stacks work? The processor stack can only store one data type, so that's how our first stack will be implemented.

    Code:
    #define ALOC 1024
    #define push(A) stackpush(&A)
    #define pop(A) stackpop(&A)
    
    int stackmem[ALOC]; // The stack needs memory...
    int * sp = stackmem;  // And it needs this...
    
    void stackpush (int * a)
    {
     *sp++ = *a;
     return;
    }
    
    void stackpop (int * a)
    {
     *a = *--sp;
     return;
    }
    The functions are called stackpush and stackpop, and instead of accepting the variables, they accept pointers to the variables (otherwise the pop operation wouldn't work)... but then we have the macros push(a) and pop(a). No matter how the guts are built, the interface is always going to be push(a) and pop(a), although you could easily call stackpush(&a) or stackpop(&a)...

    Oh shucks, I've got a class soon. Well, I'm hoping that so far this has been instructive...

    If you include the third bit of code with either of the first two, then they should work, although it's a pretty limited stack so far. I'll get back to this later.

Popular pages Recent additions subscribe to a feed

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