Ch05 Stack Queue
Ch05 Stack Queue
2. Implementation of Stacks
3. Applications of Stack
5. Implementation of Queue
6. Applications of Queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 1 / 86
Outcomes
• 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 queue.
• L.O.2.2 - Describe storage structures by using pseudocode for: (a) array list and linked
list, including single link and double links, and multiple links; (b) stack; and (c) queue and
circular queue.
• L.O.2.3 - List necessary methods supplied for list, stack, and queue, and describe them
using pseudocode.
• L.O.2.4 - Implement list, stack, and queue using C/C++.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 2 / 86
Outcomes
• L.O.2.5 - Use list, stack, and queue for problems in real-life, and choose an appropriate
implementation type (array vs. link).
• L.O.2.6 - Analyze the complexity and develop experiment (program) to evaluate the
efficiency of methods supplied for list, stack, and queue.
• L.O.8.4 - Develop recursive implementations for methods supplied for the following
structures: list, tree, heap, searching, and graphs.
• L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational
complexity of algorithms composed by using the following control structures: sequence,
branching, and iteration (not recursion).
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 86
Basic operations of Stacks
Linear List Concepts
General list:
• No restrictions on which operation can be used on the list.
• No restrictions on where data can be inserted/deleted.
Restricted list:
• Only some operations can be used on the list.
• Data can be inserted/deleted only at the ends of the list.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 4 / 86
Linear list concepts
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 5 / 86
Stack
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.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 6 / 86
Basic operations of Stacks
Basic operations:
• Construct a stack, leaving it empty.
• Push an element: put a new element on to the top of the stack.
• Pop an element: remove the top element from the top of the stack.
• Top an element: retrieve the top element.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 7 / 86
Basic operations of Stacks
Extended operations:
• Determine whether the stack is empty or not.
• Determine whether the stack is full or not.
• Find the size of the stack.
• Clear the stack to make it empty.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 8 / 86
Basic operations of Stacks: Push
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 86
Basic operations of Stacks: Push
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 86
Basic operations of Stacks: Pop
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 86
Basic operations of Stacks: Pop
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 86
Basic operations of Stacks: Top
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 86
Basic operations of Stacks: Top
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 14 / 86
Implementation of Stacks
Linked-list implementation
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 15 / 86
Linked-list implementation
Stack structure
stack
c o u n t <i n t e g e r >
t o p <node p o i n t e r >
end s t a c k
node
d a t a <dataType>
n e x t <node p o i n t e r >
end node
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 16 / 86
Linked-list implementation in C++
t e m p l a t e < c l a s s ItemType>
s t r u c t Node {
ItemType data ;
Node<ItemType> ∗ n e x t ;
};
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 17 / 86
Linked-list implementation in C++
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 18 / 86
Create an empty Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 19 / 86
Create an empty Linked Stack
stack.count = 0
stack.top = null
return
End createStack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 20 / 86
Create an empty Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 86
Push data into a Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 86
Push data into a Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 86
Push data into a Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 25 / 86
Push data into a Linked Stack
• Push is successful when allocation memory for the new node is successful.
• There is no difference between push data into a stack having elements and push data into
an empty stack (top having NULL value is assigned to pNew->next: that’s corresponding
to a list having only one element).
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 26 / 86
Pop Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 86
Pop Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 29 / 86
Pop Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 30 / 86
Pop Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 31 / 86
Stack Top
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 32 / 86
Stack Top
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 33 / 86
Stack Top
if ( t h i s −>G e t S i z e ( ) == 0 )
return 0;
d a t a O u t = t h i s −>top−>d a t a ;
return 1;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 34 / 86
Destroy Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 35 / 86
Destroy Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 36 / 86
Destroy Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 37 / 86
isEmpty Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 38 / 86
isEmpty Linked Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 39 / 86
isFull Linked Stack
if ( pNew != NULL) {
delete pNew ;
return 0;
} else {
return 1;
}
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 40 / 86
Print Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 41 / 86
Using Stack
i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) {
S t a c k <i n t > ∗ myStack = new S t a c k <i n t > ( ) ;
int val ;
myStack−>Push ( 7 ) ;
myStack−>Push ( 9 ) ;
myStack−>Push ( 1 0 ) ;
myStack−>Push ( 8 ) ;
myStack−>P r i n t 2 C o n s o l e ( ) ;
myStack−>Pop ( v a l ) ;
myStack−>P r i n t 2 C o n s o l e ( ) ;
d e l e t e myStack ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 42 / 86
Array-based stack implementation
Implementation of array-based stack is very simple. It uses top variable to point to the
topmost stack’s element in the array.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 43 / 86
Array-based stack implementation
#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 ArrayStack {
private :
i n t top ;
int capacity ;
int ∗storage ;
public :
ArrayStack ( i n t capacity ) {
s t o r a g e = new i n t [ c a p a c i t y ] ;
t h i s −>c a p a c i t y = c a p a c i t y ;
t o p = −1;
}
// . . .
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 44 / 86
Array-based stack implementation
~ArrayStack () {
delete [ ] storage ;
}
v o i d push ( i n t v a l u e ) {
i f ( t o p == c a p a c i t y − 1 )
throw s t r i n g ( " Stack ␣ i s ␣ o v e r f l o w " ) ;
t o p ++;
s t o r a g e [ top ] = v a l u e ;
}
v o i d pop ( i n t &d a t a O u t ) {
i f ( t o p == −1)
t h r o w s t r i n g ( " S t a c k ␣ i s ␣ empty " ) ;
dataOut = s t o r a g e [ top ] ;
top −−;
}
// ...
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 45 / 86
Array-based stack implementation
i n t getTop ( ) {
i f ( t o p == −1)
t h r o w s t r i n g ( " S t a c k ␣ i s ␣ empty " ) ;
r e t u r n s t o r a g e [ top ] ;
}
bool isEmpty ( ) {
r e t u r n ( t o p == −1);
}
bool i s F u l l () {
r e t u r n ( t o p == c a p a c i t y −1);
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 46 / 86
Array-based stack implementation
int getSize () {
r e t u r n top + 1 ;
}
void print2Console () {
i f ( t o p > −1) {
f o r ( i n t i = t o p ; i >= 0 ; i −−) {
c o u t << s t o r a g e [ i ] << " ␣ " ;
}
c o u t << e n d l ;
}
}
};
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 47 / 86
Using array-based stack
i n t main ( i n t a r g c , c h a r ∗ 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 ;
myStack−>p u s h ( 7 ) ;
myStack−>p u s h ( 9 ) ;
myStack−>p u s h ( 1 0 ) ;
myStack−>p u s h ( 8 ) ;
myStack−>p r i n t 2 C o n s o l e ( ) ;
myStack−>pop ( v a l ) ;
myStack−>p r i n t 2 C o n s o l e ( ) ;
d e l e t e myStack ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 48 / 86
Applications of Stack
Applications of Stack
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 49 / 86
Basic operations of Queues
Queue
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.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 50 / 86
Basic operations of Queues
Basic operations:
• Construct a queue, leaving it empty.
• Enqueue: put a new element in to the rear of the queue.
• Dequeue: remove the first element from the front of the queue.
• Queue Front: retrieve the front element.
• Queue Rear: retrieve the rear element.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 51 / 86
Basic operations of Queues: Enqueue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 52 / 86
Basic operations of Queues: Dequeue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 53 / 86
Basic operations of Queues: Queue Front
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 54 / 86
Basic operations of Queues: Queue Rear
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 55 / 86
Implementation of Queue
Linked-list implementation
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 56 / 86
Linked-list implementation
Queue structure
queue
c o u n t <i n t e g e r >
f r o n t <node p o i n t e r >
r e a r <node p o i n t e r >
endqueue
node
d a t a <dataType>
n e x t <node p o i n t e r >
end node
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 57 / 86
Linked-list implementation in C++
t e m p l a t e < c l a s s ItemType>
s t r u c t Node {
ItemType data ;
Node<ItemType> ∗ n e x t ;
};
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 58 / 86
Linked-list implementation in C++
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 &d a t a O u t ) ;
i n t G e t Q u e u e F r o n t ( L i s t _ I t e m T y p e &d a t a O u t ) ;
i n t GetQueueRear ( L i s t _ I t e m T y p e &d a t a O u t ) ;
void Clear ( ) ;
i n t IsEmpty ( ) ;
int GetSize ( ) ;
void Print2Console ( ) ;
private :
Node<L i s t _ I t e m T y p e > ∗ f r o n t , ∗ r e a r ;
i n t count ;
};
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 59 / 86
Create Queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 60 / 86
Create Queue
queue.count= 0
queue.front = null
queue.rear = null
return
End createQueue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 61 / 86
Create Queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 62 / 86
Enqueue: Insert into an empty queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 63 / 86
Enqueue: Insert into a queue with data
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 64 / 86
Enqueue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 65 / 86
Enqueue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 67 / 86
Dequeue: Delete data in a queue with only one item
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 68 / 86
Dequeue: Delete data in a queue with more than one item
Figure 10: Delete data in a queue with more than one item
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 69 / 86
Dequeue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 70 / 86
Dequeue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 72 / 86
Queue Front
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 73 / 86
Queue Rear
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 74 / 86
Destroy Queue
Return nothing
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 75 / 86
Destroy Queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 76 / 86
Destroy Queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 77 / 86
Queue Empty
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 78 / 86
Print Queue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 79 / 86
Using Queue
i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) {
Queue<i n t > ∗myQueue = new Queue<i n t > ( ) ;
int val ;
myQueue−>Enqueue ( 7 ) ;
myQueue−>Enqueue ( 9 ) ;
myQueue−>Enqueue ( 1 0 ) ;
myQueue−>Enqueue ( 8 ) ;
myQueue−>P r i n t 2 C o n s o l e ( ) ;
myQueue−>Dequeue ( v a l ) ;
myQueue−>P r i n t 2 C o n s o l e ( ) ;
d e l e t e myQueue ;
return 1;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 80 / 86
Array-based queue implementation
#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 :
int capacity ;
int front ;
int rear ;
int ∗storage ;
public :
ArrayQueue ( i n t c a p a c i t y ) {
s t o r a g e = new i n t [ c a p a c i t y ] ;
t h i s −>c a p a c i t y = c a p a c i t y ;
f r o n t = −1;
r e a r = −1;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 81 / 86
Array-based queue implementation
~ArrayQueue ( ) {
delete [ ] storage ;
}
v o i d enQueue ( i n t v a l u e ) {
i f ( i s F u l l ( ) ) t h r o w s t r i n g ( " Queue ␣ i s ␣ f u l l " ) ;
i f ( f r o n t == −1) f r o n t = 0 ;
r e a r ++;
storage [ rear % capacity ] = value ;
}
v o i d deQueue ( i n t &v a l u e O u t ) {
i f ( isEmpty ( ) )
t h r o w s t r i n g ( " Queue ␣ i s ␣ empty " ) ;
valueOut = storage [ f r o n t % c a p a c i t y ] ;
f r o n t ++;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 82 / 86
Array-based queue implementation
int getFront () {
i f ( isEmpty ( ) )
t h r o w s t r i n g ( " Queue ␣ i s ␣ empty " ) ;
return storage [ front % capacity ] ;
}
i n t getRear () {
i f ( isEmpty ( ) )
t h r o w s t r i n g ( " Queue ␣ i s ␣ empty " ) ;
return storage [ rear % capacity ] ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 83 / 86
Array-based queue implementation
bool isEmpty ( ) {
return ( front > rear || f r o n t == −1);
}
bool i s F u l l () {
r e t u r n ( r e a r − f r o n t + 1 == capacity );
}
int getSize () {
return rear − front + 1;
}
};
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 84 / 86
Using Array-based queue
i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) {
A r r a y Q u e u e ∗myQueue = new A r r a y Q u e u e ( 1 0 ) ;
int val ;
myQueue−>enQueue ( 7 ) ;
myQueue−>enQueue ( 9 ) ;
myQueue−>enQueue ( 1 0 ) ;
myQueue−>enQueue ( 8 ) ;
myQueue−>deQueue ( v a l ) ;
d e l e t e myQueue ;
return 1;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 85 / 86
Applications of Queue
Applications of Queue
• Polynomial Arithmetic
• Categorizing Data
• Evaluate a Prefix Expression
• Radix Sort
• Queue Simulation
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 86 / 86