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 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 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

What's hot

Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Stacks
StacksStacks
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
 
Stack
StackStack
Queue
QueueQueue
Queue
Raj Sarode
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
LavanyaJ28
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
Tech_MX
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 

What's hot (20)

Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stacks
StacksStacks
Stacks
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack
StackStack
Stack
 
Queue
QueueQueue
Queue
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Linked List
Linked ListLinked List
Linked List
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Linked list
Linked listLinked list
Linked list
 
Stack project
Stack projectStack project
Stack project
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Similar to Stack Data Structure

DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Stacks
StacksStacks
Stacks
StacksStacks
Data Structure - Stack.pptx
Data Structure - Stack.pptxData Structure - Stack.pptx
Data Structure - Stack.pptx
MarlonMagtibay2
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
MouDhara1
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
VeerannaKotagi1
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
SupriyaGhosh43
 
Stacks
StacksStacks
Stacks
FarithaRiyaz
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
Waf1231
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
Prof. Dr. K. Adisesha
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
Halid Assen
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Data structures
Data structuresData structures
Data structures
Jauhar Amir
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
maamir farooq
 

Similar to Stack Data Structure (20)

DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stacks
StacksStacks
Stacks
 
Stacks
StacksStacks
Stacks
 
Data Structure - Stack.pptx
Data Structure - Stack.pptxData Structure - Stack.pptx
Data Structure - Stack.pptx
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks
StacksStacks
Stacks
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Data structures
Data structuresData structures
Data structures
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 

More from Afaq Mansoor Khan

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
Afaq Mansoor Khan
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
Afaq Mansoor Khan
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
Afaq Mansoor Khan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
Afaq Mansoor Khan
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
Afaq Mansoor Khan
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
Afaq Mansoor Khan
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
Afaq Mansoor Khan
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
Afaq Mansoor Khan
 
Quick sort
Quick sortQuick sort
Quick sort
Afaq Mansoor Khan
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
Afaq Mansoor Khan
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
Afaq Mansoor Khan
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Afaq Mansoor Khan
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 

More from Afaq Mansoor Khan (20)

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
 
Quick sort
Quick sortQuick sort
Quick sort
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 

Recently uploaded

Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 

Recently uploaded (20)

Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 

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).
  • 5.
  • 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.
  • 11.
  • 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