0% found this document useful (0 votes)
59 views

Ch05 Stack Queue

The document describes stacks and queues, including their basic operations and implementations. It covers creating an empty linked stack, pushing data into a linked stack, and popping data from a linked stack. It also discusses queue operations and implementations using arrays and linked lists. The purpose is to teach students about these fundamental data structures and their applications.

Uploaded by

Hoàn Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Ch05 Stack Queue

The document describes stacks and queues, including their basic operations and implementations. It covers creating an empty linked stack, pushing data into a linked stack, and popping data from a linked stack. It also discusses queue operations and implementations using arrays and linked lists. The purpose is to teach students about these fundamental data structures and their applications.

Uploaded by

Hoàn Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

Data Structure and Algorithms [CO2003]

Chapter 5 - Stack and Queue

Lecturer: Duc Dung Nguyen, PhD.


Contact: [email protected]

Faculty of Computer Science and Engineering


Hochiminh city University of Technology
Contents

1. Basic operations of Stacks

2. Implementation of Stacks

3. Applications of Stack

4. Basic operations of Queues

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.

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


LIFO: The last item put on the stack is the first item that can be taken off.

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

Figure 1: Successful Push operation

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 86
Basic operations of Stacks: Push

Figure 2: Unsuccessful Push operation. Stack remains unchanged.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 86
Basic operations of Stacks: Pop

Figure 3: Successful Pop operation

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 86
Basic operations of Stacks: Pop

Figure 4: Unsuccessful Pop operation. Stack remains unchanged.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 86
Basic operations of Stacks: Top

Figure 5: Successful Top operation. Stack remains unchanged.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 86
Basic operations of Stacks: Top

Figure 6: Unsuccessful Top operation. Stack remains unchanged.

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

Stack node structure

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++

template <c l a s s List_ItemType>


c l a s s Stack {
public :
Stack ( ) ;
~Stack ( ) ;
v o i d Push ( L i s t _ I t e m T y p e d a t a I n ) ;
int Pop ( L i s t _ I t e m T y p e &d a t a O u t ) ;
int G e t S t a c k T o p ( L i s t _ I t e m T y p e &d a t a O u t ) ;
void Clear ( ) ;
int IsEmpty ( ) ;
int GetSize ( ) ;
S t a c k <L i s t _ I t e m T y p e >∗ C l o n e ( ) ;
void Print2Console ( ) ;
private :
Node<L i s t _ I t e m T y p e >∗ t o p ;
i n t count ;
};

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

Algorithm createStack(ref stack <metadata>)


Initializes the metadata of a stack
Pre: stack is a metadata structure of a stack
Post: metadata initialized

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

template <c l a s s List_ItemType>


S t a c k <L i s t _ I t e m T y p e > : : S t a c k ( ) {
t h i s −>t o p = NULL ;
t h i s −>c o u n t = 0 ;
}

template <c l a s s List_ItemType>


S t a c k <L i s t _ I t e m T y p e > : : ~ S t a c k ( ) {
t h i s −>C l e a r ( ) ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 86
Push data into a Linked Stack

1. Allocate memory for the new node and set up data.


2. Update pointers:
• Point the new node to the top node (before adding the new node).
• Point top to the new node.
3. Update count
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 22 / 86
Push data into a Linked Stack

Algorithm pushStack(ref stack <metadata>, val data <dataType>)


Inserts (pushes) one item into the stack
Pre: stack is a metadata structure to a valid stack
data contains value to be pushed into the stack
Post: data have been pushed in stack
Return true if successful; false if memory overflow

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 86
Push data into a Linked Stack

if stack full then


success = false
else
allocate (pNew)
pNew -> data = data
pNew -> next = stack.top
stack.top = pNew
stack.count = stack.count + 1
success = true
end
return success
End pushStack

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 86
Push data into a Linked Stack

template <c l a s s List_ItemType>


v o i d S t a c k <L i s t _ I t e m T y p e > : : Push
( List_ItemType v a l u e ){
Node<L i s t _ I t e m T y p e >∗ pNew =
new Node<L i s t _ I t e m T y p e > ( ) ;
pNew−>d a t a = v a l u e ;
pNew−>n e x t = t h i s −>t o p ;
t h i s −>t o p = pNew ;
t h i s −>c o u n t ++;
}

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

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


2. top points to the next element.
3. Recycle dltPtr. Decrease count by 1.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 27 / 86
Pop Linked Stack

Algorithm popStack(ref stack <metadata>, ref dataOut <dataType>)


Pops the item on the top of the stack and returns it to caller
Pre: stack is a metadata structure to a valid stack
dataOut is to receive the popped data
Post: data have been returned to caller
Return true if successful; false if stack is empty

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 86
Pop Linked Stack

if stack empty then


success = false
else
dltPtr = stack.top
dataOut = stack.top -> data
stack.top = stack.top -> next
stack.count = stack.count - 1
recycle(dltPtr)
success = true
end
return success
End popStack

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 29 / 86
Pop Linked Stack

template <c l a s s List_ItemType>


i n t S t a c k <L i s t _ I t e m T y p e > : : Pop
( L i s t _ I t e m T y p e &d a t a O u t ) {
i f ( t h i s −>G e t S i z e ( ) == 0 )
return 0;
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 ;
d a t a O u t = d l t P t r −>d a t a ;
t h i s −>t o p = d l t P t r −>n e x t ;
t h i s −>c o u n t −−;
delete dltPtr ;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 30 / 86
Pop Linked Stack

• 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 the stack (dltPtr->next having NULL value is assigned to top:
that’s corresponding to an empty stack).

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 31 / 86
Stack Top

Algorithm stackTop(ref stack <metadata>, ref dataOut <dataType>)


Retrieves the data from the top of the stack without changing the stack
Pre: stack is a metadata structure to a valid stack
dataOut is to receive top stack data
Post: data have been returned to caller
Return true if successful; false if stack is empty

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 32 / 86
Stack Top

if stack empty then


success = false
else
dataOut = stack.top -> data
success = true
end
return success
End stackTop

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 33 / 86
Stack Top

template <c l a s s List_ItemType>


i n t S t a c k <L i s t _ I t e m T y p e > : : G e t S t a c k T o p
( L i s t _ I t e m T y p e &d a t a O u t ) {

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

Algorithm destroyStack(ref stack <metadata>)


Releases all nodes back to memory

Pre: stack is a metadata structure to a valid stack


Post: stack empty and all nodes recycled

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 35 / 86
Destroy Stack

if stack not empty then


while stack.top not null do
temp = stack.top
stack.top = stack.top -> next
recycle(temp)
end
end
stack.count = 0
return
End destroyStack

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 36 / 86
Destroy Stack

template <c l a s s List_ItemType>


v o i d S t a c k <L i s t _ I t e m T y p e > : : C l e a r ( ) {
Node<L i s t _ I t e m T y p e >∗ temp ;
w h i l e ( t h i s −>t o p != NULL) {
temp = t h i s −>t o p ;
t h i s −>t o p = t h i s −>top−>n e x t ;
d e l e t e temp ;
}
t h i s −>c o u n t = 0 ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 37 / 86
isEmpty Linked Stack

Algorithm isEmpty(ref stack <metadata>)


Determines if the stack is empty
Pre: stack is a metadata structure to a valid stack
Post: return stack status
Return true if the stack is empty, false otherwise
if count = 0 then
Return true
else
Return false
end
End isEmpty

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 38 / 86
isEmpty Linked Stack

template <c l a s s List_ItemType>


i n t S t a c k <L i s t _ I t e m T y p e > : : I s E m p t y ( ) {
r e t u r n ( c o u n t == 0 ) ;
}

template <c l a s s List_ItemType>


i n t S t a c k <L i s t _ I t e m T y p e > : : G e t S i z e ( ) {
r e t u r n count ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 39 / 86
isFull Linked Stack

template <c l a s s List_ItemType>


i n t S t a c k <L i s t _ I t e m T y p e > : : I s F u l l ( ) {
Node<L i s t _ I t e m T y p e >∗ pNew =
new Node<L i s t _ I t e m T y p e > ( ) ;

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

template <c l a s s List_ItemType>


v o i d S t a c k <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 ( ) {
Node<L i s t _ I t e m T y p e >∗ p ;
p = t h i s −>t o p ;
w h i l e ( p != NULL) {
c o u t << p−>d a t a << " ␣ " ;
p = p−>n e x t ;
}
c o u t << e n d l ;
}

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.

1. Initialy top = -1;


2. push operation increases top by one and writes pushed element to storage[top];
3. pop operation checks that top is not equal to -1 and decreases top variable by 1;
4. getTop operation checks that top is not equal to -1 and returns storage[top];
5. isEmpty returns boolean if top == -1.

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

• Reversing data items


• Reverse a list
• Convert Decimal to Binary
• Parsing
• Brackets Parse
• Postponement of processing data items
• Infix to Postfix Transformation
• Evaluate a Postfix Expression
• Backtracking
• Goal Seeking Problem
• Knight’s Tour
• Exiting a Maze
• Eight Queens Problem

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.

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


FIFO: The first item stored in the queue is the first item that can be taken out.

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

Queue node structure

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 ;
};

template <c l a s s List_ItemType>


c l a s s Queue {
public :
Queue ( ) ;
~Queue ( ) ;

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

Algorithm createQueue(ref queue <metadata>)


Initializes the metadata of a queue
Pre: queue is a metadata structure of a queue
Post: metadata initialized

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

template <c l a s s List_ItemType>


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

template <c l a s s List_ItemType>


Queue<L i s t _ I t e m T y p e > : : ~ Queue ( ) {
t h i s −>C l e a r ( ) ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 62 / 86
Enqueue: Insert into an empty queue

Figure 7: 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

Figure 8: Insert into a queue with data

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 64 / 86
Enqueue

Algorithm enqueue(ref queue <metadata>, val data <dataType>)


Inserts one item at the rear of the queue

Pre: queue is a metadata structure of a valid queue


data contains data to be inserted into queue

Post: data have been inserted in queue


Return true if successful, false if memory overflow

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 65 / 86
Enqueue

if queue full then


return false
end
allocate (newPtr)
newPtr -> data = data
newPtr -> next = null
if queue.count = 0 then
queue.front = newPtr // Insert into an empty queue
else
queue.rear -> next = newPtr // Insert into a queue with data
end
queue.rear = newPtr
queue.count = queue.count + 1
return true
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 66 / 86
Enqueue

template <c l a s s List_ItemType>


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 ){
Node<L i s t _ I t e m T y p e >∗ newPtr = new Node<L i s t _ I t e m T y p e > ( ) ;
newPtr−>d a t a = v a l u e ;
newPtr−>n e x t = NULL ;
i f ( t h i s −>c o u n t == 0 )
t h i s −>f r o n t = newPtr ;
else
t h i s −>r e a r −>n e x t = newPtr ;
t h i s −>r e a r = newPtr ;
t h i s −>c o u n t ++;
}

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

Figure 9: 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

Algorithm dequeue(ref queue <metadata>, ref dataOut <dataType>)


Deletes one item at the front of the queue and returns its data to caller

Pre: queue is a metadata structure of a valid queue


dataOut is to receive dequeued data

Post: front data have been returned to caller


Return true if successful, false if memory overflow

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 70 / 86
Dequeue

if queue empty then


return false
end
dataOut = queue.front -> data
dltPtr = queue.front
if queue.count = 1 then
// Delete data in a queue with only one item
queue.rear = NULL
end
queue.front = queue.front -> next
queue.count = queue.count - 1
recycle (dltPtr)
return true
End dequeue
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 71 / 86
Dequeue

template <c l a s s List_ItemType>


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 &d a t a O u t ) {
i f ( c o u n t == 0 )
return 0;
d a t a O u t = f r o n t −>d a t a ;
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 ;
i f ( c o u n t == 1 )
t h i s −>r e a r = NULL ;
t h i s −>f r o n t = t h i s −>f r o n t −>n e x t ;
t h i s −>c o u n t −−;
delete dltPtr ;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 72 / 86
Queue Front

template <c l a s s List_ItemType>


i n t Queue<L i s t _ I t e m T y p e > : : 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 f ( c o u n t == 0 )
return 0;
d a t a O u t = t h i s −>f r o n t −>d a t a ;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 73 / 86
Queue Rear

template <c l a s s List_ItemType>


i n t Queue<L i s t _ I t e m T y p e > : : GetQueueRear ( L i s t _ I t e m T y p e &d a t a O u t ) {
i f ( c o u n t == 0 )
return 0;
d a t a O u t = t h i s −>r e a r −>d a t a ;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 74 / 86
Destroy Queue

Algorithm destroyQueue(ref queue <metadata>)


Deletes all data from a queue

Pre: queue is a metadata structure of a valid queue

Post: queue empty and all nodes recycled

Return nothing

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 75 / 86
Destroy Queue

if queue not empty then


while queue.front not null do
temp = queue.front
queue.front = queue.front->next
recycle(temp)
end
end
queue.front = NULL
queue.rear = NULL
queue.count = 0
return
End destroyQueue

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 76 / 86
Destroy Queue

template <c l a s s List_ItemType>


v o i d Queue<L i s t _ I t e m T y p e > : : C l e a r ( ) {
Node<L i s t _ I t e m T y p e >∗ temp ;
w h i l e ( t h i s −>f r o n t != NULL) {
temp = t h i s −>f r o n t ;
t h i s −>f r o n t= t h i s −>f r o n t −>n e x t ;
d e l e t e temp ;
}
t h i s −>f r o n t = NULL ;
t h i s −>r e a r = NULL ;
t h i s −>c o u n t = 0 ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 77 / 86
Queue Empty

template <c l a s s List_ItemType>


i n t Queue<L i s t _ I t e m T y p e > : : I s E m p t y ( ) {
r e t u r n ( t h i s −>c o u n t == 0 ) ;
}

template <c l a s s List_ItemType>


i n t Queue<L i s t _ I t e m T y p e > : : G e t S i z e ( ) {
r e t u r n t h i s −>c o u n t ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 78 / 86
Print Queue

template <c l a s s List_ItemType>


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 ( ) {
Node<L i s t _ I t e m T y p e >∗ p ;
p = t h i s −>f r o n t ;
c o u t << " F r o n t : ␣ " ;
w h i l e ( p != NULL) {
c o u t << p−>d a t a << " ␣ " ;
p = p−>n e x t ;
}
c o u t << e n d l ;
}

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

You might also like