×

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

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Arrange consonants and vowels nodes in a linked list

Arrange consonants and vowels nodes in a linked list In this problem, we have given a singly linked list. Here we will arrange the consonants and vowels nodes of the list...

2 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Stack Using Linked List

In the linked list implementation of the stack, we use a linked list as the primitive data structure to create the stack. It is called the dynamic implementation of the...

6 minutes read.

What are the types of Trees in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

6 minutes read.

Bubble Sort vs Heap Sort

In this article, we are going to compare the two most common sorting techniques, Bubble Sort and Heap sort. Before discussing their differences, let us first discuss the idea of...

7 minutes read.

Finding the Maximum Element in a Binary Tree

Implementation // Creating a C++ program to excavate the minimum and maximum in a given binary tree. #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new tree node. class __nod { public: int record; __nod *Lft, *Rt; /*...

4 minutes read.

Binary Search Tree vs AVL Tree: Data Structure

Difference Between Binary Search Tree and AVL Tree Binary Search Tree: The binary search tree is a kind of binary tree data structure and it follows the conditions of binary...

3 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.

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.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Inorder Successor in Binary Trees

The next node in the Inorder traversal of a binary tree is known as Inorder successor of that particular node. In a Binary Search Tree, the definition of Inorder successor can...

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

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.