Stack Operations in Data Structure

Video Tutorial
FREE
Operations on Stack thumbnail
This video belongs to
Java DSA Course - Master the Fundamentals and Beyond
12 modules
Certificate
Topics Covered

Overview

A stack is an abstract data type (ADT) which is used to store data in a linear fashion. A stack only has a single end (which is stack's top) through which we can insert or delete data from it.

There are many different stack operations which can be applied on a stack. We will discuss most of the operations in this article.

Takeaways

Operations in stack:

push(), pop(), peek(), isEmpty(), isFull(), size()

Introduction to Stack

A stack is a data structure which is used to store data in a linear fashion. It is an Abstract data type (ADT) in which data is inserted and deleted from a single end which is the stack's top. It follows a Last in, First out (LIFO) principle i.e. the data which is inserted most recently in the stack will be deleted first from the stack. Given below is the pictorial representation of how a stack looks like.

Introduction to stack [IMAGE 1 FINISH SAMPLE]

We also have a separate article where the stack data structure is discussed in detail: Stacks in Data Structure.

Stack Representation

A stack follows a Last in, First out principle (LIFO). This means the data inserted in the stack last will be first to be removed. Also, the data is inserted or deleted through the stack top.

Given below is the stack representation to show how data is inserted and deleted in a stack.

Stack Representation [IMAGE 2 FINISH SAMPLE]

Stack Operations

There are various stack operations that are applicable on a stack. Stack operations are generally used to extract information and data from a stack data structure.

Some of the stack operations are given below.

1. push()

Push is a function in stack definition which is used to insert data at the stack's top.

Stack Operations [IMAGE 3 FINISH SAMPLE]

Given below is the syntax of the push function in the stack.

2. pop()

Pop is a function in the stack definition which is used to remove data from the stack's top.

pop function in stack [IMAGE 4 FINISH SAMPLE]

Given below is the syntax of the pop function in the stack.

3. topElement() / peek()

TopElement / Peek is a function in the stack which is used to extract the element present at the stack top.

peek function in stack [IMAGE 5 FINISH SAMPLE]

In C++, we use the top()function to extract the element present at the stack's top. Given below is the syntax of the top function in the stack.

4. isEmpty()

isEmpty is a boolean function in stack definition which is used to check whether the stack is empty or not. It returns true if the stack is empty. Otherwise, it returns false.

isempty function in stack [IMAGE 6 FINISH SAMPLE]

5. isFull()

isFull is a function which is used to check whether the stack has reached its maximum limit of insertion of data or not i.e. if 'maxLimit' is the maximum number of elements that can be stored in the stack and if there are exactly maxLimit number of elements present in the stack currently, then the function isFull() returns true. Otherwise, if the number of elements present in the stack currently are less than 'maxLimit', then isFull() returns false.

Given below is the syntax for the isFull function in the stack.

6. size()

Size is a function in stack definition which is used to find out the number of elements that are present inside the stack.

size function in stack [IMAGE 7 FINISH SAMPLE]

Given below is the syntax of the size function in stack.

Conclusion

  • A stack is a data structure which is used to store data in a linear fashion.
  • Stack follows a Last in, First out (LIFO) principle i.e. the data which is inserted most recently in the stack will be deleted first from the stack.
  • There are many stack operations that can be performed on a stack. Some of them are push(), pop(), peek(), isEmpty(), isFull(), size().

Become a master of data structures with our Data Structures in C++ Course. Start your journey today!