Javatpoint Logo
Javatpoint Logo

Representation of stack in data structure

An abstract data type (ADT) called a stack is used to store data linearly. The only end of a stack via which we may add or remove data is the top of the stack.

Abstract Data Type

An object's behavior may be described by a set of values and a set of actions, and this behavior is known as an abstract data type (ADT). The definition of ADT merely specifies the actions that must be taken, not how they must be carried out. It is unclear what algorithms will be utilized to carry out the operations and how the data will be structured in memory. Because it provides an implementation-independent view, it is dubbed "abstract."

Abstraction is the technique of presenting only the basics while obscuring the nuances.

Representation of stack in data structure

The user of a data type does not need to be aware of how that data type is implemented. For instance, we have been using primitive values such as int, float, and char data types only with the understanding that these data types can operate and be performed on without being aware of how they are implemented.

Therefore, a user just has to be aware of what a data type is capable of, not how it will be used. Consider ADT as a "black box" that conceals the internal organization and design of the data type. We will now define the three ADTs: List, Stack, and Queue.

Stack ADT

  • Instead of storing data in each node, the Stack ADT Implementation stores a reference to the data.
  • The address is provided to the stack ADT and the software allots memory for the data.
  • The ADT contains both the head node and the data nodes. The only thing the calling function can see is the stack pointer.
  • A reference to the top and a count of how many elements are presently on the stack are also included in the stack head structure.
  • Push an element to the top of the stack using the push() function.
  • pop() - If the stack is not empty, remove and return the element at the top.
  • If the stack is not empty, the peek() method returns the element at the top without deleting it.

LIFO (Last in First Out)

We will insert the data pieces into the data structure using the Last In First Out method, or LIFO. We will highlight the most current data items here. It implies that the last component will emerge first.

Example from real life:

Representation of stack in data structure

The following elements should be taken into account in this example:

  • A container with balls is present.
  • Balls of various kinds are placed in the bucket.
  • The ball that goes into the bucket last will be the first one removed.
  • The ball above it will be removed first, then the ball entering the bucket last (the newer one).
  • The ball that enters the bucket first will exit it last in this manner.
  • As a result, the Last ball (Blue) that entered the bucket is withdrawn first, followed by the First ball (Red).

Book Example:

The quantity of books stacked one on top of the other

  • The Tower of Hanoi is yet another excellent example from actual life. The last in, first out rule underlies the entirety of this intriguing game.
  • Stack is a data structure that utilizes the LIFO concept.
  • We will conduct the first push operation and the second pop operation on the stack data structure.
  • The data element that was most recently placed into the stack will be removed first, and vice versa, the data element that was pushed into the stack first will be removed last, until all of the items that were present before it have been removed.
  • The stack only has one end, or you might say that it is open-ended on one side.
  • The LIFO idea is the foundation of the majority of programs.
  • Stack is crucial in the evaluation of several expressions, including postfix to infix, infix to prefix, and prefix to postfix.
  • The implementation of recursion is its principal use. Recursion is utilized to more effectively tackle the issue.
  • Additionally, when running a program, the activation records are filled up in a heap using the LIFO technique.
  • The stack data structure is utilized to construct the Pushdown automata for determining whether or not the string is accepted from the machine.
  • In order to easily count the number of alphabets present in the given string and determine whether it belongs to the mentioned context-free grammar or not, it is primarily based on the LIFO principle, in which we will push the input string one at a time into the stack as needed and similarly pop out the same number of strings.

The stack-based data structure is based on the LIFO concept. Push and pop are the two major operations we use on it. Data items are pushed into the stack using the push operation and removed from the stack using the pop operation.

The top pointer in the stack data structure points to the value that is present at the top of the stack. The underflow situation is the state in which the stack initially links to the null pointer when it is empty. We have provided the stack's capacity; let's say n, in the stack. The data values must be added to the stack one at a time until the top of the stack is filled to its maximum size. Overflow refers to the situation when it exceeds the capacity.

Where LIFO is employed:

Data Structures: Some data structures, such as Stacks and various Stack variations, process data using the LIFO method.

Get the newest information: When data is taken from an array or data buffer, computers may employ LIFO. The LIFO technique is utilized when it is necessary to retrieve the most recent data entered.

Let us see a program in C to better understand the working of LIFO using struct:


10 pushed into stack
20 pushed into stack
30 pushed into stack
30 Popped from stack
Top element is : 20
Elements present in stack : 20 10
Process executed in 3.22 seconds
Press any key to continue.


In the above C program for LIFO representation, we can see first 10 is pushed and then 20 is pushed into the stack follow by 30 into the stack, then when performing pop function the element which was entered in the stack in the last is pop out first.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA