You are on page 1of 93

Stacks and Queues

LE Thanh Sach

Chapter 5
Basic operations of
Stacks and Queues Stacks

Implementation of
Stacks
Data Structures and Algorithms Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
LE Thanh Sach Queues

Faculty of Computer Science and Engineering Implementation of


Queue
University of Technology, VNU-HCM Linked-list implementation
Array implementation

Applications of
Queue

5.1
Stacks and Queues
Outcomes
LE Thanh Sach

• L.O.2.1 - Depict the following concepts: (a) array list


and linked list, including single link and double links,
and multiple links; (b) stack; and (c) queue and circular Basic operations of
queue. Stacks

Implementation of
• L.O.2.2 - Describe storage structures by using Stacks

pseudocode for: (a) array list and linked list, including Linked-list implementation
Array implementation

single link and double links, and multiple links; (b) Applications of
Stack
stack; and (c) queue and circular queue.
Basic operations of
• L.O.2.3 - List necessary methods supplied for list, Queues

stack, and queue, and describe them using pseudocode. Implementation of


Queue

• L.O.2.4 - Implement list, stack, and queue using Linked-list implementation


Array implementation

C/C++. Applications of
Queue

5.2
Stacks and Queues
Outcomes
LE Thanh Sach

• L.O.2.5 - Use list, stack, and queue for problems in


real-life, and choose an appropriate implementation
type (array vs. link).
Basic operations of
• L.O.2.6 - Analyze the complexity and develop Stacks

experiment (program) to evaluate the efficiency of Implementation of


Stacks
methods supplied for list, stack, and queue. Linked-list implementation
Array implementation
• L.O.8.4 - Develop recursive implementations for Applications of
methods supplied for the following structures: list, tree, Stack

Basic operations of
heap, searching, and graphs. Queues

• L.O.1.2 - Analyze algorithms and use Big-O notation to Implementation of


Queue
characterize the computational complexity of algorithms Linked-list implementation
Array implementation
composed by using the following control structures:
Applications of
sequence, branching, and iteration (not recursion). Queue

5.3
Stacks and Queues
Contents
LE Thanh Sach

1 Basic operations of Stacks

2 Implementation of Stacks
Linked-list implementation Basic operations of
Array implementation Stacks

Implementation of
Stacks
3 Applications of Stack Linked-list implementation
Array implementation

Applications of
4 Basic operations of Queues Stack

Basic operations of
Queues
5 Implementation of Queue
Implementation of
Linked-list implementation Queue
Linked-list implementation
Array implementation Array implementation

Applications of
Queue
6 Applications of Queue

5.4
Stacks and Queues

LE Thanh Sach

Basic operations of

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation

Stacks Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.5
Stacks and Queues
Linear List Concepts
LE Thanh Sach

General list:

• No restrictions on which operation can be


used on the list. Basic operations of
Stacks
• No restrictions on where data can be Implementation of

inserted/deleted. Stacks
Linked-list implementation
Array implementation

Applications of
Stack
Restricted list: Basic operations of
Queues

• Only some operations can be used on the Implementation of


Queue

list. Linked-list implementation


Array implementation

Applications of
• Data can be inserted/deleted only at the Queue

ends of the list.


5.6
Stacks and Queues
Linear list concepts
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.7
Stacks and Queues
Stack
LE Thanh Sach
Definition
A stack of elements of type T is a finite sequence of
elements of T, in which all insertions and deletions are
restricted to one end, called the top.
Basic operations of
Stacks

Stack is a Last In - First Out (LIFO) data structure. Implementation of


Stacks
LIFO: The last item put on the stack is the first item that Linked-list implementation
Array implementation
can be taken off. Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.8
Stacks and Queues
Basic operations of Stacks
LE Thanh Sach

Basic operations:

• Construct a stack, leaving it empty. Basic operations of


Stacks

Implementation of
• Push an element: put a new element on to Stacks
Linked-list implementation

the top of the stack. Array implementation

Applications of
• Pop an element: remove the top element Stack

Basic operations of
from the top of the stack. Queues

Implementation of
• Top an element: retrieve the top element. Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.9
Stacks and Queues
Basic operations of Stacks
LE Thanh Sach

Extended operations:

• Determine whether the stack is empty or Basic operations of


Stacks

not. Implementation of
Stacks
Linked-list implementation

• Determine whether the stack is full or Array implementation

Applications of
not. Stack

Basic operations of
• Find the size of the stack. Queues

Implementation of
• Clear the stack to make it empty. Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.10
Stacks and Queues
Basic operations of Stacks: Push
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Hình: Successful Push operation Linked-list implementation
Array implementation

Applications of
Queue

5.11
Stacks and Queues
Basic operations of Stacks: Push
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Hình: Unsuccessful Push operation. Stack remains unchanged. Applications of


Queue

5.12
Stacks and Queues
Basic operations of Stacks: Pop
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Hình: Successful Pop operation Linked-list implementation
Array implementation

Applications of
Queue

5.13
Stacks and Queues
Basic operations of Stacks: Pop
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Hình: Unsuccessful Pop operation. Stack remains unchanged. Queue

5.14
Stacks and Queues
Basic operations of Stacks: Top
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Hình: Successful Top operation. Stack remains unchanged. Applications of


Queue

5.15
Stacks and Queues
Basic operations of Stacks: Top
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Hình: Unsuccessful Top operation. Stack remains unchanged. Queue

5.16
Stacks and Queues

LE Thanh Sach

Basic operations of

Implementation of
Stacks

Implementation of
Stacks
Linked-list implementation

Stacks Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.17
Stacks and Queues
Linked-list implementation
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.18
Stacks and Queues
Linked-list implementation
LE Thanh Sach

Stack structure

stack
count < integer > Basic operations of
Stacks
top < node pointer >
Implementation of
end stack Stacks
Linked-list implementation
Array implementation

Applications of
Stack
Stack node structure
Basic operations of
Queues

Implementation of
node Queue
Linked-list implementation
data < dataType > Array implementation

next < node pointer > Applications of


end node Queue

5.19
Stacks and Queues
Linked-list implementation in C++
LE Thanh Sach

template <c l a s s ItemType>


s t r u c t Node {
Basic operations of
ItemType d a t a ; Stacks

Node<ItemType> ∗ n e x t ; Implementation of
Stacks
}; Linked-list implementation
Array implementation

Applications of
Stack

template <c l a s s L i s t _ I t e m T y p e > Basic operations of


Queues
c l a s s Stack { Implementation of
public : Queue
Linked-list implementation
Stack ( ) ; Array implementation

~Stack ( ) ; Applications of
Queue

5.20
Stacks and Queues
Linked-list implementation in C++
LE Thanh Sach

v o i d Push ( L i s t _ I t e m T y p e d a t a I n ) ;
i n t Pop ( L i s t _ I t e m T y p e &dataOut ) ;
i n t GetStackTop ( L i s t _ I t e m T y p e &dataOut ) ; Basic operations of
Stacks
void C l e a r ( ) ; Implementation of
i n t IsEmpty ( ) ; Stacks
Linked-list implementation

int GetSize ( ) ; Array implementation

Stack <L i s t _ I t e m T y p e >∗ C l o n e ( ) ; Applications of


Stack
void P r i n t 2 C o n s o l e ( ) ; Basic operations of
Queues

Implementation of
private : Queue
Node<L i s t _ I t e m T y p e >∗ t o p ; Linked-list implementation
Array implementation

i n t count ; Applications of
}; Queue

5.21
Stacks and Queues
Create an empty Linked Stack
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.22
Stacks and Queues
Create an empty Linked Stack
LE Thanh Sach

Algorithm createStack(ref stack


<metadata>) Basic operations of
Stacks
Initializes the metadata of a stack Implementation of

Pre: stack is a metadata structure of a stack Stacks


Linked-list implementation
Array implementation

Post: metadata initialized Applications of


Stack

Basic operations of
stack.count = 0 Queues

Implementation of
stack.top = null Queue
Linked-list implementation

return Array implementation

Applications of
End createStack Queue

5.23
Stacks and Queues
Create an empty Linked Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


Basic operations of
Stack <L i s t _ I t e m T y p e > : : S t a c k ( ) { Stacks
t h i s −>t o p = NULL ; Implementation of
Stacks
t h i s −>c o u n t = 0 ; Linked-list implementation

} Array implementation

Applications of
Stack
template <c l a s s L i s t _ I t e m T y p e > Basic operations of
Queues
Stack <L i s t _ I t e m T y p e >::~ S t a c k ( ) {
Implementation of
t h i s −>C l e a r ( ) ; Queue
Linked-list implementation
} Array implementation

Applications of
Queue

5.24
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
1 Allocate memory for the new node and set up data. Queue
Linked-list implementation
2 Update pointers: Array implementation

• Point the new node to the top node (before adding the Applications of
Queue
new node).
• Point top to the new node.
3 Update count
5.25
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach

Algorithm pushStack(ref stack <metadata>,


val data <dataType>) Basic operations of
Stacks
Inserts (pushes) one item into the stack Implementation of
Pre: stack is a metadata structure to a valid Stacks
Linked-list implementation

stack
Array implementation

Applications of
Stack
data contains value to be pushed into the Basic operations of
stack Queues

Implementation of
Post: data have been pushed in stack Queue
Linked-list implementation

Return true if successful; false if memory Array implementation

Applications of
overflow Queue

5.26
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach

if stack full then


success = false
else Basic operations of
Stacks
allocate (pNew) Implementation of
Stacks
pNew -> data = data Linked-list implementation
Array implementation

pNew -> next = stack.top Applications of


Stack
stack.top = pNew Basic operations of
Queues
stack.count = stack.count + 1 Implementation of

success = true Queue


Linked-list implementation

end Array implementation

Applications of
return success Queue

End pushStack
5.27
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


Basic operations of
v o i d Stack <L i s t _ I t e m T y p e > : : Push Stacks
( List_ItemType v a l u e ){ Implementation of
Stacks
Node<L i s t _ I t e m T y p e >∗ pNew = Linked-list implementation

new Node<L i s t _ I t e m T y p e > ( ) ; Array implementation

Applications of
pNew−>d a t a = v a l u e ; Stack
pNew−>n e x t = t h i s −>t o p ; Basic operations of
Queues
t h i s −>t o p = pNew ;
Implementation of
t h i s −>c o u n t ++; Queue
Linked-list implementation
} Array implementation

Applications of
Queue

5.28
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach

• Push is successful when allocation memory for the new


node is successful.
• There is no difference between push data into a stack Basic operations of
Stacks
having elements and push data into an empty stack Implementation of
(top having NULL value is assigned to pNew->next: Stacks
Linked-list implementation
that’s corresponding to a list having only one element). Array implementation

Applications of
Stack

Basic operations of
Queues
pNew−>n e x t = t o p
Implementation of
t o p = pNew Queue
Linked-list implementation
count = count + 1 Array implementation

Applications of
Queue

5.29
Stacks and Queues
Pop Linked Stack
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation

1 dltPtr holds the element on the top of the stack. Array implementation

Applications of
2 top points to the next element. Queue

3 Recycle dltPtr. Decrease count by 1.


5.30
Stacks and Queues
Pop Linked Stack
LE Thanh Sach

Algorithm popStack(ref stack <metadata>,


ref dataOut <dataType>) Basic operations of

Pops the item on the top of the stack and Stacks

Implementation of
returns it to caller Stacks
Linked-list implementation

Pre: stack is a metadata structure to a valid Array implementation

Applications of

stack Stack

Basic operations of
dataOut is to receive the popped data Queues

Implementation of
Post: data have been returned to caller Queue
Linked-list implementation

Return true if successful; false if stack is Array implementation

Applications of
empty Queue

5.31
Stacks and Queues
Pop Linked Stack
LE Thanh Sach

if stack empty then


success = false
else Basic operations of
Stacks
dltPtr = stack.top Implementation of
Stacks
dataOut = stack.top -> data Linked-list implementation
Array implementation

stack.top = stack.top -> next Applications of


Stack
stack.count = stack.count - 1 Basic operations of
Queues
recycle(dltPtr) Implementation of

success = true Queue


Linked-list implementation

end Array implementation

Applications of
return success Queue

End popStack
5.32
Stacks and Queues
Pop Linked Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


i n t Stack <L i s t _ I t e m T y p e > : : Pop
Basic operations of
( L i s t _ I t e m T y p e &dataOut ) { Stacks

i f ( t h i s −>G e t S i z e ( ) == 0 ) Implementation of
Stacks
return 0; Linked-list implementation
Array implementation
Node<L i s t _ I t e m T y p e >∗ d l t P t r = t h i s −>t o p ;
Applications of
dataOut = d l t P t r −>d a t a ; Stack

t h i s −>t o p = d l t P t r −>n e x t ; Basic operations of


Queues
t h i s −>count −−; Implementation of
delete d l t P t r ; Queue
Linked-list implementation
return 1; Array implementation

} Applications of
Queue

5.33
Stacks and Queues
Pop Linked Stack
LE Thanh Sach

• Pop is successful when the stack is not empty.


• There is no difference between pop an element from a
stack having elements and pop the only-one element in Basic operations of
Stacks
the stack (dltPtr->next having NULL value is Implementation of
assigned to top: that’s corresponding to an empty Stacks
Linked-list implementation
stack). Array implementation

Applications of
Stack

Basic operations of
Queues
t o p = d l t P t r −>n e x t
Implementation of
recycle dltPtr Queue
Linked-list implementation
count = count − 1 Array implementation

Applications of
Queue

5.34
Stacks and Queues
Stack Top
LE Thanh Sach

Algorithm stackTop(ref stack <metadata>,


ref dataOut <dataType>) Basic operations of

Retrieves the data from the top of the stack Stacks

Implementation of
without changing the stack Stacks
Linked-list implementation

Pre: stack is a metadata structure to a valid Array implementation

Applications of

stack Stack

Basic operations of
dataOut is to receive top stack data Queues

Implementation of
Post: data have been returned to caller Queue
Linked-list implementation

Return true if successful; false if stack is Array implementation

Applications of
empty Queue

5.35
Stacks and Queues
Stack Top
LE Thanh Sach

if stack empty then Basic operations of


success = false Stacks

Implementation of
else Stacks
Linked-list implementation

dataOut = stack.top -> data Array implementation

Applications of
success = true Stack

end Basic operations of


Queues

return success Implementation of


Queue

End stackTop Linked-list implementation


Array implementation

Applications of
Queue

5.36
Stacks and Queues
Stack Top
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


i n t Stack <L i s t _ I t e m T y p e > : : GetStackTop Basic operations of
Stacks
( L i s t _ I t e m T y p e &dataOut ) {
Implementation of
Stacks

i f ( t h i s −>G e t S i z e ( ) == 0 ) Linked-list implementation


Array implementation

return 0; Applications of
Stack

Basic operations of
dataOut = t h i s −>top−>d a t a ; Queues

Implementation of
Queue
return 1; Linked-list implementation
Array implementation
}
Applications of
Queue

5.37
Stacks and Queues
Destroy Stack
LE Thanh Sach

Algorithm destroyStack(ref stack Basic operations of


Stacks
<metadata>) Implementation of
Stacks
Releases all nodes back to memory Linked-list implementation
Array implementation

Applications of
Stack
Pre: stack is a metadata structure to a valid Basic operations of
stack Queues

Implementation of
Post: stack empty and all nodes recycled Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.38
Stacks and Queues
Destroy Stack
LE Thanh Sach

if stack not empty then


while stack.top not null do Basic operations of
temp = stack.top Stacks

Implementation of
stack.top = stack.top -> next Stacks
Linked-list implementation

recycle(temp) Array implementation

Applications of
end Stack

Basic operations of
end Queues

Implementation of
stack.count = 0 Queue
Linked-list implementation

return Array implementation

Applications of
End destroyStack Queue

5.39
Stacks and Queues
Destroy Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


Basic operations of
v o i d Stack <L i s t _ I t e m T y p e > : : C l e a r ( ) { Stacks
Node<L i s t _ I t e m T y p e >∗ temp ; Implementation of
Stacks
w h i l e ( t h i s −>t o p != NULL) { Linked-list implementation

temp = t h i s −>t o p ; Array implementation

Applications of
t h i s −>t o p = t h i s −>top−>n e x t ; Stack
d e l e t e temp ; Basic operations of
Queues
}
Implementation of
t h i s −>c o u n t = 0 ; Queue
Linked-list implementation
} Array implementation

Applications of
Queue

5.40
Stacks and Queues
isEmpty Linked Stack
LE Thanh Sach

Algorithm isEmpty(ref stack <metadata>)


Determines if the stack is empty
Pre: stack is a metadata structure to a valid Basic operations of
Stacks
stack Implementation of

Post: return stack status Stacks


Linked-list implementation
Array implementation

Return true if the stack is empty, false Applications of


Stack
otherwise Basic operations of

if count = 0 then Queues

Implementation of
Return true Queue
Linked-list implementation

else Array implementation

Applications of
Return false Queue

end
End isEmpty 5.41
Stacks and Queues
isEmpty Linked Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e > Basic operations of


i n t Stack <L i s t _ I t e m T y p e > : : I s E m p t y ( ) { Stacks

Implementation of
r e t u r n ( c o u n t == 0 ) ; Stacks
} Linked-list implementation
Array implementation

Applications of
template <c l a s s L i s t _ I t e m T y p e > Stack

Basic operations of
i n t Stack <L i s t _ I t e m T y p e > : : G e t S i z e ( ) { Queues
return count ; Implementation of
Queue
} Linked-list implementation
Array implementation

Applications of
Queue

5.42
Stacks and Queues
isFull Linked Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


i n t Stack <L i s t _ I t e m T y p e > : : I s F u l l ( ) {
Basic operations of
Node<L i s t _ I t e m T y p e >∗ pNew = Stacks

new Node<L i s t _ I t e m T y p e > ( ) ; Implementation of


Stacks
Linked-list implementation
Array implementation
i f ( pNew != NULL) {
Applications of
delete pNew ; Stack

return 0; Basic operations of


Queues
} else { Implementation of
return 1; Queue
Linked-list implementation
} Array implementation

} Applications of
Queue

5.43
Stacks and Queues
Print Stack
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


Basic operations of
v o i d Stack <L i s t _ I t e m T y p e > : : P r i n t 2 C o n s o l e ( ) { Stacks
Node<L i s t _ I t e m T y p e >∗ p ; Implementation of
Stacks
p = t h i s −>t o p ; Linked-list implementation

w h i l e ( p != NULL) { Array implementation

Applications of
c o u t << p−>d a t a << " ␣ " ; Stack
p = p−>n e x t ; Basic operations of
Queues
}
Implementation of
c o u t << e n d l ; Queue
Linked-list implementation
} Array implementation

Applications of
Queue

5.44
Stacks and Queues
Using Stack
LE Thanh Sach

i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
Stack <i n t > ∗ myStack = new Stack <i n t > ( ) ;
int val ; Basic operations of
Stacks
myStack−>Push ( 7 ) ; Implementation of
myStack−>Push ( 9 ) ; Stacks
Linked-list implementation

myStack−>Push ( 1 0 ) ; Array implementation

myStack−>Push ( 8 ) ; Applications of
Stack
myStack−>P r i n t 2 C o n s o l e ( ) ; Basic operations of
myStack−>Pop ( v a l ) ; Queues

Implementation of
myStack−>P r i n t 2 C o n s o l e ( ) ; Queue
d e l e t e myStack ; Linked-list implementation
Array implementation

return 0; Applications of
} Queue

5.45
Stacks and Queues
Array-based stack implementation
LE Thanh Sach

Implementation of array-based stack is very simple. It uses


top variable to point to the topmost stack’s element in the
array.
Basic operations of
Stacks

1 Initialy top = -1; Implementation of


Stacks
2 push operation increases top by one and writes pushed Linked-list implementation
Array implementation

element to storage[top]; Applications of


Stack
3 pop operation checks that top is not equal to -1 and
Basic operations of
decreases top variable by 1; Queues

Implementation of
4 getTop operation checks that top is not equal to -1 Queue
and returns storage[top]; Linked-list implementation
Array implementation

5 isEmpty returns boolean if top == -1. Applications of


Queue

5.46
Stacks and Queues
Array-based stack implementation
LE Thanh Sach

#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;

class ArrayStack { Basic operations of


Stacks
private : Implementation of
i n t top ; Stacks
Linked-list implementation
int capacity ; Array implementation

int ∗ storage ; Applications of


Stack

Basic operations of
public : Queues

ArrayStack ( int capacity ) { Implementation of


Queue
s t o r a g e = new i n t [ c a p a c i t y ] ; Linked-list implementation
Array implementation

t h i s −>c a p a c i t y = c a p a c i t y ; Applications of
t o p = −1; Queue

}
// . . .
5.47
Stacks and Queues
Array-based stack implementation
LE Thanh Sach

~ArrayStack () {
delete [ ] storage ;
}
Basic operations of
Stacks
v o i d push ( i n t v a l u e ) { Implementation of
i f ( t o p == c a p a c i t y − 1 ) Stacks
Linked-list implementation
throw s t r i n g ( " S t a c k ␣ i s ␣ o v e r f l o w " ) ; Array implementation

t o p++; Applications of
Stack
s t o r a g e [ top ] = v a l u e ; Basic operations of
} Queues

Implementation of
Queue
v o i d pop ( i n t &dataOut ) { Linked-list implementation
Array implementation

i f ( t o p == −1) Applications of
throw s t r i n g ( " S t a c k ␣ i s ␣ empty " ) ; Queue

dataOut = s t o r a g e [ t o p ] ;
top −−;
5.48
Stacks and Queues
Array-based stack implementation
LE Thanh Sach

i n t getTop ( ) {
i f ( t o p == −1)
throw s t r i n g ( " S t a c k ␣ i s ␣ empty " ) ; Basic operations of
Stacks
return s t o r a g e [ top ] ; Implementation of
} Stacks
Linked-list implementation
Array implementation

bool i s E m p t y ( ) { Applications of
Stack
r e t u r n ( t o p == −1); Basic operations of
} Queues

Implementation of
Queue
bool i s F u l l ( ) { Linked-list implementation
Array implementation

r e t u r n ( t o p == c a p a c i t y −1); Applications of
} Queue

5.49
Stacks and Queues
Array-based stack implementation
LE Thanh Sach

int getSize () {
return top + 1 ;
} Basic operations of
Stacks

Implementation of
void p r i n t 2 C o n s o l e ( ) { Stacks
i f ( t o p > −1) { Linked-list implementation
Array implementation

f o r ( i n t i = t o p ; i >= 0 ; i −−) { Applications of


c o u t << s t o r a g e [ i ] << " ␣ " ; Stack

Basic operations of
} Queues
c o u t << e n d l ; Implementation of
Queue
} Linked-list implementation

} Array implementation

Applications of
Queue
};

5.50
Stacks and Queues
Using array-based stack
LE Thanh Sach

i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
A r r a y S t a c k ∗ myStack = new A r r a y S t a c k ( 1 0 ) ;
int val ; Basic operations of
Stacks
myStack−>push ( 7 ) ; Implementation of
myStack−>push ( 9 ) ; Stacks
Linked-list implementation

myStack−>push ( 1 0 ) ; Array implementation

myStack−>push ( 8 ) ; Applications of
Stack
myStack−>p r i n t 2 C o n s o l e ( ) ; Basic operations of
myStack−>pop ( v a l ) ; Queues

Implementation of
myStack−>p r i n t 2 C o n s o l e ( ) ; Queue
d e l e t e myStack ; Linked-list implementation
Array implementation

return 0; Applications of
} Queue

5.51
Stacks and Queues

LE Thanh Sach

Basic operations of
Stacks

Applications of Stack Implementation of


Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.52
Stacks and Queues
Applications of Stack
LE Thanh Sach
• Reversing data items
• Reverse a list

• Convert Decimal to Binary


• Parsing Basic operations of
Stacks
• Brackets Parse Implementation of

• Postponement of processing data items Stacks


Linked-list implementation
Array implementation

• Infix to Postfix Transformation Applications of


Stack
• Evaluate a Postfix Expression Basic operations of
Queues
• Backtracking Implementation of
Queue
• Goal Seeking Problem Linked-list implementation
Array implementation

• Knight’s Tour Applications of


Queue
• Exiting a Maze

• Eight Queens Problem


5.53
Stacks and Queues

LE Thanh Sach

Basic operations of

Basic operations of Stacks

Implementation of
Stacks

Queues
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.54
Stacks and Queues
Queue
LE Thanh Sach

Definition
A queue of elements of type T is a finite sequence of
elements of T, in which data can only be inserted at one end
called the rear, and deleted from the other end called the
Basic operations of
front. Stacks

Implementation of
Stacks
Linked-list implementation

Queue is a First In - First Out (FIFO) data structure. Array implementation

Applications of
FIFO: The first item stored in the queue is the first item that Stack
can be taken out. Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.55
Stacks and Queues
Basic operations of Queues
LE Thanh Sach
Basic operations:

• Construct a queue, leaving it empty.


• Enqueue: put a new element in to the rear
Basic operations of
of the queue. Stacks

Implementation of
• Dequeue: remove the first element from Stacks
Linked-list implementation

the front of the queue. Array implementation

Applications of
Stack
• Queue Front: retrieve the front element. Basic operations of
Queues
• Queue Rear: retrieve the rear element. Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.56
Stacks and Queues
Basic operations of Queues: Enqueue
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.57
Stacks and Queues
Basic operations of Queues: Dequeue
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.58
Stacks and Queues
Basic operations of Queues: Queue Front
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.59
Stacks and Queues
Basic operations of Queues: Queue Rear
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.60
Stacks and Queues

LE Thanh Sach

Basic operations of

Implementation of Stacks

Implementation of
Stacks

Queue
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.61
Stacks and Queues
Linked-list implementation
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.62
Stacks and Queues
Linked-list implementation
LE Thanh Sach

Queue structure

queue
count < integer > Basic operations of
front < node pointer > Stacks
rear < node pointer > Implementation of
Stacks
endqueue
Linked-list implementation
Array implementation

Applications of
Stack

Queue node structure Basic operations of


Queues

Implementation of
Queue
node Linked-list implementation
Array implementation
data < dataType >
Applications of
next < node pointer > Queue
end node

5.63
Stacks and Queues
Linked-list implementation in C++
LE Thanh Sach

template <c l a s s ItemType>


s t r u c t Node {
Basic operations of
ItemType d a t a ; Stacks

Node<ItemType> ∗ n e x t ; Implementation of
Stacks
}; Linked-list implementation
Array implementation

Applications of
Stack

template <c l a s s L i s t _ I t e m T y p e > Basic operations of


Queues
c l a s s Queue { Implementation of
public : Queue
Linked-list implementation
Queue ( ) ; Array implementation

~Queue ( ) ; Applications of
Queue

5.64
Stacks and Queues
Linked-list implementation in C++
LE Thanh Sach

v o i d Enqueue ( L i s t _ I t e m T y p e d a t a I n ) ;
i n t Dequeue ( L i s t _ I t e m T y p e &dataOut ) ;
i n t GetQueueFront ( L i s t _ I t e m T y p e &dataOut ) Basic
; operations of
Stacks
i n t GetQueueRear ( L i s t _ I t e m T y p e &dataOut ) ; Implementation of
void C l e a r ( ) ; Stacks
Linked-list implementation

i n t IsEmpty ( ) ; Array implementation

int GetSize ( ) ; Applications of


Stack
void P r i n t 2 C o n s o l e ( ) ; Basic operations of
Queues

Implementation of
private : Queue
Node<L i s t _ I t e m T y p e > ∗ f r o n t , ∗ r e a r ; Linked-list implementation
Array implementation

i n t count ; Applications of
}; Queue

5.65
Stacks and Queues
Create Queue
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.66
Stacks and Queues
Create Queue
LE Thanh Sach

Algorithm createQueue(ref queue


<metadata>)
Basic operations of
Initializes the metadata of a queue Stacks

Pre: queue is a metadata structure of a queue Implementation of


Stacks
Linked-list implementation

Post: metadata initialized Array implementation

Applications of
Stack

queue.count= 0 Basic operations of


Queues

queue.front = null Implementation of


Queue
queue.rear = null Linked-list implementation
Array implementation

return Applications of
Queue

End createQueue
5.67
Stacks and Queues
Create Queue
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


Queue<L i s t _ I t e m T y p e > : : Queue ( ) { Basic operations of
Stacks
t h i s −>c o u n t = 0 ;
Implementation of
t h i s −>f r o n t = NULL ; Stacks

t h i s −>r e a r = NULL ; Linked-list implementation


Array implementation

} Applications of
Stack

Basic operations of
template <c l a s s L i s t _ I t e m T y p e > Queues

Queue<L i s t _ I t e m T y p e >::~ Queue ( ) { Implementation of


Queue
t h i s −>C l e a r ( ) ; Linked-list implementation
Array implementation
}
Applications of
Queue

5.68
Stacks and Queues
Enqueue: Insert into an empty queue
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Hình: Insert into an empty queue Linked-list implementation
Array implementation

Applications of
Queue

5.69
Stacks and Queues
Enqueue: Insert into a queue with data
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Hình: Insert into a queue with data Array implementation

Applications of
Queue

5.70
Stacks and Queues
Enqueue
LE Thanh Sach

Algorithm enqueue(ref queue <metadata>,


val data <dataType>)
Basic operations of
Inserts one item at the rear of the queue Stacks

Implementation of
Stacks

Pre: queue is a metadata structure of a valid Linked-list implementation


Array implementation

queue Applications of
Stack

data contains data to be inserted into queue Basic operations of


Queues

Implementation of
Queue
Post: data have been inserted in queue Linked-list implementation
Array implementation

Return true if successful, false if memory Applications of


Queue
overflow
5.71
Stacks and Queues
Enqueue
LE Thanh Sach

if queue full then


return false
end
allocate (newPtr) Basic operations of
newPtr -> data = data Stacks

newPtr -> next = null Implementation of


Stacks
if queue.count = 0 then Linked-list implementation
Array implementation

// Insert into an empty queue Applications of


queue.front = newPtr Stack

Basic operations of
else Queues
// Insert into a queue with data Implementation of
Queue
queue.rear -> next = newPtr Linked-list implementation

end Array implementation

Applications of
queue.rear = newPtr Queue

queue.count = queue.count + 1
return true
End enqueue 5.72
Stacks and Queues
Enqueue
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


v o i d Queue<L i s t _ I t e m T y p e > : : Enqueue
( List_ItemType v a l u e ){ Basic operations of
Node<L i s t _ I t e m T y p e >∗ newPtr = new Stacks

Implementation of
Node<L i s t _ I t e m T y p e > ( ) ; Stacks
newPtr−>d a t a = v a l u e ; Linked-list implementation
Array implementation

newPtr−>n e x t = NULL ; Applications of


i f ( t h i s −>c o u n t == 0 ) Stack

Basic operations of
t h i s −>f r o n t = newPtr ; Queues
else Implementation of
Queue
t h i s −>r e a r −>n e x t = newPtr ; Linked-list implementation

t h i s −>r e a r = newPtr ; Array implementation

Applications of
t h i s −>c o u n t ++; Queue
}

5.73
Stacks and Queues
Dequeue: Delete data in a queue with only one item
LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Hình: Delete data in a queue with only one item Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.74
Stacks and Queues
Dequeue: Delete data in a queue with more than one
item LE Thanh Sach

Basic operations of
Stacks

Implementation of
Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Hình: Delete data in a queue with more than one item Linked-list implementation
Array implementation

Applications of
Queue

5.75
Stacks and Queues
Dequeue
LE Thanh Sach

Algorithm dequeue(ref queue <metadata>,


ref dataOut <dataType>)
Deletes one item at the front of the queue and Basic operations of
Stacks

returns its data to caller Implementation of


Stacks
Linked-list implementation
Array implementation

Pre: queue is a metadata structure of a valid Applications of


Stack
queue Basic operations of
Queues
dataOut is to receive dequeued data Implementation of
Queue
Linked-list implementation

Post: front data have been returned to caller Array implementation

Applications of
Return true if successful, false if memory Queue

overflow
5.76
Stacks and Queues
Dequeue
LE Thanh Sach

if queue empty then


return false
end
Basic operations of
dataOut = queue.front -> data Stacks

dltPtr = queue.front Implementation of


Stacks
if queue.count = 1 then Linked-list implementation
Array implementation
// Delete data in a queue with only one item Applications of
queue.rear = NULL Stack

end Basic operations of


Queues
queue.front = queue.front -> next Implementation of
queue.count = queue.count - 1 Queue
Linked-list implementation

recycle (dltPtr) Array implementation

return true Applications of


Queue
End dequeue

5.77
Stacks and Queues
Dequeue
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


i n t Queue<L i s t _ I t e m T y p e > : : Dequeue (
L i s t _ I t e m T y p e &dataOut ) { Basic operations of
i f ( c o u n t == 0 ) Stacks

Implementation of
return 0; Stacks
dataOut = f r o n t −>d a t a ; Linked-list implementation
Array implementation

Node<L i s t _ I t e m T y p e >∗ d l t P t r= t h i s −>f r o n t ; Applications of


i f ( c o u n t == 1 ) Stack

Basic operations of
t h i s −>r e a r = NULL ; Queues
t h i s −>f r o n t = t h i s −>f r o n t −>n e x t ; Implementation of
Queue
t h i s −>count −−; Linked-list implementation

delete d l t P t r ; Array implementation

Applications of
return 1; Queue
}

5.78
Stacks and Queues
Queue Front
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e > Basic operations of


Stacks
i n t Queue<L i s t _ I t e m T y p e > : : GetQueueFront Implementation of
( L i s t _ I t e m T y p e &dataOut ) { Stacks
Linked-list implementation

i f ( c o u n t == 0 ) Array implementation

return 0; Applications of
Stack
dataOut = t h i s −>f r o n t −>d a t a ; Basic operations of
return 1; Queues

Implementation of
} Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.79
Stacks and Queues
Queue Rear
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e > Basic operations of


Stacks
i n t Queue<L i s t _ I t e m T y p e > : : GetQueueRear Implementation of
( L i s t _ I t e m T y p e &dataOut ) { Stacks
Linked-list implementation

i f ( c o u n t == 0 ) Array implementation

return 0; Applications of
Stack
dataOut = t h i s −>r e a r −>d a t a ; Basic operations of
return 1; Queues

Implementation of
} Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.80
Stacks and Queues
Destroy Queue
LE Thanh Sach

Algorithm destroyQueue(ref queue


<metadata>) Basic operations of
Stacks
Deletes all data from a queue Implementation of
Stacks
Linked-list implementation

Pre: queue is a metadata structure of a valid Array implementation

Applications of

queue Stack

Basic operations of
Queues

Post: queue empty and all nodes recycled Implementation of


Queue
Linked-list implementation
Array implementation

Return nothing Applications of


Queue

5.81
Stacks and Queues
Destroy Queue
LE Thanh Sach

if queue not empty then


while queue.front not null do
temp = queue.front Basic operations of
Stacks
queue.front = queue.front->next Implementation of
Stacks
recycle(temp) Linked-list implementation
Array implementation

end Applications of
Stack
end Basic operations of
Queues
queue.front = NULL Implementation of

queue.rear = NULL Queue


Linked-list implementation
Array implementation
queue.count = 0 Applications of
Queue
return
End destroyQueue
5.82
Stacks and Queues
Destroy Queue
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


v o i d Queue<L i s t _ I t e m T y p e > : : C l e a r ( ) {
Basic operations of
Node<L i s t _ I t e m T y p e >∗ temp ; Stacks

w h i l e ( t h i s −>f r o n t != NULL) { Implementation of


Stacks
temp = t h i s −>f r o n t ; Linked-list implementation
Array implementation
t h i s −>f r o n t= t h i s −>f r o n t −>n e x t ;
Applications of
d e l e t e temp ; Stack

} Basic operations of
Queues
t h i s −>f r o n t = NULL ; Implementation of
t h i s −>r e a r = NULL ; Queue
Linked-list implementation
t h i s −>c o u n t = 0 ; Array implementation

} Applications of
Queue

5.83
Stacks and Queues
Queue Empty
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e > Basic operations of


i n t Queue<L i s t _ I t e m T y p e > : : I s E m p t y ( ) { Stacks

Implementation of
r e t u r n ( t h i s −>c o u n t == 0 ) ; Stacks
} Linked-list implementation
Array implementation

Applications of
template <c l a s s L i s t _ I t e m T y p e > Stack

Basic operations of
i n t Queue<L i s t _ I t e m T y p e > : : G e t S i z e ( ) { Queues
r e t u r n t h i s −>c o u n t ; Implementation of
Queue
} Linked-list implementation
Array implementation

Applications of
Queue

5.84
Stacks and Queues
Print Queue
LE Thanh Sach

template <c l a s s L i s t _ I t e m T y p e >


v o i d Queue<L i s t _ I t e m T y p e > : : P r i n t 2 C o n s o l e ( ) { Basic operations of
Stacks
Node<L i s t _ I t e m T y p e >∗ p ;
Implementation of
p = t h i s −>f r o n t ; Stacks
Linked-list implementation
c o u t << " F r o n t : ␣ " ; Array implementation

w h i l e ( p != NULL) { Applications of
Stack
c o u t << p−>d a t a << " ␣ " ;
Basic operations of
p = p−>n e x t ; Queues

} Implementation of
Queue
c o u t << e n d l ; Linked-list implementation
Array implementation
}
Applications of
Queue

5.85
Stacks and Queues
Using Queue
LE Thanh Sach

i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
Queue<i n t > ∗myQueue = new Queue<i n t > ( ) ;
int val ; Basic operations of
Stacks
myQueue−>Enqueue ( 7 ) ; Implementation of
myQueue−>Enqueue ( 9 ) ; Stacks
Linked-list implementation

myQueue−>Enqueue ( 1 0 ) ; Array implementation

myQueue−>Enqueue ( 8 ) ; Applications of
Stack
myQueue−>P r i n t 2 C o n s o l e ( ) ; Basic operations of
myQueue−>Dequeue ( v a l ) ; Queues

Implementation of
myQueue−>P r i n t 2 C o n s o l e ( ) ; Queue
d e l e t e myQueue ; Linked-list implementation
Array implementation

return 1; Applications of
} Queue

5.86
Stacks and Queues
Array-based queue implementation
LE Thanh Sach

#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;
c l a s s ArrayQueue {
private : Basic operations of
Stacks
int capacity ; Implementation of
int front ; Stacks
Linked-list implementation
int rear ; Array implementation

int ∗ storage ; Applications of


Stack

Basic operations of
public : Queues

ArrayQueue ( i n t c a p a c i t y ) { Implementation of
Queue
s t o r a g e = new i n t [ c a p a c i t y ] ; Linked-list implementation
Array implementation

t h i s −>c a p a c i t y = c a p a c i t y ; Applications of
f r o n t = −1; Queue

r e a r = −1;
}
5.87
Stacks and Queues
Array-based queue implementation
LE Thanh Sach

~ArrayQueue ( ) {
delete [ ] storage ;
}
Basic operations of
Stacks
v o i d enQueue ( i n t v a l u e ) { Implementation of
i f ( i s F u l l ( ) ) throw s t r i n g ( " Queue ␣ i s ␣ f u l l " Stacks
);
Linked-list implementation
i f ( f r o n t == −1) f r o n t = 0 ; Array implementation

r e a r ++; Applications of
Stack
storage [ rear % capacity ] = value ; Basic operations of
} Queues

Implementation of
Queue
v o i d deQueue ( i n t &v a l u e O u t ) { Linked-list implementation
Array implementation

i f ( isEmpty ( ) ) Applications of
throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; Queue

valueOut = storage [ f r o n t % c a p a c i t y ] ;
f r o n t ++;
5.88
Stacks and Queues
Array-based queue implementation
LE Thanh Sach

int getFront () {
i f ( isEmpty ( ) ) Basic operations of
Stacks
throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ;
Implementation of
return storage [ f r o n t % c a p a c i t y ] ; Stacks
Linked-list implementation
} Array implementation

Applications of
Stack
int getRear () {
Basic operations of
i f ( isEmpty ( ) ) Queues

throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; Implementation of


Queue
return storage [ r e a r % c a p a c i t y ] ; Linked-list implementation
Array implementation
}
Applications of
Queue

5.89
Stacks and Queues
Array-based queue implementation
LE Thanh Sach

bool i s E m p t y ( ) {
r e t u r n ( f r o n t > r e a r | | f r o n t == −1);
} Basic operations of
Stacks

Implementation of
bool i s F u l l ( ) { Stacks
Linked-list implementation

r e t u r n ( r e a r − f r o n t + 1 == capacity ); Array implementation

} Applications of
Stack

Basic operations of
int getSize () { Queues

return r e a r − f r o n t + 1; Implementation of
Queue
} Linked-list implementation
Array implementation

Applications of
}; Queue

5.90
Stacks and Queues
Using Array-based queue
LE Thanh Sach

i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
ArrayQueue ∗myQueue = new ArrayQueue ( 1 0 ) ; Basic operations of
Stacks
int val ;
Implementation of
myQueue−>enQueue ( 7 ) ; Stacks
Linked-list implementation
myQueue−>enQueue ( 9 ) ; Array implementation

myQueue−>enQueue ( 1 0 ) ; Applications of
Stack
myQueue−>enQueue ( 8 ) ;
Basic operations of
myQueue−>deQueue ( v a l ) ; Queues

d e l e t e myQueue ; Implementation of
Queue
return 1; Linked-list implementation
Array implementation
}
Applications of
Queue

5.91
Stacks and Queues

LE Thanh Sach

Basic operations of
Stacks

Applications of Queue Implementation of


Stacks
Linked-list implementation
Array implementation

Applications of
Stack

Basic operations of
Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.92
Stacks and Queues
Applications of Queue
LE Thanh Sach

• Polynomial Arithmetic Basic operations of


Stacks

• Categorizing Data Implementation of


Stacks
Linked-list implementation
• Evaluate a Prefix Expression Array implementation

Applications of
• Radix Sort Stack

Basic operations of
• Queue Simulation Queues

Implementation of
Queue
Linked-list implementation
Array implementation

Applications of
Queue

5.93

You might also like