Docsity

Queues, Thesis of Discrete Mathematics

Partial preview of the text

Download Queues and more Thesis Discrete Mathematics in PDF only on Docsity!

FAST- National University of

Computer and Emerging Sciences

CFD Campus

Data Structures

Queues

Zain Iqbal [email protected]

Lecture No. 9

Queues

Queues

A queue is like a line of people waiting for a

bank teller. The queue has a front and a rear.

$ $

Rear Front

Queues

New people must enter the queue at the rear.

$ $

Front

Rear

Some examples

  • Billing counter
    • Booking movie tickets
    • Queue for paying bills
  • A print queue
  • Vehicles on toll-tax bridge
  • Luggage checking machine

8

Applications of Queues

  • Operating system
    • multi-user/multitasking environments, where several
users or task may be requesting the same resource
simultaneously.
  • Communication Software
    • queues to hold information received over networks
and dial up connections. (Information can be
transmitted faster than it can be processed, so is
placed in a queue waiting to be processed)

10

11

Implementation

  • Static
    • Queue is implemented by an array, and size

of queue remains fix

  • Dynamic
    • A queue can be implemented as a linked

list , and expand or shrink with each enqueue

or dequeue operation.

13

Array Implementation

  • Use two counters that signify rear and front First Element Last Element maxlength Front Second Element. . Rear
When queue is empty both front and rear are set to - 1
While enqueueing increment rear by 1, and while
dequeueing increment front by 1
When there is only one value in the Queue, both rear
and front have same index

14

Array Implementation

5 4 6 7 8 7 6 0 1 2 3 4 5 6 7 8 Front= Rear= 8 7 6 0 1 2 3 4 5 6 7 8 Front= Rear= 7 6 12 67 0 1 2 3 4 5 6 7 8 Front= Rear= How can we insert more elements? Rear index can not move beyond the last element….

7 6 12 67 0 1 2 3 4 5 6 7 8 Front= Rear= Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0 39 7 6 12 67 0 1 2 3 4 5 6 7 8 Front= Rear=

17

How to determine empty and full Queues?

  • It can be somewhat tricky
  • Number of approaches
    • A counter indicating number of values in the queue
can be used (We will use this approach)
  • We will see another approach as well at the end

19 IntQueue::IntQueue(int s) //constructor { queueArray = new int[s]; queueSize = s; front = - 1; rear = - 1; numItems = 0; } IntQueue::~IntQueue(void) //destructor { delete [] queueArray; }

20

// Function enqueue inserts the value in num *
// at the rear of the queue. *
void IntQueue::enqueue(int num)
if (isFull())
cout << "The queue is full.\n";
else
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;