0% found this document useful (0 votes)
1 views8 pages

Stack

A stack is a linear data structure that operates on the LIFO (Last In First Out) principle, where the last element added is the first to be removed. There are two types of stacks: fixed size and dynamic size, with operations such as push, pop, top, isEmpty, and isFull, all having a time complexity of O(1). Stacks are widely used in applications like function calls, recursion, expression evaluation, and memory management, but they have limitations such as limited access and potential overflow.

Uploaded by

jananippriya18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

Stack

A stack is a linear data structure that operates on the LIFO (Last In First Out) principle, where the last element added is the first to be removed. There are two types of stacks: fixed size and dynamic size, with operations such as push, pop, top, isEmpty, and isFull, all having a time complexity of O(1). Stacks are widely used in applications like function calls, recursion, expression evaluation, and memory management, but they have limitations such as limited access and potential overflow.

Uploaded by

jananippriya18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Stack is a linear data structure that follows LIFO (Last In First Out) Principle,

the last element inserted is the first to be popped out. It means both insertion and
deletion operations happen at one end only.

LIFO(Last In First Out) Principle


Here are some real world examples of LIFO
 Consider a stack of plates. When we add a plate, we add at the top. When we
remove, we remove from the top.
 A shuttlecock box (or any other box that is closed from one end) is another
great real-world example of the LIFO (Last In, First Out) principle where
do insertions and removals from the same end.
Representation of Stack Data Structure:
Stack follows LIFO (Last In First Out) Principle so the element which is pushed
last is popped first.

Types of Stack:
 Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size
and cannot grow or shrink dynamically. If the stack is full and an attempt is
made to add an element to it, an overflow error occurs. If the stack is empty
and an attempt is made to remove an element from it, an underflow error
occurs.
 Dynamic Size Stack : A dynamic size stack can grow or shrink dynamically.
When the stack is full, it automatically increases its size to accommodate the
new element, and when the stack is empty, it decreases its size. This type of
stack is implemented using a linked list, as it allows for easy resizing of the
stack.
Implementation of Stack in Data Structures
You can perform the implementation of stacks in data structures using two data
structures that are an array and a linked list.
 Array: In array implementation, the stack is formed using an array. All the
operations are performed using arrays. You will see how all operations can
be implemented on the stack in data structures using an array data structure.

 Linked-List: Every new element is inserted as a top element in the linked list
implementation of stacks in data structures. That means every newly inserted
element is pointed to the top. Whenever you want to remove an element
from the stack, remove the node indicated by the top, by moving the top to
its previous node in the list.
Basic Operations on Stack:
In order to make manipulations in a stack, there are certain operations provided to
us.
 push() to insert an element into the stack
 pop() to remove an element from the stack
 top() Returns the top element of the stack.
 isEmpty() returns true if stack is empty else false.
 isFull() returns true if the stack is full else false.
Push Operation on Stack
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
Algorithm for Push Operation:
 Before pushing the element to the stack, we check if the stack is full .
 If the stack is full (top == capacity-1) , then Stack Overflows and we
cannot insert the element to the stack.
 Otherwise, we increment the value of top by 1 (top = top + 1) and the new
value is inserted at top position .
 The elements can be pushed into the stack till we reach the capacity of the
stack.
Pop Operation in Stack
Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Algorithm for Pop Operation:
 Before popping the element from the stack, we check if the stack is empty .
 If the stack is empty (top == -1), then Stack Underflows and we cannot
remove any element from the stack.
 Otherwise, we store the value at top, decrement the value of top by 1 (top =
top - 1) and return the stored top value.

Top or Peek Operation on Stack


Returns the top element of the stack.
Algorithm for Top Operation:
 Before returning the top element from the stack, we check if the stack is
empty.
 If the stack is empty (top == -1), we simply print "Stack is empty".
 Otherwise, we return the element stored at index = top .

isEmpty Operation in Stack Data Structure:


Returns true if the stack is empty, else false.
Algorithm for isEmpty Operation:
 Check for the value of top in stack.
 If (top == -1), then the stack is empty so return true .
 Otherwise, the stack is not empty so return false .

isFull Operation in Stack Data Structure:


Returns true if the stack is full, else false.
Algorithm for isFull Operation:
 Check for the value of top in stack.
 If (top == capacity-1), then the stack is full so return true.
 Otherwise, the stack is not full so return false.

Complexity Analysis of Operations on Stack Data Structure:

Operations Time Complexity Space Complexity

push() O(1) O(1)

pop() O(1) O(1)

top()
O(1) O(1)
or peek()

isEmpty() O(1) O(1)

isFull() O(1) O(1)


Applications of Stacks:
 Function calls: Stacks are used to keep track of the return addresses of
function calls, allowing the program to return to the correct location after a
function has finished executing.
 Recursion: Stacks are used to store the local variables and return addresses
of recursive function calls, allowing the program to keep track of the current
state of the recursion.
 Expression evaluation: Stacks are used to evaluate expressions in postfix
notation (Reverse Polish Notation).
 Syntax parsing: Stacks are used to check the validity of syntax in
programming languages and other formal languages.
 Memory management: Stacks are used to allocate and manage memory in
some operating systems and programming languages.
 Used to solve popular problems like Next Greater, Previous Greater, Next
Smaller, Previous Smaller, Largest Area in a Histogram and Stock Span
Problems.
Advantages of Stacks:
 Simplicity: Stacks are a simple and easy-to-understand data structure,
making them suitable for a wide range of applications.
 Efficiency: Push and pop operations on a stack can be performed in constant
time (O(1)), providing efficient access to data.
 Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that
the last element added to the stack is the first one removed. This behavior is
useful in many scenarios, such as function calls and expression evaluation.
 Limited memory usage: Stacks only need to store the elements that have
been pushed onto them, making them memory-efficient compared to other
data structures.
Disadvantages of Stacks:
 Limited access: Elements in a stack can only be accessed from the top,
making it difficult to retrieve or modify elements in the middle of the stack.
 Potential for overflow: If more elements are pushed onto a stack than it can
hold, an overflow error will occur, resulting in a loss of data.
 Not suitable for random access: Stacks do not allow for random access to
elements, making them unsuitable for applications where elements need to
be accessed in a specific order.
 Limited capacity: Stacks have a fixed capacity, which can be a limitation if
the number of elements that need to be stored is unknown or highly variable.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy