Support Forums

Full Version: [C][example] Stacks.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Stacks are a certain way to handle an array.
The best way to explain a stack is comparing it to a CD holder.
Basically, you can either take one off the top or add one to the top, but you can not access the ones near the bottom without taking other ones off at first.
[Image: tower_of_ten_with_base.jpg]
I commented about every line of the code, so it should be pretty much straight forward.
The compiler ought to give you about 10 warnings upon compilation due to the way I declared my array (no fixed size), but it works correctly, not to say flawlessly.

Code:
//Stack
#include <stdio.h>
//Some global variables. Note that these are declared outside any function and outside main().
//This means they are accessible for all functions alike.
//Since I don't know the size of the wished stack array at the beginning, I'll just declare stack with a pointer.
//I stands for the position we're currently at in the stack and imax for the maximum size of the stack.
int i, imax, * stack;
//Get the last element from the stack and print it out.
get()
{
    //If the stack is not empty.
    if(i>0)
    {
    //Move back one element.
    i--;
    //Print the element.
    printf("\n%d",stack[i]);
    }
}
//Read an element into the stack from the keyboard.
put()
{
    //If the stack is not full.
    if(i<imax)
    {
    //Read an element in via the keyboard.
    scanf ("%d",&stack[i]);
    //Change the current location in the stack.
    i++;
    }
}
//Put an element into the stack directly.
puti(int a)
{
    //If the stack is not full.
    if(i<imax)
    {
    //Put the element into the stack.
    stack[i]=a;
    //Change the current location in the stack.
    i++;
    }
}
//My lovely main function.
int main ()
{
    //Another variable. Note that this one is declared inside main() and is thus only accessible inside main().
    int x;
    do
    {
        //Prompt the user for the stack size.
        printf ("Enter the required size of the stack: ");
        scanf ("%d",&imax);
        //In case of 0 or a negative number, prompt with an error and ask again.
        if(imax<1)
        printf("You failed, retry. The size of the stack has to be a positive number.\n");
    }
    //Until the user understands. :P
    while(imax<1);
    //Allocate enough memory for our stack.
    stack=(int*)calloc(i,sizeof(int));
    //Errorcheck.
    if(stack==NULL)return 1;
    //Ask for input.
    do
    {
        printf("Add a number to the stack (%d/%d): ",i+1,imax);
        put();
    }
    //...Leaving the last element free for this particular example.
    while(i<imax-1);
    //Prompting that the program is adding one number directly.
    printf("And the program adds one number from itself (%d/%d).\n",i+1,imax);
    //The program adds the number '15' to the stack.
    puti(15);
    //Prompting.
    printf("And now to print the stack out:");
    //Print the numbers using the get() function.
    do
    {
        get();
    }
    while(i>0);
    //Free the memory allocated by the array.
    free (stack);
    //Wait for the user to hit return and feel good.
    getchar();
    getchar();
    return 0;
}
Wow, nice tutorial (would you call it that?), this has helped me understand the stack now (couldn't really understand it, previously...). Good job.
thanku



great