Ch6_Stacks_and_Queues
Ch6_Stacks_and_Queues
Ch6_Stacks_and_Queues
Dr. Nguyen Ho
Man Rang
Chapter 6
Stacks and Queues Basic operations of
Stacks
Implementation of
Data Structures and Algorithms Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Applications of
Queue
6.1
Stacks and Queues
Outcomes
Dr. Nguyen Ho
Man Rang
single link and double links, and multiple links; (b) Array implementation
Applications of
stack; and (c) queue and circular queue. Stack
Applications of
Queue
6.2
Stacks and Queues
Outcomes
Dr. Nguyen Ho
Man Rang
6.3
Stacks and Queues
Contents
Dr. Nguyen Ho
Man Rang
2 Implementation of Stacks
Linked-list implementation
Array implementation Basic operations of
Stacks
Implementation of
3 Applications of Stack Stacks
Linked-list implementation
Array implementation
Basic operations of
5 Implementation of Queue Queues
Linked-list implementation Implementation of
Queue
Array implementation Linked-list implementation
Array implementation
Applications of
6 Applications of Queue Queue
6.4
Stacks and Queues
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.5
Stacks and Queues
Linear List Concepts
Dr. Nguyen Ho
Man Rang
General list:
inserted/deleted. Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Restricted list: Stack
Basic operations of
Queues
• Only some operations can be used on the Implementation of
Queue
list. Linked-list implementation
Array implementation
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
6.7
Stacks and Queues
Stack
Dr. Nguyen Ho
Definition Man Rang
Basic operations of
Stacks
Stack is a Last In - First Out (LIFO) data structure. Implementation of
LIFO: The last item put on the stack is the first item that Stacks
Linked-list implementation
can be taken off. Array implementation
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.8
Stacks and Queues
Basic operations of Stacks
Dr. Nguyen Ho
Man Rang
Basic operations:
Applications of
Queue
6.9
Stacks and Queues
Basic operations of Stacks
Dr. Nguyen Ho
Man Rang
Extended operations:
not. Applications of
Stack
Applications of
Queue
6.10
Stacks and Queues
Basic operations of Stacks: Push
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Basic operations of
Queues
Implementation of
Hình: Successful Push operation Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.11
Stacks and Queues
Basic operations of Stacks: Push
Dr. Nguyen Ho
Man Rang
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
6.12
Stacks and Queues
Basic operations of Stacks: Pop
Dr. Nguyen Ho
Man Rang
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
6.13
Stacks and Queues
Basic operations of Stacks: Pop
Dr. Nguyen Ho
Man Rang
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 Pop operation. Stack remains unchanged. Applications of
Queue
6.14
Stacks and Queues
Basic operations of Stacks: Top
Dr. Nguyen Ho
Man Rang
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
Applications of
Queue
6.15
Stacks and Queues
Basic operations of Stacks: Top
Dr. Nguyen Ho
Man Rang
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 Top operation. Stack remains unchanged. Applications of
Queue
6.16
Stacks and Queues
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.17
Stacks and Queues
Linked-list implementation
Dr. Nguyen Ho
Man Rang
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
6.18
Stacks and Queues
Linked-list implementation
Dr. Nguyen Ho
Man Rang
Stack structure
stack
count < integer >
Basic operations of
top < node pointer > Stacks
end stack
Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack node structure Stack
Basic operations of
Queues
node Implementation of
Queue
data < dataType > Linked-list implementation
next < node pointer > Array implementation
6.19
Stacks and Queues
Linked-list implementation in C++
Dr. Nguyen Ho
Man Rang
Applications of
Stack
template <c l a s s L i s t _ I t e m T y p e >
Basic operations of
c l a s s Stack { Queues
public : Implementation of
Queue
Stack ( ) ; Linked-list implementation
Array implementation
~Stack ( ) ;
Applications of
Queue
6.20
Stacks and Queues
Linked-list implementation in C++
Dr. Nguyen Ho
Man Rang
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
void C l e a r ( ) ; Stacks
i n t IsEmpty ( ) ; Implementation of
Stacks
int GetSize ( ) ; Linked-list implementation
Array implementation
Stack <L i s t _ I t e m T y p e >∗ C l o n e ( ) ;
Applications of
void P r i n t 2 C o n s o l e ( ) ; Stack
Basic operations of
Queues
private : Implementation of
Node<L i s t _ I t e m T y p e >∗ t o p ; Queue
Linked-list implementation
i n t count ; Array implementation
}; Applications of
Queue
6.21
Stacks and Queues
Create an empty Linked Stack
Dr. Nguyen Ho
Man Rang
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
6.22
Stacks and Queues
Create an empty Linked Stack
Dr. Nguyen Ho
Man Rang
Basic operations of
stack.count = 0 Queues
Implementation of
stack.top = null Queue
Linked-list implementation
Applications of
End createStack Queue
6.23
Stacks and Queues
Create an empty Linked Stack
Dr. Nguyen Ho
Man Rang
Implementation of
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
Stack <L i s t _ I t e m T y p e >::~ S t a c k ( ) { Queues
t h i s −>C l e a r ( ) ; Implementation of
Queue
} Linked-list implementation
Array implementation
Applications of
Queue
6.24
Stacks and Queues
Push data into a Linked Stack
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Basic operations of
Queues
1 Allocate memory for the new node and set up data. Implementation of
Queue
2 Update pointers: Linked-list implementation
• Point the new node to the top node (before adding the Array implementation
Applications of
new node). Queue
• Point top to the new node.
3 Update count
6.25
Stacks and Queues
Push data into a Linked Stack
Dr. Nguyen Ho
Man Rang
6.26
Stacks and Queues
Push data into a Linked Stack
Dr. Nguyen Ho
Man Rang
Implementation of
pNew -> data = data Stacks
Linked-list implementation
Applications of
stack.top = pNew Stack
Basic operations of
stack.count = stack.count + 1 Queues
End pushStack
6.27
Stacks and Queues
Push data into a Linked Stack
Dr. Nguyen Ho
Man Rang
Implementation 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 > ( ) ; Linked-list implementation
Array implementation
pNew−>d a t a = v a l u e ; Applications of
pNew−>n e x t = t h i s −>t o p ; Stack
Basic operations of
t h i s −>t o p = pNew ; Queues
t h i s −>c o u n t ++; Implementation of
Queue
} Linked-list implementation
Array implementation
Applications of
Queue
6.28
Stacks and Queues
Push data into a Linked Stack
Dr. Nguyen Ho
Man Rang
Applications of
Stack
Basic operations of
pNew−>n e x t = t o p Queues
t o p = pNew Implementation of
Queue
count = count + 1 Linked-list implementation
Array implementation
Applications of
Queue
6.29
Stacks and Queues
Pop Linked Stack
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
1 dltPtr holds the element on the top of the stack. Linked-list implementation
Array implementation
is empty Applications of
Queue
6.31
Stacks and Queues
Pop Linked Stack
Dr. Nguyen Ho
Man Rang
Implementation of
dataOut = stack.top -> data Stacks
Linked-list implementation
Applications of
stack.count = stack.count - 1 Stack
Basic operations of
recycle(dltPtr) Queues
End popStack
6.32
Stacks and Queues
Pop Linked Stack
Dr. Nguyen Ho
Man Rang
delete d l t P t r ; Implementation of
Queue
return 1; Linked-list implementation
Array implementation
}
Applications of
Queue
6.33
Stacks and Queues
Pop Linked Stack
Dr. Nguyen Ho
Man Rang
Applications of
Stack
Basic operations of
t o p = d l t P t r −>n e x t Queues
Applications of
Queue
6.34
Stacks and Queues
Stack Top
Dr. Nguyen Ho
Man Rang
is empty Applications of
Queue
6.35
Stacks and Queues
Stack Top
Dr. Nguyen Ho
Man Rang
else Implementation of
Stacks
dataOut = stack.top -> data Linked-list implementation
Array implementation
Implementation of
End stackTop Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.36
Stacks and Queues
Stack Top
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
i f ( t h i s −>G e t S i z e ( ) == 0 ) Linked-list implementation
Applications of
Stack
dataOut = t h i s −>top−>d a t a ; Basic operations of
Queues
Implementation of
return 1; Queue
Linked-list implementation
} Array implementation
Applications of
Queue
6.37
Stacks and Queues
Destroy Stack
Dr. Nguyen Ho
Man Rang
Implementation of
Releases all nodes back to memory Stacks
Linked-list implementation
Array implementation
Applications of
Queue
6.38
Stacks and Queues
Destroy Stack
Dr. Nguyen Ho
Man Rang
end Applications of
Stack
stack.count = 0 Implementation of
Queue
return Linked-list implementation
Array implementation
6.39
Stacks and Queues
Destroy Stack
Dr. Nguyen Ho
Man Rang
Implementation of
w h i l e ( t h i s −>t o p != NULL) { Stacks
temp = t h i s −>t o p ; Linked-list implementation
Array implementation
Basic operations of
} Queues
t h i s −>c o u n t = 0 ; Implementation of
Queue
} Linked-list implementation
Array implementation
Applications of
Queue
6.40
Stacks and Queues
isEmpty Linked Stack
Dr. Nguyen Ho
Man Rang
else Applications of
Queue
Return false
end 6.41
Stacks and Queues
isEmpty Linked Stack
Dr. Nguyen Ho
Man Rang
Implementation of
} Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.42
Stacks and Queues
isFull Linked Stack
Dr. Nguyen Ho
Man Rang
return 1; Implementation of
Queue
} Linked-list implementation
Array implementation
}
Applications of
Queue
6.43
Stacks and Queues
Print Stack
Dr. Nguyen Ho
Man Rang
Implementation of
p = t h i s −>t o p ; Stacks
w h i l e ( p != NULL) { Linked-list implementation
Array implementation
Basic operations of
} Queues
c o u t << e n d l ; Implementation of
Queue
} Linked-list implementation
Array implementation
Applications of
Queue
6.44
Stacks and Queues
Using Stack
Dr. Nguyen Ho
Man Rang
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
myStack−>Push ( 7 ) ; Stacks
myStack−>Push ( 9 ) ; Implementation of
Stacks
myStack−>Push ( 1 0 ) ; Linked-list implementation
Array implementation
myStack−>Push ( 8 ) ;
Applications of
myStack−>P r i n t 2 C o n s o l e ( ) ; Stack
} Applications of
Queue
6.45
Stacks and Queues
Array-based stack implementation
Dr. Nguyen Ho
Man Rang
Applications of
3 pop operation checks that top is not equal to -1 and Stack
decreases top variable by 1; Basic operations of
Queues
4 getTop operation checks that top is not equal to -1 Implementation of
and returns storage[top]; Queue
Linked-list implementation
Applications of
Queue
6.46
Stacks and Queues
Array-based stack implementation
Dr. Nguyen Ho
Man Rang
#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
private : Stacks
i n t top ; Implementation of
Stacks
int capacity ; Linked-list implementation
Array implementation
int ∗ storage ;
Applications of
Stack
t o p = −1; Applications of
Queue
}
// . . .
6.47
Stacks and Queues
Array-based stack implementation
Dr. Nguyen Ho
Man Rang
~ArrayStack () {
delete [ ] storage ;
}
Basic operations of
v o i d push ( i n t v a l u e ) { Stacks
i f ( t o p == c a p a c i t y − 1 ) Implementation of
Stacks
throw s t r i n g ( " S t a c k ␣ i s ␣ o v e r f l o w " ) ; Linked-list implementation
Array implementation
t o p++;
Applications of
s t o r a g e [ top ] = v a l u e ; Stack
} Basic operations of
Queues
Implementation of
v o i d pop ( i n t &dataOut ) { Queue
Linked-list implementation
i f ( t o p == −1) Array implementation
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
return s t o r a g e [ top ] ; Stacks
} Implementation of
Stacks
Linked-list implementation
Array implementation
bool i s E m p t y ( ) {
Applications of
r e t u r n ( t o p == −1); Stack
} Basic operations of
Queues
Implementation of
bool i s F u l l ( ) { Queue
Linked-list implementation
r e t u r n ( t o p == c a p a c i t y −1); Array implementation
} Applications of
Queue
6.49
Stacks and Queues
Array-based stack implementation
Dr. Nguyen Ho
Man Rang
int getSize () {
return top + 1 ;
}
Basic operations of
Stacks
void p r i n t 2 C o n s o l e ( ) { Implementation of
i f ( t o p > −1) { Stacks
Linked-list implementation
Implementation of
} Queue
} Linked-list implementation
Array implementation
Applications of
}; Queue
6.50
Stacks and Queues
Using array-based stack
Dr. Nguyen Ho
Man Rang
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
myStack−>push ( 7 ) ; Stacks
myStack−>push ( 9 ) ; Implementation of
Stacks
myStack−>push ( 1 0 ) ; Linked-list implementation
Array implementation
myStack−>push ( 8 ) ;
Applications of
myStack−>p r i n t 2 C o n s o l e ( ) ; Stack
} Applications of
Queue
6.51
Stacks and Queues
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.52
Stacks and Queues
Applications of Stack
Dr. Nguyen Ho
• Reversing data items Man Rang
• Reverse a list
• Convert Decimal to Binary
• Parsing
Basic operations of
• Brackets Parse Stacks
Applications of
• Evaluate a Postfix Expression Stack
Applications of
• Exiting a Maze Queue
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.54
Stacks and Queues
Queue
Dr. Nguyen Ho
Man Rang
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
front. Basic operations of
Stacks
Implementation of
Stacks
Queue is a First In - First Out (FIFO) data structure. Linked-list implementation
Array implementation
FIFO: The first item stored in the queue is the first item that Applications of
can be taken out. Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.55
Stacks and Queues
Basic operations of Queues
Dr. Nguyen Ho
Basic operations: Man Rang
Basic operations of
• Queue Rear: retrieve the rear element. Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.56
Stacks and Queues
Basic operations of Queues: Enqueue
Dr. Nguyen Ho
Man Rang
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
6.57
Stacks and Queues
Basic operations of Queues: Dequeue
Dr. Nguyen Ho
Man Rang
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
6.58
Stacks and Queues
Basic operations of Queues: Queue Front
Dr. Nguyen Ho
Man Rang
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
6.59
Stacks and Queues
Basic operations of Queues: Queue Rear
Dr. Nguyen Ho
Man Rang
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
6.60
Stacks and Queues
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.61
Stacks and Queues
Linked-list implementation
Dr. Nguyen Ho
Man Rang
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
6.62
Stacks and Queues
Linked-list implementation
Dr. Nguyen Ho
Man Rang
Queue structure
queue
count < integer >
front < node pointer > Basic operations of
Stacks
rear < node pointer >
endqueue Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Queue 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
6.63
Stacks and Queues
Linked-list implementation in C++
Dr. Nguyen Ho
Man Rang
Applications of
Stack
template <c l a s s L i s t _ I t e m T y p e >
Basic operations of
c l a s s Queue { Queues
public : Implementation of
Queue
Queue ( ) ; Linked-list implementation
Array implementation
~Queue ( ) ;
Applications of
Queue
6.64
Stacks and Queues
Linked-list implementation in C++
Dr. Nguyen Ho
Man Rang
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
i n t GetQueueRear ( L i s t _ I t e m T y p e &dataOut ) ; Stacks
void C l e a r ( ) ; Implementation of
Stacks
i n t IsEmpty ( ) ; Linked-list implementation
Array implementation
int GetSize ( ) ;
Applications of
void P r i n t 2 C o n s o l e ( ) ; Stack
Basic operations of
Queues
private : Implementation of
Node<L i s t _ I t e m T y p e > ∗ f r o n t , ∗ r e a r ; Queue
Linked-list implementation
i n t count ; Array implementation
}; Applications of
Queue
6.65
Stacks and Queues
Create Queue
Dr. Nguyen Ho
Man Rang
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
6.66
Stacks and Queues
Create Queue
Dr. Nguyen Ho
Man Rang
Implementation of
queue Stacks
Linked-list implementation
Applications of
Stack
return Applications of
Queue
End createQueue
6.67
Stacks and Queues
Create Queue
Dr. Nguyen Ho
Man Rang
} 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
Queue<L i s t _ I t e m T y p e >::~ Queue ( ) {
Implementation of
t h i s −>C l e a r ( ) ; Queue
Linked-list implementation
} Array implementation
Applications of
Queue
6.68
Stacks and Queues
Enqueue: Insert into an empty queue
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Basic operations of
Queues
Applications of
Queue
6.69
Stacks and Queues
Enqueue: Insert into a queue with data
Dr. Nguyen Ho
Man Rang
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 a queue with data Linked-list implementation
Array implementation
Applications of
Queue
6.70
Stacks and Queues
Enqueue
Dr. Nguyen Ho
Man Rang
Applications of
data contains data to be inserted into Stack
Implementation of
Queue
Post: data have been inserted in queue Linked-list implementation
Array implementation
memory overflow
6.71
Stacks and Queues
Enqueue
Dr. Nguyen Ho
Man Rang
return true
End enqueue 6.72
Stacks and Queues
Enqueue
Dr. Nguyen Ho
Man Rang
i f ( t h i s −>c o u n t == 0 ) Applications of
Stack
t h i s −>f r o n t = newPtr ; Basic operations of
else Queues
6.73
Stacks and Queues
Dequeue: Delete data in a queue with only one item
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Implementation of
Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Basic operations of
Queues
Hình: Delete data in a queue with only one item Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.74
Stacks and Queues
Dequeue: Delete data in a queue with more than one
item Dr. Nguyen Ho
Man Rang
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 more than one item Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.75
Stacks and Queues
Dequeue
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
Pre: queue is a metadata structure of a Linked-list implementation
Array implementation
Implementation of
caller Applications of
Queue
Return true if successful, false if
memory overflow 6.76
Stacks and Queues
Dequeue
Dr. Nguyen Ho
Man Rang
6.77
Stacks and Queues
Dequeue
Dr. Nguyen Ho
Man Rang
i f ( c o u n t == 1 ) Applications of
Stack
t h i s −>r e a r = NULL ; Basic operations of
t h i s −>f r o n t = t h i s −>f r o n t −>n e x t ; Queues
return 1; Applications of
} Queue
6.78
Stacks and Queues
Queue Front
Dr. Nguyen Ho
Man Rang
( L i s t _ I t e m T y p e &dataOut ) { Implementation of
Stacks
i f ( c o u n t == 0 ) Linked-list implementation
Array implementation
return 0;
Applications of
dataOut = t h i s −>f r o n t −>d a t a ; Stack
Applications of
Queue
6.79
Stacks and Queues
Queue Rear
Dr. Nguyen Ho
Man Rang
( L i s t _ I t e m T y p e &dataOut ) { Implementation of
Stacks
i f ( c o u n t == 0 ) Linked-list implementation
Array implementation
return 0;
Applications of
dataOut = t h i s −>r e a r −>d a t a ; Stack
Applications of
Queue
6.80
Stacks and Queues
Destroy Queue
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
Basic operations of
Queues
Post: queue empty and all nodes recycled Implementation of
Queue
Linked-list implementation
Return nothing
Array implementation
Applications of
Queue
6.81
Stacks and Queues
Destroy Queue
Dr. Nguyen Ho
Man Rang
recycle(temp) Implementation of
Stacks
Linked-list implementation
end Array implementation
Applications of
end Stack
return Applications of
Queue
End destroyQueue
6.82
Stacks and Queues
Destroy Queue
Dr. Nguyen Ho
Man Rang
d e l e t e temp ; Applications of
Stack
}
Basic operations of
t h i s −>f r o n t = NULL ; Queues
6.83
Stacks and Queues
Queue Empty
Dr. Nguyen Ho
Man Rang
Implementation of
} Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.84
Stacks and Queues
Print Queue
Dr. Nguyen Ho
Man Rang
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
6.85
Stacks and Queues
Using Queue
Dr. Nguyen Ho
Man Rang
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
myQueue−>Enqueue ( 7 ) ; Stacks
myQueue−>Enqueue ( 9 ) ; Implementation of
Stacks
myQueue−>Enqueue ( 1 0 ) ; Linked-list implementation
Array implementation
myQueue−>Enqueue ( 8 ) ;
Applications of
myQueue−>P r i n t 2 C o n s o l e ( ) ; Stack
} Applications of
Queue
6.86
Stacks and Queues
Array-based queue implementation
Dr. Nguyen Ho
Man Rang
#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
int capacity ; Stacks
f r o n t = −1; Applications of
Queue
r e a r = −1;
}
6.87
Stacks and Queues
Array-based queue implementation
Dr. Nguyen Ho
Man Rang
~ArrayQueue ( ) {
delete [ ] storage ;
}
Basic operations of
v o i d enQueue ( i n t v a l u e ) { Stacks
} Basic operations of
Queues
Implementation of
v o i d deQueue ( i n t &v a l u e O u t ) { Queue
Linked-list implementation
i f ( isEmpty ( ) ) Array implementation
int getFront () {
i f ( isEmpty ( ) )
Basic operations of
throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; Stacks
return storage [ f r o n t % c a p a c i t y ] ; Implementation of
Stacks
} Linked-list implementation
Array implementation
Applications of
int getRear () { Stack
i f ( isEmpty ( ) ) Basic operations of
Queues
throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ;
Implementation of
return storage [ r e a r % c a p a c i t y ] ; Queue
Linked-list implementation
} Array implementation
Applications of
Queue
6.89
Stacks and Queues
Array-based queue implementation
Dr. Nguyen Ho
Man Rang
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
bool i s F u l l ( ) { Implementation of
r e t u r n ( r e a r − f r o n t + 1 == Stacks
Linked-list 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
6.90
Stacks and Queues
Using Array-based queue
Dr. Nguyen Ho
Man Rang
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
int val ; Stacks
myQueue−>enQueue ( 7 ) ; Implementation of
Stacks
myQueue−>enQueue ( 9 ) ; Linked-list implementation
Applications of
myQueue−>enQueue ( 8 ) ; Stack
myQueue−>deQueue ( v a l ) ; Basic operations of
Queues
d e l e t e myQueue ;
Implementation of
return 1; Queue
Linked-list implementation
} Array implementation
Applications of
Queue
6.91
Stacks and Queues
Dr. Nguyen Ho
Man Rang
Basic operations of
Stacks
Applications of
Stack
Basic operations of
Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.92
Stacks and Queues
Applications of Queue
Dr. Nguyen Ho
Man Rang
Implementation of
Stacks
• Evaluate a Prefix Expression Linked-list implementation
Array implementation
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
6.93