Stack

Stack Concept

A stack is a useful data structure in programming. It is just like a pile of plates kept on top of each other.

a stack of plates is a good representation of stack data structure as you can only take out a plate from the top and put a plate on top of the other plates

Think about the things you can do with such a pile of plates

  • Put a new plate on top
  • Remove the top plate

If you want the plate at the bottom, you have to first remove all the plates on top. Such kind of arrangement is called Last In First Out - the last item that was placed is the first item to go out.

Stack in Programming Terms

In programming terms, putting an item on top of the stack is called "push" and removing an item is called "pop".

stack push pop fifo operations

In the above image, although item 2 was kept last, it was removed first - so it follows the Last In First Out(LIFO) principle.

We can implement stack in any programming language like C, C++, Java, Python or C#, but the specification is pretty much the same.

Stack Specification

A stack is an object or more specifically an abstract data structure(ADT) that allows the following operations:

  • Push: Add element to top of stack
  • Pop: Remove element from top of stack
  • IsEmpty: Check if stack is empty
  • IsFull: Check if stack is full
  • Peek: Get the value of the top element without removing it

How stack works

The operations work as follows:

  1. A pointer called TOP is used to keep track of the top element in the stack.
  2. When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1.
  3. On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
  4. On popping an element, we return the element pointed to by TOP and reduce its value.
  5. Before pushing, we check if stack is already full
  6. Before popping, we check if stack is already empty

stack operations

Stack Implementation in programming language

The most common stack implementation is using arrays, but it can also be implemented using lists.

Here is an implementation using arrays and C programming language.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define MAX 10

struct stack
{
    int items[MAX];
    int top;
};
typedef struct stack st;

void createEmptyStack(st *s)
{
    s->top=-1;
}

int isfull(st *s)
{
    if (s->top==MAX-1)
        return 1;
    else
        return 0;
}

int isempty(st *s)
{
    if (s->top==-1)
        return 1;
    else
        return 0;
}

void push(st *s)
{
    int newitem;
    printf("Enter item to be inserted: ");
    scanf("%d",&newitem);
    if (isfull(s))
    {
        printf("STACK FULL");
    }
    else
    {
        s->top++;
        s->items[s->top]=newitem;
    }
}


void pop (st *s)
{
    if (isempty(s))
    {
        printf("\n STACK EMPTY \n");
    }
    else
    {
        printf("Item popped= %d",s->items[s->top]);
        s->top--;
    }
}

void main()
{
    int ch;
    int loop;
    loop=1;
    st *s;

    createEmptyStack(s);

    do
    {
        printf("\n ***STACK OPERATIONS");
        printf("\n 1. PUSH");
        printf("\n 2. POP");
        printf("\n 3. EXIT");
        printf("\n ***************");
        printf("\n Enter your choice: ");
        scanf("%d", &ch);

        switch (ch)
        {
            case 1: 
                push(s);
                break;
            case 2:
                pop(s);
                break;
            case 3:
                printf("THANK YOU");
                loop=0;
                exit(0);
            default:
                printf("Invalid choice");
        }
    } while(loop);

    getch();
}

Use of stack

Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:

  • To reverse a word - Put all the letters in a stack and pop them out. Because of LIFO order of stack, you will get the letters in reverse order.
  • In compilers - Compilers use stack to calculate the value of expressions like 2+4/5*(7-9) by converting the expression to prefix or postfix form.
  • In browsers - The back button in a browser saves all the urls you have visited previously in a stack. Each time you visit a new page, it is added on top of the stack. When you press the back button, the current URL is removed from the stack and the previous url is accessed.