Operations on stack in C

Stack:

A stack is a linear data structure, which uses the LIFO principle(last in first out). Here the element that is pushed last is the first one to be popped out.

Array representation of stack:

In memory, stacks can be represented as a linear array. Every stack has a TOP variable with it, which is used to store the address of the topmost element of the stack. It is at this position where the element will be added or deleted from. The variable MAX will be used to store the maximum number of elements that the stack can hold. If TOP=NULL, then the stack is empty, and if TOP= MAX-1, then the stack is full.

Operations on stack in C:

A stack supports three operations: PUSH, POP, PEEK, ISEMPTY, and ISFULL.

The PUSH operation inserts the element into the stack. The POP operation pops the element from the stack. The PEEK operation prints the topmost element in the stack.

PUSH operation:


The push operation is used to add an element to the stack. The new element is added at the topmost part of the stack. However, before inserting the value, check if TOP==MAX-1, as it would mean that stack is full. If an attempt is made to insert a value in a stack that is full it will show an OVERFLOW message is printed.

For example:

Consider the stack

PUSH operations on stack

To insert an element with value 6, We will first check if TOP==MAX-1. If the condition is false then we will increment the value of TOP and store the new element at the position given by stack[TOP]. Thus, the updated stack becomes as shown below:

push operation on stack

Algorithm for push operation:

STEP1: IF TOP= = MAX-1, then PRINT “OVERFLOW” Goto step 4.

STEP2: SET TOP= TOP+1.

STEP3: SET STACK[TOP]=VALUE.

STEP4: END.

POP operation:

It is used to pop the topmost element in the stack. If TOP==NULL, it would mean as the stack is empty and therefore there are no further deletions in the stack. If we delete a value from a stack that is already empty, an UNDERFLOW message is printed.

For example, consider the same stack to do pop operation:

To pop the topmost element in the stack we will check if TOP==NULL. If the condition is false then we will decrement the value of TOP. Thus the updated stack becomes,

Pop operation on stack

Algorithm for pop operation:

STEP1: IF TOP = =NULL, then PRINT “UNDERFLOW” Goto step 4.

STEP2: SET VAL =STACK[TOP].

STEP3: SET TOP= TOP-1.

STEP4: END.

PEEK operation:

It prints the topmost element of the stack without deleting it. The peek operation first checks if the stack is empty or contains elements in it. And it will return the topmost element.

Algorithm for Peek operation:

STEP1: IF TOP == NULL, then PRINT “STACK IS EMPTY” Goto step 3.

STEP2: RETURN STACK[TOP].

STEP3: END.

ISEMPTY operation:

It checks whether the stack is empty are not. Condition to check isempty is TOP==-1

ISFULL  operation:

It returns true if the stack is full otherwise returns false. Condition to check isfull is TOP==MAX.

Operations on stack in C program:

#include <stdio.h>

int MAX = 8;       
int stack[8];     
int top = -1;            

int isempty() {

   if(top == -1)
      return 1;
   else
      return 0;
}
   
int isfull() {

   if(top == MAX)
      return 1;
   else
      return 0;
}
//peek operation function
int peek() {
   return stack[top];
}
//pop operation function
int pop() {
   int data;
    
   if(!isempty()) {
      data = stack[top];
      top = top - 1;   
      return data;
   } else {
      printf("Could not remove data, Stack is empty.\n");
   }
}
//push operation function
int push(int data) {

   if(!isfull()) {
      top = top + 1;   
      stack[top] = data;
   } else {
      printf("Couldn't insert data, Stack is full.\n");
   }
}
//main program
int main() {
   //pushing elements into stack
   push(1);
   push(2);
   push(3);
   push(4);
   push(5);
   push(6);

   printf("Element at top of the stack: %d\n" ,peek());
   printf("Elements: \n");
   while(!isempty()) {
      int data = pop();
      printf("%d\n",data);
   }

   printf("Stack full: %s\n" , isfull()?"true":"false");
   printf("Stack empty: %s\n" , isempty()?"true":"false");
   
   return 0;
}

Output:

 

Also, read the circular linked list in C.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *