SlideShare a Scribd company logo
ADVANCED DATA
STRUCTURES
STACKS
CRISTIAN REY C. SECO, MIT
Objectives
At the end of the lesson, the student should be able
to:
 Explain the basic concepts and operations on the
ADT stack
 Implement the ADT stack using sequential and
linked representation
 Discuss applications of stack: the pattern
recognition problem and conversion from infix
to postfix
 Explain how multiple stacks can be stored
using one dimensional array
 Reallocate memory during stack overflow in
multiple-stack array using unit-shift policy and
Garwick's algorithm
Introduction
 Stack - linearly ordered set of elements having
the last-in, first-out (LIFO) discipline
 Operations are done on top of the stack only
and we have no access to other elements
 Basic operations: push and pop
 Representation: sequential or linked
Operations
 Insertion of new element onto the stack (push)
 Deletion of the top element from the stack (pop)
 Getting the size of stack
 Checking for empty stack
 Getting the top element without deleting it from the
stack
Operations
 Push
Operations
 Pop
Operations
procedure SPUSH(S,n,top,x)
array S(1:n)
if top = n then call STACKFULL
top <- top + 1
S(top) <- x
end SPUSH
procedure SPOP(S,n,top,x)
array S(1:n)
if top = 0 then call STACKEMPTY
else [ x <- S(top);
top <- top –1 ]
end SPOP
Operations
public interface Stack
{
public int size(); /* returns the size of the stack */
public boolean isEmpty(); /* checks if empty */
public Object top() throws StackException;
public Object pop() throws StackException;
public void push(Object item) throws StackException;
}
class StackException extends RuntimeException
{
public StackException(String err)
{
super(err);
}
}
interface
 interface – reserved word in java, a class
that contains only the method headings, and
each method heading is terminated with a
semicolon
inheritance
 Inheritance means that a new class can
be derived from or based on an already
existing class.
 The new class inherits features such as
methods from the existing class, which
saves a lot of time for programmers.
 Example: a newly define class
StackException that would extend the
definition of RuntimeException.
inheritance
 When you use inheritance, the class
containing your application program will
have more than one method.
 Constructor
 is a special type of method of a class that is
automatically executed when an object of the class
is created.
 It is used to initialize object. The name of the
constructor is always the same as the name of the
class
Implementations
 Sequential Representation
 Makes use of arrays
 Stack is empty if top=-1 and full if top=n-1
 Deletion from an empty stack causes an underflow
 Insertion onto a full stack causes an overflow
Implementations
public class ArrayStack implements Stack
{
public static final int CAPACITY = 1000;
public int capacity;
Object S[];
int top = -1;
public ArrayStack()
{
this(CAPACITY);
}
public ArrayStack(int c)
{
capacity = c;
S = new Object[capacity];
}
public int size()
{
return (top+1);
}
public boolean isEmpty()
{
return (top < 0);
}
Implementations
public Object top()
{
if (isEmpty()) throw new
StackException("Stack empty.");
return S[top];
}
public Object pop()
{
Object item;
if (isEmpty()) throw new
StackException("Stack underflow.");
item = S[top];
S[top--] = null;
return item;
}
public void push(Object item)
{
if (size()==capacity)
throw new StackException
("Stack overflow.");
S[++top]=item;
}
Implementations
•Linked Representation
– A linked list of stack nodes could be used to
implement a stack
Implementations
public class LinkedStack implements Stack
{
private Node top;
private int numElements = 0;
public int size()
{
return (numElements);
}
public boolean isEmpty()
{
return (top == null);
}
public Object top()
{
if (isEmpty())
throw new StackException
("Stack empty.");
return top.info;
}
Implementations
public Object pop()
{
Node temp;
if (isEmpty())
throw new StackException("Stack underflow.");
temp = top;
top = top.link;
return temp.info;
}
public void push(Object item)
{
Node newNode = new Node();
newNode.info = item;
newNode.link = top;
top = newNode;
}
}
Application: Infix to Postfix
 Expressions can be in:
 infix form - every subexpression is of the
form operand-operator-operand
 postfix form - every subexpression is of
the form operand-operand-operator, form
most appropriate for computers
Application: Infix to Postfix
 Theorem: A postfix expression is well-formed if the
rank of every proper head is greater than or equal to 1 and
the rank of the expression is 1.
Operator Priority Property Example
^ 3 right associative a^b^c = a^(b^c)
* / 2 left associative a*b*c = (a*b)*c
+ - 1 left associative a+b+c = (a+b)+c
Application: Infix to Postfix
 Examples:
Infix Expression Postfix Expression
a * b + c / d a b * c d / -
a ^ b ^ c - d a b c ^ ^ d -
a * ( b + ( c + d ) / e ) - f a b c d + e /+* f -
a * b / c + f * ( g + d ) / ( f – h ) ^ i a b * c / f g d + * f h – i ^ / +
Application: Infix to Postfix
 Exercises:
Infix Expression
1) a * b + c / d a b * c d / -
2) a ^ b ^ c - d a b c ^ ^ d -
3) a * ( b + ( c + d ) / e ) - f a b c d + e /+* f -
4) a * b / c + f * ( g + d ) / ( f – h ) ^ i a b * c / f g d + * f h – i ^ / +

More Related Content

What's hot

Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
Nguync91368
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
Nguync91368
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course
 
Lesson 6 recursion
Lesson 6  recursionLesson 6  recursion
Lesson 6 recursion
MLG College of Learning, Inc
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
Hanif Durad
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
Edward Chen
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
Hanif Durad
 
Chapter11
Chapter11Chapter11
Chapter11
Rohini Sharma
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
Andres Mendez-Vazquez
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
Rsquared Academy
 
Programming in R
Programming in RProgramming in R
Programming in R
Smruti Sarangi
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data Structure
Sakthi Dasans
 
Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R Language
Gaurang Dobariya
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_list
Andres Mendez-Vazquez
 
Java arrays
Java   arraysJava   arrays
Java arrays
Maneesha Caldera
 
R language introduction
R language introductionR language introduction
R language introduction
Shashwat Shriparv
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
Hermann Hueck
 
Lesson 3 simple sorting
Lesson 3   simple sortingLesson 3   simple sorting
Lesson 3 simple sorting
MLG College of Learning, Inc
 

What's hot (20)

Python Pandas
Python PandasPython Pandas
Python Pandas
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Lesson 6 recursion
Lesson 6  recursionLesson 6  recursion
Lesson 6 recursion
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Chapter11
Chapter11Chapter11
Chapter11
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
Programming in R
Programming in RProgramming in R
Programming in R
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data Structure
 
Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R Language
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_list
 
Java arrays
Java   arraysJava   arrays
Java arrays
 
R language introduction
R language introductionR language introduction
R language introduction
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
Lesson 3 simple sorting
Lesson 3   simple sortingLesson 3   simple sorting
Lesson 3 simple sorting
 

Similar to Advanced data structures slide 2 2+

DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
kumarkaushal17
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
Hanif Durad
 
Stack
StackStack
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
bunnykhan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stack
StackStack
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Lec2
Lec2Lec2
Lec2
Lec2Lec2
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
AliJama14
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
ecomputernotes
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
ecomputernotes
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
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
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
BHARATH KUMAR
 

Similar to Advanced data structures slide 2 2+ (20)

DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Stack
StackStack
Stack
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Lec2
Lec2Lec2
Lec2
 
Lec2
Lec2Lec2
Lec2
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
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
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
 

More from jomerson remorosa

XML DTD Validate
XML DTD ValidateXML DTD Validate
XML DTD Validate
jomerson remorosa
 
ORACLE: Normalization student
ORACLE: Normalization student ORACLE: Normalization student
ORACLE: Normalization student
jomerson remorosa
 
ORACLE: Database management system student
ORACLE: Database management system studentORACLE: Database management system student
ORACLE: Database management system student
jomerson remorosa
 
Cisco network 1 1
Cisco network 1 1Cisco network 1 1
Cisco network 1 1
jomerson remorosa
 
Logic design and switching theory
Logic design and switching theoryLogic design and switching theory
Logic design and switching theory
jomerson remorosa
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
jomerson remorosa
 

More from jomerson remorosa (6)

XML DTD Validate
XML DTD ValidateXML DTD Validate
XML DTD Validate
 
ORACLE: Normalization student
ORACLE: Normalization student ORACLE: Normalization student
ORACLE: Normalization student
 
ORACLE: Database management system student
ORACLE: Database management system studentORACLE: Database management system student
ORACLE: Database management system student
 
Cisco network 1 1
Cisco network 1 1Cisco network 1 1
Cisco network 1 1
 
Logic design and switching theory
Logic design and switching theoryLogic design and switching theory
Logic design and switching theory
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 

Recently uploaded

Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 

Recently uploaded (20)

Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 

Advanced data structures slide 2 2+

  • 2. Objectives At the end of the lesson, the student should be able to:  Explain the basic concepts and operations on the ADT stack  Implement the ADT stack using sequential and linked representation  Discuss applications of stack: the pattern recognition problem and conversion from infix to postfix  Explain how multiple stacks can be stored using one dimensional array  Reallocate memory during stack overflow in multiple-stack array using unit-shift policy and Garwick's algorithm
  • 3. Introduction  Stack - linearly ordered set of elements having the last-in, first-out (LIFO) discipline  Operations are done on top of the stack only and we have no access to other elements  Basic operations: push and pop  Representation: sequential or linked
  • 4. Operations  Insertion of new element onto the stack (push)  Deletion of the top element from the stack (pop)  Getting the size of stack  Checking for empty stack  Getting the top element without deleting it from the stack
  • 7. Operations procedure SPUSH(S,n,top,x) array S(1:n) if top = n then call STACKFULL top <- top + 1 S(top) <- x end SPUSH procedure SPOP(S,n,top,x) array S(1:n) if top = 0 then call STACKEMPTY else [ x <- S(top); top <- top –1 ] end SPOP
  • 8. Operations public interface Stack { public int size(); /* returns the size of the stack */ public boolean isEmpty(); /* checks if empty */ public Object top() throws StackException; public Object pop() throws StackException; public void push(Object item) throws StackException; } class StackException extends RuntimeException { public StackException(String err) { super(err); } }
  • 9. interface  interface – reserved word in java, a class that contains only the method headings, and each method heading is terminated with a semicolon
  • 10. inheritance  Inheritance means that a new class can be derived from or based on an already existing class.  The new class inherits features such as methods from the existing class, which saves a lot of time for programmers.  Example: a newly define class StackException that would extend the definition of RuntimeException.
  • 11. inheritance  When you use inheritance, the class containing your application program will have more than one method.  Constructor  is a special type of method of a class that is automatically executed when an object of the class is created.  It is used to initialize object. The name of the constructor is always the same as the name of the class
  • 12. Implementations  Sequential Representation  Makes use of arrays  Stack is empty if top=-1 and full if top=n-1  Deletion from an empty stack causes an underflow  Insertion onto a full stack causes an overflow
  • 13. Implementations public class ArrayStack implements Stack { public static final int CAPACITY = 1000; public int capacity; Object S[]; int top = -1; public ArrayStack() { this(CAPACITY); } public ArrayStack(int c) { capacity = c; S = new Object[capacity]; } public int size() { return (top+1); } public boolean isEmpty() { return (top < 0); }
  • 14. Implementations public Object top() { if (isEmpty()) throw new StackException("Stack empty."); return S[top]; } public Object pop() { Object item; if (isEmpty()) throw new StackException("Stack underflow."); item = S[top]; S[top--] = null; return item; } public void push(Object item) { if (size()==capacity) throw new StackException ("Stack overflow."); S[++top]=item; }
  • 15. Implementations •Linked Representation – A linked list of stack nodes could be used to implement a stack
  • 16. Implementations public class LinkedStack implements Stack { private Node top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public Object top() { if (isEmpty()) throw new StackException ("Stack empty."); return top.info; }
  • 17. Implementations public Object pop() { Node temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.link; return temp.info; } public void push(Object item) { Node newNode = new Node(); newNode.info = item; newNode.link = top; top = newNode; } }
  • 18. Application: Infix to Postfix  Expressions can be in:  infix form - every subexpression is of the form operand-operator-operand  postfix form - every subexpression is of the form operand-operand-operator, form most appropriate for computers
  • 19. Application: Infix to Postfix  Theorem: A postfix expression is well-formed if the rank of every proper head is greater than or equal to 1 and the rank of the expression is 1. Operator Priority Property Example ^ 3 right associative a^b^c = a^(b^c) * / 2 left associative a*b*c = (a*b)*c + - 1 left associative a+b+c = (a+b)+c
  • 20. Application: Infix to Postfix  Examples: Infix Expression Postfix Expression a * b + c / d a b * c d / - a ^ b ^ c - d a b c ^ ^ d - a * ( b + ( c + d ) / e ) - f a b c d + e /+* f - a * b / c + f * ( g + d ) / ( f – h ) ^ i a b * c / f g d + * f h – i ^ / +
  • 21. Application: Infix to Postfix  Exercises: Infix Expression 1) a * b + c / d a b * c d / - 2) a ^ b ^ c - d a b c ^ ^ d - 3) a * ( b + ( c + d ) / e ) - f a b c d + e /+* f - 4) a * b / c + f * ( g + d ) / ( f – h ) ^ i a b * c / f g d + * f h – i ^ / +