Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing

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); 
}



ADVERTISEMENT
ADVERTISEMENT