All about Stack

All about Stack

Have you ever perceived the number of plates piled in a restaurant ?...

Have you ever observed the way in which those piles are arranged?

If yes, it will easier for you to figure out what the stack is.

piled plates in restaurant.jpg

The definition of the stack goes like this:

A stack is a linear data structure in which the elements are inserted or deleted at only a single.

In the scenario of a pile of dinner plates in a restaurant if a person wants to keep a new plate he needs to keep it above the topmost plate or he wants to remove the plate he needs to remove the uppermost plate. The one which is at the bottommost position will stay for a longer time. Hence it is said that stack data structure works on the principle of Last In First Out(LIFO).

Let us look at the functionalities of the stack : Stack mainly has four operations. namely, push( to insert an element into stack ), pop( to delete an element into stack ), overflow ( to check whether the stack is full or not), underflow ( to check whether the stack is empty or not).

stack.jpeg

Note: The code is written in the C language.

To create the stack:

The creation of the stack can be done by using a one-dimensional array.

createStack(){
    #define SIZE 10
    int stack[SIZE];
    top=-1;
}

Here SIZE refers to the maximum number of elements the stack can hold. the stack is the array name, the top is a pointer variable that always points to the topmost element in the stack.

To push an element into the stack:

void push(){
    if(top==SIZE-1){
        printf("STACK OVERFLOW");
        return 0;

    }
    printf("\tEnter the element to be pushed:");
    scanf("%d",&item);
    top++;                      //top increments by one
    stack[top]=item;     //item assigned 
}

First, check whether a sufficient place is available or not. If available ask the user the element to be pushed then push that element inside the stack. Else it will print the message "STACK OVERFLOW".

To pop an element out of stack:

void pop(){
    if(top==-1){
         printf("\tSTACK UNDERFLOW\n");
         return 0; 
    } 
printf("\t %d is deleted from stack",stack[top]);
top--;                                                       //top decrements by one
printf("\tDeleted item =%d",stack[top]);
}

Before deleting an element it checks whether the element is present or not in the stack. If present it pops the topmost element else it prints the message "STACK UNDERFLOW".

To display the contents of the stack:

void display(){
     if(top==-1){
          printf("\tSTACK UNDERFLOW\n");
          return 0;
     }
     printf("\n\n\tContents of the stack are:\n");
     for (i=0; i<top; i++)
            printf("\t %d\n",stack[i]);
}

Before displaying the element it checks whether the element is present or not in the stack. If present it displays all the elements using a for loop else it prints the message "STACK UNDERFLOW".

Application of stack are:

  • Conversion and evaluation of an arithmetic expression
  • Recursion Handling
  • Parenthesis Handling
  • Backtracking

Click on the below link for complete code:

%github.com/Preksha-N/Data-Structures/blob/m..

THANK YOU, READERS:)

-Preksha