# Stack in Data Structure

Stack is a well-known data structure, and you'll find out all about it here. Let's get right down to the introductory section then.

## Introduction to Stack Data Structure

To organize information in a sequential order, one can use a data structure called a stack. Data are stacked as shown in the diagram below.

Color plates are arranged in a stack in the figure above. Let me explain why these color plates are organized in a stack: in a linear data structure, if elements can only be inserted or deleted from one side (the top), this is referred to as the stack. Consider the following scenario: all plates are stacked on top of each other in a canteen, and we can either add a new plate to the stack or take one from the top. That is, we can only take a plate from the top and place it on the stack at the top.

## Stack adheres to the LIFO principle

The data structure "stack" adheres to the Last-In-First-Out (LIFO) principle, which states that the elements of the stack inserted last are the first to come out. Consider the following real-world example: if we add a new plate to the stack of plates, anyone who wants to take one plate from the stack will get the plate that was most recently placed on top of the stack, indicating that the stack adheres to the LIFO principle.

## Stack Operations

The stack data structure performs the following two operations:

• "Push operation": adding an element to the stack.
• "Pop operation": the removal of a stack element.

In other words, insertion refers to a "push" operation in the stack, while deletion refers to a "pop" operation.

Unlike linked lists, we only need to keep track of the last element in a stack data structure. That is, we must use a pointer called "top" to keep track of the address of the last or top element. It is because the operations "push" and "pop" in the stack data structure are both performed from the top of the stack.

Allow me to create and include a photo representing the insertion and deletion operations in stack for your convenience and understanding of the push and pop operations in stack. As a result, I created the following pictorial representation of stack operations:

The above pictorial representation of stack operations shows that the first stack has four elements, and when we push a new element, say "E," into it, it is added to the top of the stack. As a result, the second stack is the stack that results from pushing "E" into the first stack. Because popping always happens at the top of the stack, popping an element from the stack pops the top element, which in the above stack is "E"; thus, after popping "E" from the stack, we get the third stack, as shown in the above snapshot.

After reading the above description of stack operations, push and pop, I don't think you'll have any doubts that both are always performed to the top of the stack.

## In programming, how does the Stack data structure work?

Because I already stated that we only store the address of the stack's top element using a pointer called "Top," stack operations involving the "Top" pointer work as follows:

• The "Top" pointer is used to keep track of the stack's top element.
• Don't forget to set "-1" to the "Top" at the start of the program so that you can compare "Top == -1" to see if the stack is empty.
• When you add an element to the stack, the "Top" value rises, and when you remove an element from the stack, it falls. That is, increase the value of "top" after pushing and decrease it after popping.
• Before pushing an element into a stack, always check to see if it is full.
• Similarly, before popping an element from the stack, check to see if it is already empty.

Data Structure Quiz

« Previous Tutorial Next Tutorial »

Liked this post? Share it!