×

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

Red Black Tree vs AVL Tree: Data Structure

Difference Between Red Black Tree vs AVL Tree Red Black Tree: A red-black tree is referred as self-balancing binary search tree. In red-black, each node stores an extra bit that determines...

4 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

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...

3 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

What is B tree?

What do you mean by B Tree in Data Structures? In the technological world, a B tree is simply a well-managed and coordinated tree and an integral part of the data...

6 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 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.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

3 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.