SlideShare a Scribd company logo
Stack Data Structure
Prepared by: Afaq Mansoor Khan
BSSE III- Group A
Session 2017-21
IMSciences, Peshawar.
Last Lecture Summary
• Introduction to Double Linked List
• Insertions and Deletions in Doubly Linked List
• Introduction to Circular Linked List
• Insertion and Deletion in Circular Linked List
Objectives Overview
• Introduction to Stack Data Structure
• Stack Operations
• Applications of Stack Data Structure in Computer
Science
What is a stack?
• It is an ordered group of homogeneous items of elements.
• Elements are added to and removed from the top of the
stack (the most recently added items are at the top of the
stack).
• The last element to be added is the first to be removed
(LIFO: Last In, First Out).
Stack Data Structure
Stack Specification
• Definitions: (provided by the user)
▫ MAX_ITEMS: Max number of items that might be on
the stack
▫ ItemType: Data type of the items on the stack
• Operations
▫ MakeEmpty
▫ Boolean IsEmpty
▫ Boolean IsFull
▫ Push (ItemType newItem)
▫ Pop (ItemType& item)
Push Operation
• Function: Adds newItem to the top of the stack.
• Preconditions: Stack has been initialized and is not
full.
• Post conditions: newItem is at the top of the stack.
void StackType::Push(ItemType newItem)
{
top++;
items[top] = newItem;
}
Algorithm for PUSH operation
1. Check if the stack is full or not.
2. If the stack is full, then print error of overflow and
exit the program.
3. If the stack is not full, then increment the top and
add the element.
Pop Operation
• Function: Removes topItem from stack and returns it in
item.
• Preconditions: Stack has been initialized and is not
empty.
• Post conditions: Top element has been removed from
stack and item is a copy of the removed element.
void StackType::Pop(ItemType item)
{
item = items[top];
top--;
}
Algorithm for POP operation
1. Check if the stack is empty or not.
2. If the stack is empty, then print error of underflow
and exit the program.
3. If the stack is not empty, then print the element at
the top and decrement the top.
Stack Data Structure
Stack Implementation
StackType::StackType()
{
top = -1;
}
void StackType::MakeEmpty()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
Stack Implementation
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}
void StackType::Push(ItemType newItem)
{
top++;
items[top] = newItem;
}
void StackType::Pop(ItemType& item)
{
item = items[top];
top--;
}
• The condition resulting from trying to push an
element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
Stack Overflow
• The condition resulting from trying to pop an
empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
Stack Underflow
Position of Top
Analysis of Stack Operations
• Push Operation : O(1)
• Pop Operation : O(1)
• Top Operation : O(1)
The time complexities for push() and pop() functions
are O(1) because we always have to insert or remove
the data from the top of the stack, which is a one step
process.
Applications of Stack Data Structure
Stack Applications
• Stacks are a very common data structure
▫ compilers
 parsing data between delimiters (brackets)
▫ operating systems
 program stack
▫ virtual machines
 manipulating numbers
 pop 2 numbers off stack, do work (such as add)
 push result back on stack and repeat
▫ artificial intelligence
 finding a path
Postfix expressions
• Postfix notation is another way of writing arithmetic
expressions.
• In postfix notation, the operator is written after the
two operands.
infix: 2+5 postfix: 2 5 +
• Expressions are evaluated from left to right.
• Precedence rules and parentheses are never
needed!!
Postfix expressions
Postfix expressions:
Algorithm using stacks
Postfix expressions:
Algorithm using stacks
WHILE more input items exist
Get an item
IF item is an operand
stack.Push(item)
ELSE
stack.Pop(operand2)
stack.Pop(operand1)
Compute result
stack.Push(result)
stack.Pop(result)
Reverse Polish Notation
• Way of inputting numbers to a calculator
▫ (5 + 3) * 6 becomes 5 3 + 6 *
▫ 5 + 3 * 6 becomes 5 3 6 * +
• We can use a stack to implement this
▫ consider 5 3 + 6 *
5
3
8
+
8
6
*6
48
– try doing 5 3 6 * +
Finding a Path
• Consider the following graph of flights
PR
X Q
W
Y
Z
S
T
Key
: city (represented as C)
: flight from city C1 to city C2
C1 C2
flight goes from W to S
W S
Example
Finding a Path
• If it exists, we can find a path from any city C1 to
another city C2 using a stack
▫ place the starting city on the bottom of the stack
 mark it as visited
 pick any arbitrary arrow out of the city
 city cannot be marked as visited
 place that city on the stack
 also mark it as visited
 if that’s the destination, we’re done
 otherwise, pick an arrow out of the city currently at
 next city must not have been visited before
 if there are no legitimate arrows out, pop it off the stack and go back to
the previous city
 repeat this process until the destination is found or all the cities have
been visited
Example
• Want to go from P to Y
▫ push P on the stack and mark it as visited
▫ pick R as the next city to visit (random select)
 push it on the stack and mark it as visited
▫ pick X as the next city to visit (only choice)
 push it on the stack and mark it as visited
▫ no available arrows out of X – pop it
▫ no more available arrows from R – pop it
▫ pick W as next city to visit (only choice left)
 push it on the stack and mark it as visited
▫ pick Y as next city to visit (random select)
 this is the destination – all done
Pseudo-Code for the Example
public boolean findPath(City origin, City destination) {
StackArray stack = new Stack(numCities);
clearAllCityMarks();
stack.push(origin);
origin.mark();
while(!stack.isEmpty()) {
City next = pickCity();
if(next == destination) { return true; }
if(next != null) { stack.push(next); }
else { stack.pop(); } // no valid arrows out of city
}
return false;
}
Summary
• Introduction to Stack Data Structure
• Stack Operations
• Analysis of Stack Operations
• Applications of Stack Data Structure in Computer
Science
References
• https://www.geeksforgeeks.org/stack-data-
structure/
• https://www.cse.unr.edu/~bebis/CS308/PowerPoint/
Stacks.ppt
• pages.cs.wisc.edu/~mattmcc/cs367/notes/Stacks.ppt
• https://www.studytonight.com/data-
structures/stack-data-structure
• https://www.tutorialspoint.com/data_structures_alg
orithms/stack_algorithm.htm

More Related Content

PPT
Stacks
PDF
PPTX
Linked List
PPT
Binary search tree(bst)
PPTX
stacks and queues
PPTX
My lectures circular queue
PPSX
PPT
Queue Data Structure
Stacks
Linked List
Binary search tree(bst)
stacks and queues
My lectures circular queue
Queue Data Structure

What's hot (20)

PPTX
Data structure by Digvijay
PPTX
Introduction to stack
PDF
Abstract Data Types
PPTX
PPSX
Stacks Implementation and Examples
PPSX
Data Structure (Stack)
PPT
List Data Structure
PDF
Set methods in python
PDF
Expression trees
PPTX
Priority Queue in Data Structure
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PPTX
Presentation on queue
PPT
Data Structure and Algorithms Binary Search Tree
PPT
GAC DS Priority Queue Presentation 2022.ppt
PPT
Linked list
PPTX
Priority queue in DSA
PPTX
Hashing Technique In Data Structures
PPTX
Doubly linked list (animated)
PPTX
Stacks in c++
PDF
Queue as data_structure
Data structure by Digvijay
Introduction to stack
Abstract Data Types
Stacks Implementation and Examples
Data Structure (Stack)
List Data Structure
Set methods in python
Expression trees
Priority Queue in Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
Presentation on queue
Data Structure and Algorithms Binary Search Tree
GAC DS Priority Queue Presentation 2022.ppt
Linked list
Priority queue in DSA
Hashing Technique In Data Structures
Doubly linked list (animated)
Stacks in c++
Queue as data_structure
Ad

Similar to Stack Data Structure (20)

PPTX
Stack,queue and linked list data structure.pptx
PPTX
DS-UNIT 3 FINAL.pptx
PPT
week 7,8,10,11 alll files included from .ppt
PPT
Stacks_As_Advanced_Data_Strcutures11.ppt
PPTX
Stack_Overview_Implementation_WithVode.pptx
PPT
PPTX
introduction of the Stacks and Queues.pptx
PPTX
Data Structure - Stack.pptx
PPTX
DSA_Unit3_ Stacks and Queues using array (1).pptx
PPTX
STACK1.pptx
PPTX
Unit 3 Stacks and Queues.pptx
PPT
Data Structures and algorithms using c .ppt
PPTX
Stack and Queue.pptx university exam preparation
PPTX
Unit 3 stack
PPTX
Data Structures Stack and Queue Data Structures
PPTX
Stack and its operations, Queue and its operations
PPTX
DS UNIT1_STACKS.pptx
PDF
Data structure lab manual
PPT
Stacks
Stack,queue and linked list data structure.pptx
DS-UNIT 3 FINAL.pptx
week 7,8,10,11 alll files included from .ppt
Stacks_As_Advanced_Data_Strcutures11.ppt
Stack_Overview_Implementation_WithVode.pptx
introduction of the Stacks and Queues.pptx
Data Structure - Stack.pptx
DSA_Unit3_ Stacks and Queues using array (1).pptx
STACK1.pptx
Unit 3 Stacks and Queues.pptx
Data Structures and algorithms using c .ppt
Stack and Queue.pptx university exam preparation
Unit 3 stack
Data Structures Stack and Queue Data Structures
Stack and its operations, Queue and its operations
DS UNIT1_STACKS.pptx
Data structure lab manual
Stacks
Ad

More from Afaq Mansoor Khan (20)

PPTX
Feature Selection - Natural Language Processing
PPTX
WiFi vs LiFi - A Comparison
PPTX
Role of Electronic Media in Pakistan
PPTX
Agile Testing - Approach and Strategies
PPTX
Ethical Hacking - An Overview
PPTX
Software Architecture Design Decisions
PPTX
How to Design an Algorithm
PPTX
Software Quality Qssurance, Scrum and Linkedin
PPTX
Quick sort
PPTX
.Physics presentation - Asteroids
PPTX
Graph Data Structure
PPTX
AVL Tree Data Structure
PPTX
Binary tree
PPTX
Queue Data Structure
PPTX
Prefix, Infix and Post-fix Notations
PPTX
Doubly & Circular Linked Lists
PPTX
Linked List - Insertion & Deletion
PPTX
Dynamic Memory & Linked Lists
PPTX
Sorting Algorithms
PPTX
Recursion and Sorting Algorithms
Feature Selection - Natural Language Processing
WiFi vs LiFi - A Comparison
Role of Electronic Media in Pakistan
Agile Testing - Approach and Strategies
Ethical Hacking - An Overview
Software Architecture Design Decisions
How to Design an Algorithm
Software Quality Qssurance, Scrum and Linkedin
Quick sort
.Physics presentation - Asteroids
Graph Data Structure
AVL Tree Data Structure
Binary tree
Queue Data Structure
Prefix, Infix and Post-fix Notations
Doubly & Circular Linked Lists
Linked List - Insertion & Deletion
Dynamic Memory & Linked Lists
Sorting Algorithms
Recursion and Sorting Algorithms

Recently uploaded (20)

PPTX
Save Business Costs with CRM Software for Insurance Agents
PDF
Build Multi-agent using Agent Development Kit
PPTX
Online Work Permit System for Fast Permit Processing
PPT
JAVA ppt tutorial basics to learn java programming
PDF
Comprehensive Salesforce Implementation Services.pdf
PPTX
How a Careem Clone App Allows You to Compete with Large Mobility Brands
PPTX
Introduction to Artificial Intelligence
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PDF
Forouzan Book Information Security Chaper - 1
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
top salesforce developer skills in 2025.pdf
PDF
Become an Agentblazer Champion Challenge
PDF
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Save Business Costs with CRM Software for Insurance Agents
Build Multi-agent using Agent Development Kit
Online Work Permit System for Fast Permit Processing
JAVA ppt tutorial basics to learn java programming
Comprehensive Salesforce Implementation Services.pdf
How a Careem Clone App Allows You to Compete with Large Mobility Brands
Introduction to Artificial Intelligence
AIRLINE PRICE API | FLIGHT API COST |
Materi-Enum-and-Record-Data-Type (1).pptx
Forouzan Book Information Security Chaper - 1
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
top salesforce developer skills in 2025.pdf
Become an Agentblazer Champion Challenge
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Upgrade and Innovation Strategies for SAP ERP Customers
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx

Stack Data Structure

  • 1. Stack Data Structure Prepared by: Afaq Mansoor Khan BSSE III- Group A Session 2017-21 IMSciences, Peshawar.
  • 2. Last Lecture Summary • Introduction to Double Linked List • Insertions and Deletions in Doubly Linked List • Introduction to Circular Linked List • Insertion and Deletion in Circular Linked List
  • 3. Objectives Overview • Introduction to Stack Data Structure • Stack Operations • Applications of Stack Data Structure in Computer Science
  • 4. What is a stack? • It is an ordered group of homogeneous items of elements. • Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). • The last element to be added is the first to be removed (LIFO: Last In, First Out).
  • 6. Stack Specification • Definitions: (provided by the user) ▫ MAX_ITEMS: Max number of items that might be on the stack ▫ ItemType: Data type of the items on the stack • Operations ▫ MakeEmpty ▫ Boolean IsEmpty ▫ Boolean IsFull ▫ Push (ItemType newItem) ▫ Pop (ItemType& item)
  • 7. Push Operation • Function: Adds newItem to the top of the stack. • Preconditions: Stack has been initialized and is not full. • Post conditions: newItem is at the top of the stack. void StackType::Push(ItemType newItem) { top++; items[top] = newItem; }
  • 8. Algorithm for PUSH operation 1. Check if the stack is full or not. 2. If the stack is full, then print error of overflow and exit the program. 3. If the stack is not full, then increment the top and add the element.
  • 9. Pop Operation • Function: Removes topItem from stack and returns it in item. • Preconditions: Stack has been initialized and is not empty. • Post conditions: Top element has been removed from stack and item is a copy of the removed element. void StackType::Pop(ItemType item) { item = items[top]; top--; }
  • 10. Algorithm for POP operation 1. Check if the stack is empty or not. 2. If the stack is empty, then print error of underflow and exit the program. 3. If the stack is not empty, then print the element at the top and decrement the top.
  • 12. Stack Implementation StackType::StackType() { top = -1; } void StackType::MakeEmpty() { top = -1; } bool StackType::IsEmpty() const { return (top == -1); }
  • 13. Stack Implementation bool StackType::IsFull() const { return (top == MAX_ITEMS-1); } void StackType::Push(ItemType newItem) { top++; items[top] = newItem; } void StackType::Pop(ItemType& item) { item = items[top]; top--; }
  • 14. • The condition resulting from trying to push an element onto a full stack. if(!stack.IsFull()) stack.Push(item); Stack Overflow
  • 15. • The condition resulting from trying to pop an empty stack. if(!stack.IsEmpty()) stack.Pop(item); Stack Underflow
  • 17. Analysis of Stack Operations • Push Operation : O(1) • Pop Operation : O(1) • Top Operation : O(1) The time complexities for push() and pop() functions are O(1) because we always have to insert or remove the data from the top of the stack, which is a one step process.
  • 18. Applications of Stack Data Structure
  • 19. Stack Applications • Stacks are a very common data structure ▫ compilers  parsing data between delimiters (brackets) ▫ operating systems  program stack ▫ virtual machines  manipulating numbers  pop 2 numbers off stack, do work (such as add)  push result back on stack and repeat ▫ artificial intelligence  finding a path
  • 20. Postfix expressions • Postfix notation is another way of writing arithmetic expressions. • In postfix notation, the operator is written after the two operands. infix: 2+5 postfix: 2 5 + • Expressions are evaluated from left to right. • Precedence rules and parentheses are never needed!!
  • 23. Postfix expressions: Algorithm using stacks WHILE more input items exist Get an item IF item is an operand stack.Push(item) ELSE stack.Pop(operand2) stack.Pop(operand1) Compute result stack.Push(result) stack.Pop(result)
  • 24. Reverse Polish Notation • Way of inputting numbers to a calculator ▫ (5 + 3) * 6 becomes 5 3 + 6 * ▫ 5 + 3 * 6 becomes 5 3 6 * + • We can use a stack to implement this ▫ consider 5 3 + 6 * 5 3 8 + 8 6 *6 48 – try doing 5 3 6 * +
  • 25. Finding a Path • Consider the following graph of flights PR X Q W Y Z S T Key : city (represented as C) : flight from city C1 to city C2 C1 C2 flight goes from W to S W S Example
  • 26. Finding a Path • If it exists, we can find a path from any city C1 to another city C2 using a stack ▫ place the starting city on the bottom of the stack  mark it as visited  pick any arbitrary arrow out of the city  city cannot be marked as visited  place that city on the stack  also mark it as visited  if that’s the destination, we’re done  otherwise, pick an arrow out of the city currently at  next city must not have been visited before  if there are no legitimate arrows out, pop it off the stack and go back to the previous city  repeat this process until the destination is found or all the cities have been visited
  • 27. Example • Want to go from P to Y ▫ push P on the stack and mark it as visited ▫ pick R as the next city to visit (random select)  push it on the stack and mark it as visited ▫ pick X as the next city to visit (only choice)  push it on the stack and mark it as visited ▫ no available arrows out of X – pop it ▫ no more available arrows from R – pop it ▫ pick W as next city to visit (only choice left)  push it on the stack and mark it as visited ▫ pick Y as next city to visit (random select)  this is the destination – all done
  • 28. Pseudo-Code for the Example public boolean findPath(City origin, City destination) { StackArray stack = new Stack(numCities); clearAllCityMarks(); stack.push(origin); origin.mark(); while(!stack.isEmpty()) { City next = pickCity(); if(next == destination) { return true; } if(next != null) { stack.push(next); } else { stack.pop(); } // no valid arrows out of city } return false; }
  • 29. Summary • Introduction to Stack Data Structure • Stack Operations • Analysis of Stack Operations • Applications of Stack Data Structure in Computer Science
  • 30. References • https://www.geeksforgeeks.org/stack-data- structure/ • https://www.cse.unr.edu/~bebis/CS308/PowerPoint/ Stacks.ppt • pages.cs.wisc.edu/~mattmcc/cs367/notes/Stacks.ppt • https://www.studytonight.com/data- structures/stack-data-structure • https://www.tutorialspoint.com/data_structures_alg orithms/stack_algorithm.htm