Upload
abdur-rehman-usmani
View
94
Download
0
Embed Size (px)
Citation preview
GROUP MEMBERSNames Roll No.Fatima Saqib 39Bilal Younus 10Areesha Butt 58Muhammad Fahim 42Poshma Kiyani 02Abdur Rehman 41
QUEUEA queue is two ended data structure in which items can be inserted from one end and taken out from the other end. Therefore , the first item inserted into queue is the first item to be taken out from the queue. This property is called First in First out [FIFO].e.g: the picture given below:
Queue Example ● A queue of people in the bank who are trying to submit utility bills. ● A line of vehicle on the motorway toll plaza. ● Consider a shared printer in a computer laboratory. Student can send their print request to the printer and each print request goto queue and serve one by one. ● Computer input operations performing through mouse and keyboard are also recorded into queue and serve on the basis of FIFO.
Deque stands for double-end queueA data structure in which elements can be added or deleted at either the front or rear.But no changes can be made in the listDeque is generalization of both stack and queue.
Deque
Variations In Deque:
There are two variations of a deque. Input Restricted Deque Output Restricted Deque
An input restricted deque restricts the insertion of elements at one end only, but the deletion of elements can perform at
both the ends.
Input Restricted Deque
An output restricted queue, restricts the deletion of elements at one end only, and
allows insertion to be done at both the ends of deque
Output Restricted Deque
Possibilities The two possibilities that must be considered while inserting or deleting elements into the queue are : When an attempt is made to insert an element into a deque which is already full, an overflow occurs. When an attempt is made to delete an element from a deque which is empty, underflow occurs
Deque
The Queue Operations• A queue is like a line of people
waiting for a bank teller. The queue has a front and a rear.
$ $
Front
Rear
The Queue Operations
• New people must enter the queue at the rear. • The C++ queue class calls this a push,
although it is usually called an enqueue operation.
$ $
FrontRear
The Queue Operations
• When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation.
$ $
FrontRear
RearRear is the tail/bottom of the queue. The
entry at rear is the most recently arrived item and must wait until the other entries are
present in the queue.
Insert (Enqueue)
Remove(Dequeu) rearfront
FRONTFront is the position from where items can be
taken out from queue. It is also termed as head or top of the queue. The entry at the
front position is ready to be served
Insert (Enqueue)
Remove(Dequeu) rearfront
APPENDThe operation to add an item at the rear
position of the queue is termed as append or enqueue. We can append items at the rear position of the queue until the queue is not full. The rear points towards the position
where new item has been appended.
17 23 97 44 333After insertion:
17 23 97 44Initial queue:
Serve The operation to remove an item from the front position of the queue is termed as
serve or dequeue. We can serve the items in the queue until the queue is not empty. The front points towards the position from where
the available item can be severed
SERVE
Queue is consider empty when there is no item on front of the queue. IsEmpty operation checks for this condition and return true if the queue is empty else
return false.
IsEmpty
0 1 2 3 4 5 6 7myQueue:
If no element can be appended at rear of
the queue then queue consider as full. When rear of the queue reaches this limit, then queue is full and the IsFull operation
return true else return false
IsFull
44 55 66 77 88 11 22 33
0 1 2 3 4 5 6 7myQueue:
Array Implementation of Queue• When enqueuing, the front index is always fixed and
the rear index moves forward in the array.• There are several different algorithms to implement
Enqueue and Dequeue
front
rear
Enqueue(3)
3
front
rear
Enqueue(6)
3 6
front
rear
Enqueue(9)
3 6 9
Queue Implementation of Array• When enqueuing, the front index is always fixed
and the rear index moves forward in the array.• When dequeuing, the element at the front of the
queue is removed. Move all the elements after it by one position.
Dequeue()
front
rear
6 9
Dequeue() Dequeue()
front
rear
9
rear = -1
front
Queue Implementation of Array
• Better way• When an item is enqueued, make the rear
index move forward.• When an item is dequeued, the front index
moves by one element towards the back of the queue (thus removing the front item).
Array Implementation....• A queue can be implemented with an array.
For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear).
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . ..
An array of integers to implement a queue of integers
4 8 6
Array Implementation....• The easiest implementation also
keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear).
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .
4 8 6
size3
first0
last2
..Array implementation of queues
• A queue is a first in, first out (FIFO) data structure• This is accomplished by inserting at one end (the
rear) and deleting from the other (the front)
• To insert: put new element in location 4, and set rear to 4
• To delete: take element from location 0, and set front to 1
28
17 23 97 44
0 1 2 3 4 5 6 7myQueue:
rear = 3front = 0
…Array implementation of queues
• Notice how the array contents “crawl” to the right as elements are inserted and deleted
• This will be a problem after a while!
29
17 23 97 44 333After insertion:
23 97 44 333After deletion:
rear = 4front = 1
17 23 97 44Initial queue:
rear = 3front = 0
Representation of queue by program
• How to insert Value• How to delete value• How to print value• Complete Program Example
To Insert ValueAlgorithm:R=F=-1 IF R >= SIZE THEN
PRINT ‘’Overflow’’RETURN
ELSE {R=R+1 [Insert Item]
Q[R]=‘’item’’ IF F=-1 THEN F=0 } END IF
Algorithm: If ( FRONT = -1 ) then print "Queue is empty" Exit Else ITEM = Que [ FRONT ] If ( FRONT = REAR ) REAR = -1 FRONT = -1 Else FRONT = FRONT + 1 Stop
To Delete Value