Professional Documents
Culture Documents
Slide, Stacks and Queues
Slide, 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
Applications of
Queue
5.1
Stacks and Queues
Outcomes
LE Thanh Sach
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
C/C++. Applications of
Queue
5.2
Stacks and Queues
Outcomes
LE Thanh Sach
Basic operations of
heap, searching, and graphs. Queues
5.3
Stacks and Queues
Contents
LE Thanh Sach
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
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:
inserted/deleted. Stacks
Linked-list implementation
Array implementation
Applications of
Stack
Restricted list: Basic operations of
Queues
Applications of
• Data can be inserted/deleted only at the Queue
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
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:
Implementation of
• Push an element: put a new element on to Stacks
Linked-list 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:
not. Implementation of
Stacks
Linked-list 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
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
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
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
5.19
Stacks and Queues
Linked-list implementation in C++
LE Thanh Sach
Node<ItemType> ∗ n e x t ; Implementation of
Stacks
}; Linked-list implementation
Array implementation
Applications of
Stack
~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
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
Basic operations of
stack.count = 0 Queues
Implementation of
stack.top = null Queue
Linked-list implementation
Applications of
End createStack Queue
5.23
Stacks and Queues
Create an empty Linked Stack
LE Thanh Sach
} 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
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
Applications of
overflow Queue
5.26
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach
Applications of
return success Queue
End pushStack
5.27
Stacks and Queues
Push data into a Linked Stack
LE Thanh Sach
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
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
Implementation of
returns it to caller Stacks
Linked-list 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
Applications of
empty Queue
5.31
Stacks and Queues
Pop Linked Stack
LE Thanh Sach
Applications of
return success Queue
End popStack
5.32
Stacks and Queues
Pop Linked Stack
LE Thanh Sach
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
} Applications of
Queue
5.33
Stacks and Queues
Pop Linked Stack
LE Thanh Sach
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
Implementation of
without changing the stack Stacks
Linked-list 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
Applications of
empty Queue
5.35
Stacks and Queues
Stack Top
LE Thanh Sach
Implementation of
else Stacks
Linked-list implementation
Applications of
success = true Stack
Applications of
Queue
5.36
Stacks and Queues
Stack Top
LE Thanh Sach
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
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
Implementation of
stack.top = stack.top -> next Stacks
Linked-list implementation
Applications of
end Stack
Basic operations of
end Queues
Implementation of
stack.count = 0 Queue
Linked-list implementation
Applications of
End destroyStack Queue
5.39
Stacks and Queues
Destroy Stack
LE Thanh Sach
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
Implementation of
Return true Queue
Linked-list implementation
Applications of
Return false Queue
end
End isEmpty 5.41
Stacks and Queues
isEmpty Linked Stack
LE Thanh Sach
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
} Applications of
Queue
5.43
Stacks and Queues
Print Stack
LE Thanh Sach
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 ( 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
4 getTop operation checks that top is not equal to -1 Queue
and returns storage[top]; Linked-list implementation
Array implementation
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 ;
Basic operations of
public : Queues
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
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 ( 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
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
LE Thanh Sach
Basic operations of
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
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:
Implementation of
• Dequeue: remove the first element from Stacks
Linked-list 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
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
Node<ItemType> ∗ n e x t ; Implementation of
Stacks
}; Linked-list implementation
Array implementation
Applications of
Stack
~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
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
Applications of
Stack
return Applications of
Queue
End createQueue
5.67
Stacks and Queues
Create Queue
LE Thanh Sach
} 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
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
Implementation of
Stacks
queue Applications of
Stack
Implementation of
Queue
Post: data have been inserted in queue Linked-list implementation
Array implementation
Basic operations of
else Queues
// Insert into a queue with data Implementation of
Queue
queue.rear -> next = newPtr Linked-list 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
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
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
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
Applications of
Return true if successful, false if memory Queue
overflow
5.76
Stacks and Queues
Dequeue
LE Thanh Sach
5.77
Stacks and Queues
Dequeue
LE Thanh Sach
Implementation of
return 0; Stacks
dataOut = f r o n t −>d a t a ; Linked-list implementation
Array implementation
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
Applications of
return 1; Queue
}
5.78
Stacks and Queues
Queue Front
LE Thanh Sach
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
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
Applications of
queue Stack
Basic operations of
Queues
5.81
Stacks and Queues
Destroy Queue
LE Thanh Sach
end Applications of
Stack
end Basic operations of
Queues
queue.front = NULL Implementation of
} 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
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
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 ( 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
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
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
} 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
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
Applications of
• Radix Sort Stack
Basic operations of
• Queue Simulation Queues
Implementation of
Queue
Linked-list implementation
Array implementation
Applications of
Queue
5.93