Title: Lecture 12 ADT Queue
1Lecture 12ADT Queue
Logical Level
A queue is an ordered, homogeneous group of
elements (items) in which new elements are added
at one end (the rear) and elements are
removedfrom the other end (the front). Also
called FIFO (First In, First Out) queue.
- Operations performed on a queue
- Enqueue(item) adds item to the rear of the
queue - Dequeue() -gt item takes item off the front of
the queue - IsEmpty() -gt bool checks whether the queue is
empty - IsFull() -gt bool checks whether the queue is
full - MakeEmpty() empties the queue
2Specifications for ADT Queue
ifndef QUEUE_TYPE_Hdefine QUEUE_TYPE_H
templateltclass ItemTypegt class
QueueTypepublic QueueType() // constructs
an empty queue QueueType() void
MakeEmpty() bool IsFull() const bool
IsEmpty() const void Enqueue(ItemType
newItem) void Dequeue(ItemType
item)private endif
QueueType.h
templateltclass ItemTypegtstruct NodeType
NodeTypeltItemTypegt qFrontNodeTypeltItemTypegt
qRear
3Implementing Queue by a Chain of Linked Nodes in
the Heap
queue item
pointertonode
A single node in the heap
templateltclass ItemTypegtstruct NodeType
ItemType info NodeType next
goes to QueueType.cc
A chain of linked nodes in the heap
4Constructor (creates an empty queue)
templateltclass ItemTypegtQueueTypeltItemTypegtQueu
eType() qFront NULL qRear NULL
qRear
qFront
IsEmpty (checks whether the queue is empty)
templateltclass ItemTypegtbool QueueTypeltItemTypegt
IsEmpty() const return (qRear NULL)
5Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem
copy
newItem
newNodePtr
6Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL
newItem
newNodePtr
7Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr
newItem
newNodePtr
8Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr qRear newNodePtr
newItem
newNodePtr
9Enqueue (inserting an item into an empty queue)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr qRear newNodePtr
qRear
qFront
newNodePtr
10Enqueue (inserting an item into an empty queue)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr qRear newNodePtr
qFront
newNodePtr
11Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo
qFront
copy
item
tempPtr
12Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext
qFront
item
tempPtr
13Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext
delete tempPtr
qFront
item
tempPtr
14Dequeue (removing the last node)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext if
(qFront NULL) qRear NULL delete
tempPtr
qFront
qRear
item
tempPtr
15Dequeue (removing the last node)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext if
(qFront NULL) qRear NULL delete
tempPtr
qFront
qRear
item
tempPtr
16Dequeue (removing the last node)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext if
(qFront NULL) qRear NULL delete
tempPtr
qFront
qRear
item
tempPtr
17MakeEmpty
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
MakeEmpty() NodeTypeltItemTypegt tempPtr
while (qFront ! NULL) tempPtr qFront
qFront qFront-gtnext delete tempPtr
qRear NULL
templateltclass ItemTypegtQueueTypeltItemTypegtQue
ueType() MakeEmpty()
18IsFull
templateltclass ItemTypegtbool QueueTypeltItemTypegt
IsFull() NodeTypeltItemTypegt ptr ptr
new NodeTypeltItemTypegt if (ptr NULL)
return true else delete ptr return
false
19Shallow Copy
class SomeType public int x float y
int w2 SomeType a, b a.x 1 a.y
2.0 a.w0 200 a.w1 201 b a
20Shallow Copy
class SomeType public int x float y
int w2 SomeType a, b a.x 1 a.y
2.0 a.w0 200 a.w1 201 b a
Memory
x 1y 2.0w 200 201
Object a
x y w
Object b
21Shallow Copy
class SomeType public int x float y
int w2 SomeType a, b a.x 1 a.y
2.0 a.w0 200 a.w1 201 b a
Memory
x 1y 2.0w 200 201
Object a
x 1y 2.0w 200 201
Object b
22Shallow Copy (more)
template ltclass ItemTypegtclass QueueType
private NodeTypeltItemTypegt qFront
NodeTypeltItemTypegt qRear QueueType q1,
q2
23Shallow Copy (more)
template ltclass ItemTypegtclass QueueType
private NodeTypeltItemTypegt qFront
NodeTypeltItemTypegt qRear QueueType q1,
q2 q2 q1
This operation could be dangerous!!!
Memory
qRear
qFront
q1
qFront
qRear
q2
Heap
24Solution deep copy by overloading the operator
Specification in QueueType.hvoid
operator(QueueTypeltItemTypegt otherQueue)
Implementation in QueueType.cc template ltclass
ItemTypegt void QueueTypeltItemTypegtoperator(Que
ueTypeltItemTypegt otherQueue)
NodeTypeltItemTypegt tmpPtr otherQueue.qFront
MakeEmpty() while (tmpPtr ! NULL)
Enqueue(tmpPtr-gtinfo) tmpPtr
tmpPtr-gtnext
This function performs deep copy, i.e. it not
only copies one class object to another but also
makes copies of any pointed-to data
25Copy constructor
- In order to be able to
- make initialization in a variable declaration
QueueTypeltintgt q2 q1 - have functions whose parameters are objects of
class QueueType passed by value void
operator(QueueTypeltItemTypegt otherQueue) - have functions which return objects of type
QueueType return q2
we need to define and implement a copy
constructor of the type ClassName(const
ClassName anotherObject)
26Copy constructor (more)
Specification in QueueType.hQueueType(const
QueueTypeltItemTypegt otherQueue)
Implementation in QueueType.cc template ltclass
ItemTypegt QueueTypeltItemTypegtQueueType(const
QueueTypeltItemTypegt otherQueue)
NodeTypeltItemTypegt tmpPtr otherQueue.qFront
qFront NULL qRear NULL while (tmpPtr !
NULL) Enqueue(tmpPtr-gtinfo) tmpPtr
tmpPtr-gtnext
The rule of the big 3 If one of the three
class destructor, copy constructor, or
overloaded operator is necessary, then most
likely all three are necessary.