×

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end is removed first, and the element that is added first is removed at the end. There is only one end to insert and remove the element in the stack called the top end.

Stack

Example:

A good real-life example of a stack is the pile of dinner plates. When you have to take one plate from a pile, you pick the topmost plate and then the next plate. The last plate is picked at the end, which is placed first in a pile. The last plate is known as the base plate in the stack.

Stack

Stack operations

There are four types of operations in the stack.

  1. Push
  2. Pop
  3. Peek
  4. Update

Push: Push operation is used to insert a new element in the stack. Push operation is shown in the figure below.

Stack

Pop: Pop operation is used to delete an element in the stack. Pop operation is shown in the figure below.

Stack

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

Update: When the value of an element is changed in the stack, that operation is called update operation.

Overflow & Underflow Conditions

If the stack is empty and trying to delete an element from it, that situation is called underflow. If the stack is full and trying to add a new element to it, that situation is called overflow.

Applications of stack

  1. Expression evaluation: Stack is used to evaluating the prefix, postfix, and infix expressions.
Infix notationPrefix notationPostfix notation
A + B+ A BA B +
(A - C) * B*- A C BA C – B *
A + (B * C)+ A * BCA B C * +
(A + B) / (C + D)/ + A B – C DA B + C D - /
(A + (B * D)) / (C - (D * B))/ + A * B C – C * D BA B C * + C D B * - /
  • Expression conversion: An expression is represented in the prefix, postfix, or infix notation. The stack is used to convert the form of one expression to another form.
  • Syntax parsing: Many compilers use the stack to parsing the syntax expressions.
  • Memory management: It is also used in memory management.
  • Variable tracking: Stack is also used to track local variables in runtime.
  • Parenthesis checking: Stack is also used to check whether parenthesis is correctly open and closed or not.
  • String reverse: Stack is also used to reverse the string. In the string, we push the characters into the stack one by one and then pop the characters from the stack one by one.
  • Undo: Stack is also used to undo in text-editor.

Implementation 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

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

Why is Binary Heap Preferred over BST for Priority Queue

A priority queue is a linear and ordered collection of elements in which each element has an attribute named priority and the priority attribute decides the order in which elements...

2 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

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

B+ Tree Program in Q language

A B+ tree is just an improvised version of a self-balancing and well-maintained tree in which all the key values that hold valuable information is present at the bottom, which...

9 minutes read.

Perfect Binary Tree

Complete binary trees are an important and general topic in the concept – of tree data structures. Before discussing a complete binary tree, we need to know the concept of...

4 minutes read.

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation #include <stdio.h> #include <stdlib.h>   /* Creating a binary tree node that consists of some data along with the pointer to the left and right child.  */ struct __nod {     int record;     struct...

4 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Reverse the Singly Linked List in C

Reverse the Singly Linked List in C This article has given a singly linked list and will reverse the linked list by changing the links between nodes. Example:                         Input:  2 -> 4...

3 minutes read.

Linked List Data Structure

Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each...

3 minutes read.

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

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

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.