×

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the linked list. But dynamic memory allocation is not possible with the array.

Push operation

Push operation is used to insert a new element in the stack.

In case, the array is full, and no new item can be added in the array. This condition is also called an OVERFLOW (STACK_FULL).

Algorithm of the push operation

Step 1: Check whether the stack is full.
Step 2: If the stack is full, it will print the "overflow" message, and the program will terminate.
Step 3: If the stack is not full, the stack-array will increment [top + 1], and a new item will be added to the stack-array.  
// Assuming that array can hold maximum N element 
  Top = -1    
Read item          // item, which is to be pushed
if (Top == size - 1) then   // if top is at end of stack-array
{
print (“overflow”);
}
else
{
Top++;         // increment top to move to next empty position to hold new item Stack [Top] = element;
}
end     

Pop operation

Pop operation is used to delete an element in the stack.

In case, the last item is popped, the stack becomes empty. If one tries to delete an item from an empty stack, this condition is also called an UNDERFLOW (STACK_EMPTY).

Algorithm of the pop operation

Step 1: Check whether the stack is empty. 
Step 2: If the stack is empty, it will print the "underflow" message, and the program will terminate. Step 3: If the stack is not empty, the delete-item will be printed, and the top will contain a decrement [top - 1].  
// firstly, check for underflow condition if top == -1 then{print(“underflow”);    
      // exit the program 
}
else
{
element = stack[top]Top --;
}
end

Peek operation

When the data is received from a particular location in the stack, that operation is called peep operation.

Algorithm of peek operation

PEEK (STACK, TOP) Begin    
     if top = -1 then stack empty   
     item = stack[top]   
     return item   End      

Stack program in C language:

#include <stdio.h> 
#include <stdlib.h> 
#define MAX 10   
int count = 0;
 // Creating a stack   
struct stack 
{   
int items[MAX];  
 int top; };  
 typedef struct stack st; 
  void createEmptyStack(st *s)
 {  
 s->top = -1; 
}  
 // Check if the stack is full int isfull(st *s)
 {  
 if (s->top == MAX - 1) 
    return 1; 
  else   
  return 0;
 } 
  // Check if the stack is empty int isempty(st *s)
 { 
  if (s->top == -1) 
    return 1;
   else     return 0;
 } 
  // Add elements into stack void push(st *s, int newitem)
 {  
 if (isfull(s))
 {    
 printf("STACK FULL");
   } 
else {     s->top++;
     s->items[s->top] = newitem;
   }   
count++;
 }  
 // Remove element from stack void pop(st *s)
 { 
  if (isempty(s))
 {    
 printf("\n STACK EMPTY \n");
   }
 else 
{     
printf("Item popped= %d", s->items[s->top]);
     s->top--; 
  }  
 count--; 
  printf("\n"); 
} 
  // Print elements of stack void printStack(st *s)
 { 
  printf("Stack: ");
   for (int i = 0; i < count; i++)
 {  
   printf("%d ", s->items[i]);
   } 
  printf("\n"); 
}  
 // Driver code int main()
 { 
  int ch;  
 st *s = (st *)malloc(sizeof(st)); 
  createEmptyStack(s);  
 push(s, 1); 
  push(s, 3);
   push(s, 4); 
  printStack(s);
   pop(s);  
 printf("\nAfter popping out\n"); 
  printStack(s); 
}

Related Topics

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

3 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such...

4 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the...

6 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Left View of Binary Tree

Implementation // creating a C++ program to print the Left view of the binary tree. #include <bits/stdc++.h> using namespace std; struct Nod { int record; struct Nod *Lft, *Rt; }; // creating a utility function that will eventually help...

4 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.