EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Data Structures Tutorial Stack in Data Structure
Secondary Sidebar
Data Structures Tutorial
  • Basics
    • Linked List Advantages
    • What is Data Structure
    • Heap Data Structure
    • Types of Trees in Data Structure
    • AVL Tree in Data Structure
    • B Tree in Data Structure
    • B+ Tree in Data Structure
    • DFS Algorithm
    • BFS Algorithm
    • Arrays in Data Structure
    • Graph in Data Structure
    • Graph Representation
    • Breadth First Search
    • Depth Limited Search
    • Hashing in Data Structure
    • Searching in Data Structure
    • Linear Search in Data Structure
    • Linked List in Data Structure
    • Doubly linked list in Data Structure
    • Circular Linked List in Data Structure
    • Pointers in Data Structure
    • Types of Graph in Data Structure
    • Bubble Sort in Data Structure
    • Quick Sort in Data Structure
    • Bitonic Sort
    • Merge Sort in Data Structure
    • Selection Sort in Data Structure
    • Insertion Sort in Data Structure
    • Radix Sort in Data Structure
    • Stack in Data Structure
    • Queue in Data Structure
    • Priority Queue in Data Structure
    • Asymptotic Analysis
    • Tree Traversal in Data Structure
    • Tree Traversal Techniques
    • Trie Data Structure
    • Splay Tree in Data Structure
    • Spanning Tree Algorithm
    • Sparse Matrix in Data Structure
    • Radix Sort Algorithm
    • Counting Sort Algorithm
    • Skip List Data Structure
    • Linked List Algorithm
    • Linked List Types
    • Inorder Traversal of Binary Tree
    • Kruskals Algorithm
    • Prims Algorithm
    • BFS VS DFS
    • BCNF
    • Skip List
    • Hash Table?in Data Structure
    • Data Structure Interview Questions
    • Data Structures & Algorithms Interview
    • AVL Tree Deletion
    • B+ Tree Deletion
    • Decision Tree Advantages and Disadvantages
    • Data Architect Skills
    • Data Architecture Principles
    • Data Engineer Jobs
    • Data Engineer Roadmap
    • Fundamentals of Data Structure
    • Circular queue in Data Structure
    • Spanning Tree in Data Structure
    • Tree traversal types
    • Deque in Data structure
    • Shell Sort in Data Structure
    • Heap sort in data structure
    • Heap data structure C++
    • Heap data structure in Java
    • Binary Search Tree Types
    • Binary Tree in Data Structure
    • Binary Tree Types
    • Binary search tree in data structure
    • Binary Search Tree Advantages
    • Binary Search Tree Properties
    • Binary Search in Data Structure
    • Binary Tree Deletion
    • Sparse Matrix Multiplication
    • Preorder Traversal of Binary Tree
    • Postorder traversal
    • Decision Tree Hyperparameters
    • PostOrder Traversal without Recursion
    • AVL Tree Rotation
    • Avro File Format
    • Decision Tree Types
    • Binomial heap
    • Confluence Jira Integration
    • Timm Sort
    • Depth First Search

Related Courses

All in One Data Science Course

Oracle DBA Course

SQL Certification Course

Stack in Data Structure

By Priya PedamkarPriya Pedamkar

Stack in Data Structure

Introduction to Stack in Data Structure

A stack is a data structure that allows insertion and deletion operation in a LIFO (last-in-first-out) manner. The memory operations, therefore, are regulated in a particular manner. When an element is added to the stack, it occupies the top position. When it comes to removal operation, the most recent element in terms of being inserted into the stack gets first removed, hence the LIFO characteristic. This is similar to a stack of saucers or tiles, kept one over another. We keep going on placing saucers on over another, and while removing, the most recently added one is removed first.

Features of Stack

Some of the features are as follows:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)
  • A stack is an ordered list of elements of a similar type. E.g. stack storing numbers or stack of strings.
  • It is essentially based on LIFO (Last-In-First-Out) feature or FILO (First-In-Last-Out) feature.
  • When a stack is full, it is called to be in Overflow state, and when it is empty, it is called to be in Underflow state.

How does Stack work in Data Structure?

A stack is a very simple data structure, and there are necessarily two operations associated with stack, which are Push and Pop. The working of the stack as a data structure can be understood through these operations. Let’s understand each of them one by one, as described below. Let’s understand the insertion and deletion (removal) operations in a stack as discussed below.

1. Insertion Operation

The operation to insert an element in a data structure is called Push operation. When we insert an element into a stack, it occupies the bottom-most position, as an object pushed into a pit. The next element inserted goes over the top of the previous element, and likewise, the insertion of all elements happens. As each time, the insertion operation resembles pushing an element into the stack, and the operation is termed as “posh operation”. We will understand the working of push operations through the following steps.

Consider we have five numeric elements which are 51, 98, 24, 76, and 38. We intend to save these elements into memory using a stack data structure. The elements have to be inserted into a stack in the mentioned order. The push operation in this context will happen as discussed below.

1. In the beginning, the stack is empty, as shown below. Using push operation, we shall insert each element into this stack.
Stack in Data Structure 1

2. In the beginning, using push operation 51 is inserted into the stack, and it occupies the bottom-most position, as shown below.

Stack in Data Structure 2

3. The next element to be inserted into the stack is 98, which gets inserted using the push() operation, as shown below.

Stack in Data Structure 3

4. Similarly, the next two elements viz. 24 and 76 are inserted into the stack using the push() operation. This is as shown below.

Stack in Data Structure 4

5. Finally, we have 38 going into the stack through push operation. And when the last element is pushed into the stack, the stack gets full. This is as shown below.

Stack in Data Structure 5

The push operation in stack works, as mentioned in the above section. We will now see how the removal of an element from stack takes place, as described in the below section.

2. Deletion or Removal Operations

We will understand the deletion operation in the stack through the example discussed in the insertion operation section. Let’s start with the last step in the above section. Now, we know that the stack is full. We will remove every single element in the stack. The operation to remove an element from the stack is called “Pop” operation. The pop operation removes the most recently added element first. The removal operation in the stack, facilitated by pop operation, will work as described below.

1. Initially, the stack is full. This is as shown below.

Stack in Data Structure 1.1

2. The element that occupies the top position in the stack is 38. It is the most recently added element into the stack. The pop operation removes this element first. This is as shown below.

Stack in Data Structure 1.2

3. The next element to be removed from the stack is 76. The pop operation removes it from the stack. This is as shown below.

pop operation-1.3

4. Similarly, 76, and 24, in that order are removed from the stack facilitated by the pop operation, as shown below.

pop operation-1.4

5. Next, last-but-one element, i.e. 98, is removed from the stack through pop operations, as shown below.

last-but-one element- 1.5

6. So, one-by-one every element in the stack is removed through the pop operation. Lastly, we have only 51 left in the stack. As we know, 51 was the first element to enter, but when it came to removing, it turned out to be the last element to get removed from the stack, this is because, to have it removed, we first need to remove the element located above it. Hence, the stack follows the LIFO (Last-In-First-Out) feature for insertion and deletion operations. The last element gets removed, as shown below.

LIFO- 1.6

7. Finally, the stack becomes empty. This is as shown below.

Stack in Data Structure 1.7

3. Top and Search Operations

As the names suggest the Top operation in stack returns the topmost element in the stack. Similarly, the Search operation enables searching through the data structure.

Applications of Stack in Data Structure

  • The stack is the most convenient data structure to reverse a string. This is facilitated by the LIFO (Last-In-First-Out) nature of the stack.
  • The stack is a very effective data structure for parsing operations.
  • It is also used for expression conversion, i.e. converting infix expression to postfix expression, or postfix expression to prefix expression.
  • It proves very efficient when it comes to performing backtracking.

Conclusion

Stack though a simple data structure is a powerful tool to store and manage data in the required manner. It can be considered a path with the entrance, which facilitates both insertion and removal operation. Because of this nature of stack, the insertion and deletion operations are LIFO based.

Recommended Articles

This is a guide to Stack in Data Structure. Here we discuss features and working of Stack in Data Structure along with its applications. You can also go through our other related articles to learn more –

  1. Attributes of App Stack
  2. Arrays in Data Structure
  3. Bubble Sort in Data Structure
  4. Pointers in Data Structure
Popular Course in this category
All in One Data Science Bundle (360+ Courses, 50+ projects)
  360+ Online Courses |  1500+ Hours |  Verifiable Certificates |  Lifetime Access
4.7
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
SQL Training Program (7 Courses, 8+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more