# Introduction to Stacks in Data Structures

## Stack in Data Structures

Stack is one of the basic linear Data structure, that we use for storing our data. Data in a stack is stored in a serialized manner. One important thing about using a Stack is that the data first entered in the stack will be at the last of the stack. This is one of the reason why we also called Stack a LIFO Data Structure, i.e; Last in First Out. We’ll be discussing more about this feature later in this article.

 Best Time Complexity O(1) Average Time Complexity O(1) Worst Time Complexity O(n) Space Complexity O(n) Average Comparisons (n+1)/2

## Basic Terminology of Stack

Before we move on further with understanding the Stack Data Structure, we need to learn about the basic terminology that is associated with this data structure so that understanding stack will be a little easy for us.

The basic terminology that we will be using in Stack is :

• Top – This refers to the topmost element of the stack, or in other words the element last entered in the stack.
• Push () – This is one of the operation that we can perform on stack, for inserting data in this data structure.
• Pop () – This operation deals with deleting the data from the stack. It deletes the top-most data from the stack.
• Peek () – This operation helps us in looking at the topmost element of the data without removing it from the stack.

A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space for push operation, the stack is then considered to be in an overflow state.

Below is simple representation of a stack with push and pop operations – ## Advantages of Using Stack over other data structures

• Manages the data in a Last In First Out(LIFO) method which is not possible with Linked list and array.
• When a function is called the local variables are stored in a stack, and it is automatically destroyed once returned.
• A stack is used when a variable is not used outside that function.
• It allows you to control how memory is allocated and deallocated.
• Stack automatically cleans up the object.
• Not easily corrupted
• Variables cannot be resized.

## Disadvantages of Using Stack over other data structures

• Stack memory is very limited.
• Creating too many objects on the stack can increase the risk of stack overflow.
• Random access is not possible.
• Variable storage will be overwritten, which sometimes leads to undefined behavior of the function or program.
• The stack will fall outside of the memory area, which might lead to an abnormal termination.

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

### Stacks

• Introduction to Stack in Data Structure
• Operations on a Stack
• Stack: Infix, Prefix and Postfix conversions
• Stack Representation in –
C | C++ | Java
• Representation of a Stack as an Array. –
C | C++ | Java
• Representation of a Stack as a Linked List. –
C | C++ | Java
• Infix to Postfix Conversion –
C | C++ | Java
• Infix to prefix conversion in –
C | C++ | Java
• Postfix to Prefix Conversion in –
C | C++ | Java

### Queues

• Queues in Data Structures (Introduction)
• Queues Program in C and implementation
• Implementation of Queues using Arrays | C Program
• Types of Queues in Data Structure
• Application of Queue Data Structure
• Insertion in Queues Program (Enqueuing) –
C | C++ | Java
• Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java
• Reverse a Queue –
C | C++ | Java
• Queues using Linked Lists –
C | C++ | Java
• Implement Queue using Stack –
C | C++ | Java
• Implement Queue using two Stacks –
C | C++ | Java

### Priority Queue

• Application of Priority Queue
• Priority Queue Example
• Priority Queue Introduction –
C | C++ | Java
• Priority Queue Implementation using Array –
C | C++ | Java
• Priority Queue using Linked List –
C | C++ | Java
• Priority Queue Insertion and Deletion-
C | C++ | Java

### Priority Queue

• Application of Priority Queue
• Priority Queue Example
• Priority Queue Introduction – C | C++ | Java
• Priority Queue Implementation using Array – C | C++ | Java
• Priority Queue using Linked List – C | C++ | Java
• Priority Queue Insertion and Deletion- C | C++ | Java