






















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























































LECTURE 03: STACK & QUEUE LECTURE 03: STACK & QUEUE
Stacks
Array-based stack
Stack implemented by a singly linked list
Stack class in java.util
Queues
Priority Queues
Queue interface in java.util
Reading at home
Stacks
Array-based stack
Stack implemented by a singly linked list
Stack class in java.util
Queues
Priority Queues
Queue interface in java.util
Reading at home
Any sort of nesting (such as parentheses)
Evaluating arithmetic expressions (and other sorts of expression)
Implementing function or method calls
Keeping track of previous choices (as in backtracking )
Keeping track of choices yet to be made (as in creating a maze)
Undo sequence in a text editor
Auxiliary data structure for algorithms
Component of other data structure
Any sort of nesting (such as parentheses)
Evaluating arithmetic expressions (and other sorts of expression)
Implementing function or method calls
Keeping track of previous choices (as in backtracking )
Keeping track of choices yet to be made (as in creating a maze)
Undo sequence in a text editor
Auxiliary data structure for algorithms
Component of other data structure
ļ A simple way of implementing the Stack ADT uses an
array
ļ We add elements from left to right
ļ A variable top keeps track of the index of the top
element
ļ A simple way of implementing the Stack ADT uses an
array
ļ We add elements from left to right
ļ A variable top keeps track of the index of the top
element
Array based ImplementationArray based Implementation
ļ Common
characteristics
Object[] a
int top, max
ļ Constructors
ArrayStack(int
max)
ArrayStack() ā
default max=
ļ Common
characteristics
Object[] a
int top, max
ļ Constructors
ArrayStack(int
max)
ArrayStack() ā
default max=
ļ Common behaviors
isEmpty()
isFull()
clear()
grow()
push()
top()
pop()
ļ Common behaviors
isEmpty()
isFull()
clear()
grow()
push()
top()
pop()
LinkedList Implementation of
Stack
LinkedList Implementation of
Stack
10
0
5
/
%
1
2
/
%
0
1
/
%
1
0
/
%
Stop
return return
1
0
return
1
return
0
Values 2
0 2
1 2
2 2
3