Lecture 12 ADT Queue - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Lecture 12 ADT Queue

Description:

new elements are added at one end (the 'rear') and elements are removed. from the other end (the 'front'). Also called FIFO (First In, First Out) queue. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 27
Provided by: nikolan
Category:
Tags: adt | end | lecture | queue | rear

less

Transcript and Presenter's Notes

Title: Lecture 12 ADT Queue


1
Lecture 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

2
Specifications 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
3
Implementing 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
4
Constructor (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)
5
Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem
copy
newItem
newNodePtr
6
Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL
newItem
newNodePtr
7
Enqueue
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
8
Enqueue
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
9
Enqueue (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
10
Enqueue (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
11
Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo
qFront
copy
item
tempPtr
12
Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext

qFront
item
tempPtr
13
Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext
delete tempPtr
qFront
item
tempPtr
14
Dequeue (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
15
Dequeue (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
16
Dequeue (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
17
MakeEmpty
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
MakeEmpty() NodeTypeltItemTypegt tempPtr
while (qFront ! NULL) tempPtr qFront
qFront qFront-gtnext delete tempPtr
qRear NULL
templateltclass ItemTypegtQueueTypeltItemTypegtQue
ueType() MakeEmpty()
18
IsFull
templateltclass ItemTypegtbool QueueTypeltItemTypegt
IsFull() NodeTypeltItemTypegt ptr ptr
new NodeTypeltItemTypegt if (ptr NULL)
return true else delete ptr return
false
19
Shallow 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
20
Shallow 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
21
Shallow 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
22
Shallow Copy (more)
template ltclass ItemTypegtclass QueueType
private NodeTypeltItemTypegt qFront
NodeTypeltItemTypegt qRear QueueType q1,
q2
23
Shallow 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
24
Solution 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
25
Copy 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)
26
Copy 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.
Write a Comment
User Comments (0)
About PowerShow.com