0% found this document useful (0 votes)
9 views63 pages

Chapter-7 QUEUESds

Uploaded by

vinaydarling063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views63 pages

Chapter-7 QUEUESds

Uploaded by

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

Chapter - 7

QUEUES
The linear lists and linear arrays allow inserting and deleting elements at any place in the list i.e.
at the beginning, at the end, or in the middle. There are certain frequent situations when one
wants to restrict insertions and deletions so that they can take place only at the beginning or at
the end of the list, not in the middle. One of the data structures that are useful in such situations
is a Queue.

QUEUE

A Queue is a linear list of elements in which data can be inserted at one end, called the rear, and
deleted from the other end, called the front. Queues are also called first in–first out (FIFO) lists,
since the first element in a queue will be the first element out of the queue.

Examples

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 1


Representation of Queues in memory
Queues may be represented in the computer in various ways:

1) Array representation

2) Linked list representation

1) Array representation of Queues

The queue will be maintained by a linear array QUEUE and two pointer variables: FRONT,
containing the location of the front element of the queue and REAR, containing the location of
the rear element of the queue.

The condition FRONT = NULL will indicate that the queue is empty.

FRONT : 1

REAR : 4

1 2 3 4 5 6 7 … N

2) Linked lists representation of Queues

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 2


Implementation of Queues using Arrays

The queue will be maintained by a linear array QUEUE and two pointer variables: FRONT,
containing the location of the front element of the queue and REAR, containing the location of
the rear element of the queue.

Operations on Queues using Arrays

Inserting an element into a Queue using Arrays

QINSERT(QUEUE,N,FRONT,REAR, ITEM)

// This algorithm inserts an element ITEM into a queue.

1) If FRONT = 1 AND REAR = N or If FRONT = REAR + 1 then

Print “ overflow “and Return.

2) // Inserting a new element into the empty queue

IF FRONT : = NULL, then

Set FRONT : = 1 AND REAR : = 1

ELSE

3) If REAR = N, then: Set REAR:= 1

ELSE

4) Set REAR := REAR + 1

} // End of If

5) // Inserts new element

Set QUEUE[REAR] := ITEM

6) Return.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 3


Complexity

Time complexity: O(n)

Space complexity: O(n)

Deleting an element from a Queue using Arrays

QDELETE(QUEUE,N,FRONT,REAR,ITEM)

// This algorithm deletes an element from a queue.

1) If FRONT := NULL, then Print “UNDERFLOW”

Return

2) Set ITEM := QUEUE[FRONT]

3) // Queue has only one element to start.

IF FRONT = REAR, then set FRONT := NULL AND REAR := NULL.

ELSE

IF FRONT = N, then set FRONT := 1

Else

Set FRONT := FRONT + 1

}// End of If

4) Return.

Complexity

Time complexity: O(n)

Space complexity: O(n)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 4


Implementation of Queues using Linked lists

ADT type definitions for Queue

QUEUE ADT TYPE DEFINITIONS

typedef struct node

int data;

struct node *next;

};

typedef struct QUEUE

QUEUE *front;

QUEUE *rear;

int count;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 5


};

Operations on Queues using Linked lists

 Create queue
 Enqueue
 Deletequeue
 Queuefront
 Queuerear
 Empty queue
 Full queue
 Queue count
 Destroy queue

Create queue

Create queue allocates a node for the queue header. It then initializes the front and rear pointers
to null and sets the count to zero. If overflow occurs, the return value is null. If the allocation is
successful, it returns the address of the queue head.

Algorithm QUEUE *createQueue (void)

// Allocates memory for a queue head node from dynamic memory

/ / Local Declarations

QUEUE *queue;

1) // Allocate memory for the queue head

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 6


queue = (QUEUE * ) malloc(sizeof (QUEUE));

2) // Is memory available

If (queue)

3) // Set queue front to null

queue àfront = NULL;

4) // Set queue rear to null

queue àrear = NULL;

5) // Set queue count to null

queue àcount = 0 ;

} // End of if

return queue;

} // End of createqueue

Enqueue

Enqueue operation is used to insert an element into the queue. When we insert data into a queue
with data in it, we must point both the link field in the last node and the rear pointer to the new

node.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 7


Algorithm Enqueue(QUEUE *queue, int newnode)

// This algorithm inserts data into a queue.

QUEUE *newnode;

1) newnode = (QUEUE * )malloc(sizeof(QUEUE));

2) / * Allocate new node * /

newnode àdata= num;

3) / * Inserting into an empty queue * /

if (queue àcount==0)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 8


4) / * set queue front to the address of new data * /

queue àfront = newnode;

queueàrear = newnode;

newnodeànext = null;

else

5) / * point old rear to new node * /

queue à rear à next = newnode;

(queue àcount) ++;

} / * End of if * /

6) / * Set queue rear to the address of new node * /

queue àrear = newnode;

return 1 ;

} / * End of enqueue * /

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deletequeue

While deleting an element from a queue we must first ensure that the queue contains data. Given
that there are data to be deleted, we pass the data back through the parameter list and then set the
front pointer to the next item in the queue. The node that is deleted is recycled.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 9


Algorithm Deletequeue(QUEUE *head, int item)

// This algorithm deletes a node from the queue.

/ * check whether queue is empty * /

1) if (queueàcount == 0)

return 0;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 10


2) / * Move front data to item * /

item= queue àfront àdata;

3) / * Deleting only item in the queue * /

if (queue àcount == 1)

queue àrear = queue àfront = NULL;

else

4) / * set queue front to queue next * /

queue àfront = queue àfront ànext;

5) / * Decrement the queue count * /

(queue àcount)--;

free(item);

return 1;

} / * End of Deletequeue * /

Complexity

Time complexity: O(n)

Space complexity: O(n)

Queuefront

Queue front retrieves the element at the front of the queue.

Algoritm Queuefront(QUEUE *queue, int item)

// This algorithm retrieves the data at the front of the queue without changing the queue contents.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 11


1) / * check whether queue is empty * /

if (queueàcount == 0)

return 0;

else

2) / * Move data at front of the queue to dataout /

item= queueàfront àdata;

return(item);

} / *end of else */

} / * end of queueFront * /

Queuerear

The queuerear retrieves the data at the rear of the queue.

Algoritm QueueRear(QUEUE *queue, int item)

// This algorithm retrieves the data at the rear of the queue without changing the queue contents.

1) / * check whether queue is empty * /

If (queueàcount ==0)

return 0;

else

2) / * Move data at rear of the queue to dataOut * /

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 12


item = queueàrear àdata;

return (item);

} / *end of else */

} / * end of queueRear * /

QueueEmpty

It simply uses the count in the queue header to determine if the queue is empty or contains data.

Algoritm Emptyqueue(QUEUE *queue)

// This algorithm checks to see if a queue is empty.

return(queueàcount == 0);

} / * End of emptyqueue * /

FullQueue

Checks to see if a queue is full. If the queue is full if memory cannot be allocated for another
node.

Algoritm fullQueue(QUEUE *queue)

// This algorithm checks to see if a queue is full. The queue is full if memory cannot be
allocated // for another node.

QUEUE *temp;

/ * check if memory is available * /

1) If ( ( temp = (QUEUE *)malloc(sizeof(QUEUE));

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 13


{

free (temp);

return 0;

else

return 1;

Queue count

Queue count returns the number of elements in the queue. Queue count simply returns the count
found in the queue head node.

Algoritm queueCount (QUEUE *queue)

// This algorithm returns the number of elements in the queue.

return (queueàcount);

Destroy Queue

The Destroy Queue operation deletes all the elements in the queue and frees each of their nodes.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 14


Algorithm Destroyqueue (QUEUE *queue)

// This algorithm deletes all data from a queue, then deletes and recycles queue head pointer.

QUEUE * queue;

1) if (queue)

2) Repeat steps while (QUEUE àfront != NULL)

temp = QUEUEàfrontàinfo;

QUEUEàfront = QUEUEàfrontànext;

free(temp);

} // End of while

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 15


3) free (QUEUE);

} // End of if

return NULL;

} // End of destroyQueue

TYPES OF QUEUES
Queues are classified into three types

 Circular queues
 Dequeue (Double Ended Queue)
 Priority queues

CIRCULAR QUEUES

A circular queue is a linear collection of elements in which the front and rear elements will be in
successive locations.

Representation of Circular queues in memory

Circular queues may be represented in the computer in various ways:

1) Array representation

2) Linked list representation

Array Representation of Circular queues

If we are representing the queue using arrays, then a queue with n elements starts from index 0
and ends at n – 1.The first element in the queue is at index 0 and the last element will be at index
n – 1.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 16


When a new element is added and rear is pointing at n – 1, then it needs to be checked if the
position at index 0 is free. If yes, then the element can be added to that position and rear can be
adjusted accordingly. In this way the efficient utilization of space is increased in the case of a
circular queue.

Implementation of Circular queues using Arrays

The queue will be maintained by a linear array QUEUE and two pointer variables: FRONT,
containing the location of the front element of the queue and REAR, containing the location of
the rear element of the queue.

Operations on Queues using Arrays

Inserting an element into a Circular Queue using Arrays

CQINSERT(Front,Rear,Queue,N,Y)

1) / * Reset rear pointer * /

IF REAR = N

then REAR = 1;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 17


2) / * Check for overflow * /

If Front = Rear

then print (“overflow”);

return 0;

else

3) Rear = Rear + 1;

/ * Insert element * /

Queue[Rear] = y;

4) / * IS Front pointer properly set * /

If Front = 0

then FRONT = 1;

return 0;

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deleting an element from a Circular queue using Arrays

Algorithm cqdelete(Front,Rear,Queue,N,Y)

1) / * Check for underflow * /

If Front = 0

then print(“Underflow”);

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 18


return 0;

2) / * Delete element * /

Y=Queue[Front];

3) / * Check queue empty * /

If Front = Rear =NULL

then Front = 1

else

Front =Front + 1

return(Y);

Implementation of Circular queues using Linked lists

Inserting an element into a Circular Queue using Linked lists

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 19


Algorithm Insertcqueue(QUEUE * queue, int value)

QUEUE *newnode, current;

1) newnode = (QUEUE *)malloc(sizeof(QUEUE));

2) newnode à data = value;

3) newnode à next = NULL;

4) If (QUEUE àfront ==NULL)

QUEUE à front =QUEUEàrear = newnode;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 20


else

5) Repeat steps while(currentànext!=front)

current = current ànext;

current à next = newnode;

QUEUEàrear = newnode;

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deleting an element into a Circular Queue using Linked lists

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 21


Algorithm Deletecqueue(QUEUE * queue, int item)

1)If (QUEUEàfront == Null)

print (“queue is empty”);

2) /* delete first element */

item = QUEUEàfront àvalue;

3) if (QUEUUEàfront -> next == null)

free(QUEUEàfront);

QUEUEàfront = QUEUEàrear = Null;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 22


else

4) QUEUEàfront = QUEUEàfront à next;

free(item);

Complexity

Time complexity: O(n)

Space complexity: O(n)

DOUBLE ENDED QUEUES[DEQUEUES]

A Dequeue (Double ended queue) is an abstract data type similar to queue, where addition and
deletion of elements are allowed at both the ends. A Dequeue can be implemented using Arrays
or linked lists.

Implementation of Double –Ended queues using Arrays

Insertion at the Rear

Algorithm add_rear(dq, front, rear, int y)

1) If (front == Queue_Length – 1)

Print (“Element cannot be added at the rear”)

return 0;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 23


else

2) Rear = Rear + 1;

3) dq[rear] = y;

4) If (rear == - 1)

Rear = 0;

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deletion at the Front

Algorithm delete_front(dq, front, rear, int y)

1) If Front == -1

Print(“Queue empty”);

2) else

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 24


Return dq[front];

3) If (front == rear)

Front = rear = -1

Else

4) Front = front + 1;

Insertion at the Front

1) / * Addition at the front */

Algorithm add_front(dq,front, rear, int y)

1) If (front ==0)

Print(“Element cannot be added at the front”)

return 0;

else

2) Front = front – 1

3) dq[front] = y;

4) If (Front = - 1) then

Front = 0

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 25


}

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deletion at the Rear

Algorithm delete_rear(dq, front, rear, int item)

1)If rear == - 1

Print (“ Deletion is not possible from rear”);

else

2) If (front == rear)

Front = rear = - 1

else

3) Rear = rear – 1;

4) return(item = dq[rear]) ;

Complexity

Time complexity: O(n)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 26


Space complexity: O(n)

Double ended queue is represented in two ways:

1) Input restricted Double ended queue


2) Output restricted Double ended queue

1) Input restricted Double ended queue

In Input restricted double ended queue, the insertion operation is performed only at one end and
deletion operation is performed at both the ends.

Implementation of Input restricted Double –Ended queues using Arrays

Insertion at the Rear

Algorithm add_rear(dq, front, rear, int y)

1) If (front == Queue_Length – 1)

Print (“Element cannot be added at the rear”)

return 0;

else

2) Rear = Rear + 1;

3) dq[rear] = y;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 27


4) If (rear == - 1)

Rear = 0;

Deletion at the Front

Algorithm delete_front(dq, front, rear, int y)

1) If Front == -1

Print(“Queue empty”);

2) else

Return dq[front];

3) If (front == rear)

Front = rear = -1

Else

4) Front = front + 1;

Deletion at the Rear

Algorithm delete_rear(dq, front, rear, int item)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 28


1)If rear == - 1

Print (“ Deletion is not possible from rear”);

else

2) If (front == rear)

Front = rear = - 1

else

3) Rear = rear – 1;

4) return(item = dq[rear]) ;

Output restricted Double ended queue

In Output restricted double ended queue, the deletion operation is performed only at one end and
insertion operation is performed at both the ends.

Implementation of Output restricted Double –Ended queues using Arrays

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 29


Insertion at the Rear

Algorithm add_rear(dq, front, rear, int y)

1) If (front == Queue_Length – 1)

Print (“Element cannot be added at the rear”)

return 0;

else

2) Rear = Rear + 1;

3) dq[rear] = y;

4) If (rear == - 1)

Rear = 0;

Insertion at the Front

/ * Addition at the front */

Algorithm add_front(dq,front, rear, int y)

1) If (front ==0)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 30


Print(“Element cannot be added at the front”)

return 0;

else

2) Front = front – 1

3) dq[front] = y;

4) If (Front = - 1) then

Front = 0

Deletion at the Front

Algorithm delete_front(dq, front, rear, int y)

1) If Front == -1

Print(“Queue empty”);

2) else

Return dq[front];

3) If (front == rear)

Front = rear = -1

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 31


Else

4) Front = front + 1;

Implementation of Double –Ended queues using Linked lists

Insertion at the Rear

Algorithm int Enqueuerear(QUEUE *queue, int element)

// This algorithm inserts data into a queue.

// Local Declarations

QUEUE *newnode;

1) // Dynamically create newnode

newnode = (QUEUE * ) malloc(sizeof(QUEUE));

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 32


2) // Allocate new node and move data into the new node

newnodeàdata = info;

newnode ànext = NULL;

3) // Inserting into an empty queue

If (QUEUEàcount == 0)

QUEUE àfront = newnode;

QUEUE àrear = newnode;

else

4) QUEUE à rear àlink = newnode;

5) newnodeàlink = NULL;

6) // Set queue rear to the address of new node

QUEUE àrear = newnode;

7) QUEUE àcount) ++;

return 1 ;

} End of if

} // End of enqueue

Complexity

Time complexity: O(n)

Space complexity: O(n)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 33


Deletion at the Front

Algorithm int Deletequeuefront(QUEUE *queue, int item)

// This algorithm deletes a node from the queue.

// Local Declarations

QUEUE *temp;

1) // check whether queue is empty

If (QUEUE count = 0)

return 0;

2) // Move front data to item

item = QUEUEàfront àinfo;

3) // Deleting only item in the queue

If (QUEUE àfront = QUEUE  rear) then

QUEUE àrear = QUEUEàfront = NULL;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 34


else

4) // Set queue front to queue next

temp= QUEUEfront;

QUEUE àfront = QUEUE àfront àlink;

(QUEUE àcount)--;

free(temp);

return 1;

} // End of Deletequeue

Complexity

Time complexity: O(n)

Space complexity: O(n)

Insertion at the Front

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 35


Algorithm int Enqueuefront(QUEUE *queue)

// This algorithm inserts data into a queue.

// Local Declarations

QUEUE *newnode;

1) newnode = (QUEUE *) malloc(sizeof(QUEUE));

2) // Allocate new node and move data into the new node

newnodeàdata = info;

newnode ànext = NULL;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 36


3) // Inserting into an empty queue

If (QUEUEàcount == 0)

QUEUE àfront = newnode;

QUEUE àrear = newnode;

else

4) QUEUE à front àlink = newnode;

newnodeàlink = QUEUE àfront;

6) // Set queue front to the address of new node

QUEUE àfront = Pnew;

7) (QUEUE àcount) --;

return 1 ;

} // End of if

} // End of enqueue

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deletion at the Rear

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 37


Algorithm int Deletequeuerear(QUEUE *queue, int item)

// This algorithm deletes a node from the queue.

// Local Declarations

QUEUE *temp;

1) // check whether queue is empty

If (QUEUE count = 0)

return 0;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 38


}

2) // Move front data to item

item = QUEUEàrear àinfo;

3) // Deleting only item in the queue

If (QUEUE àrear = QUEUE  front) then

QUEUE àrear = QUEUEàfront = NULL;

else

4) // Set queue rear to queue next

temp= QUEUErear;

QUEUE àrear = QUEUE àrear àlink;

(QUEUE àcount)--;

free(temp);

return 1;

} // End of Deletequeue

Complexity

Time complexity: O(n)

Space complexity: O(n)

Implementation of Input restricted Double –Ended queues using Linked lists

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 39


Insertion at the Rear

Algorithm int Enqueuerear(QUEUE *queue, int element)

// This algorithm inserts data into a queue.

// Local Declarations

QUEUE *newnode;

1) // Dynamically create newnode

newnode = (QUEUE * ) malloc(sizeof(QUEUE));

2) // Allocate new node and move data into the new node

newnodeàdata = info;

newnode ànext = NULL;

3) // Inserting into an empty queue

If (QUEUEàcount == 0)

QUEUE àfront = newnode;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 40


QUEUE àrear = newnode;

else

4) QUEUE à rear àlink = newnode;

5) newnodeàlink = NULL;

6) // Set queue rear to the address of new node

QUEUE àrear = newnode;

7) QUEUE àcount) ++;

return 1 ;

} End of if

} // End of enqueue

Deletion at the Front

Algorithm int Deletequeuefront(QUEUE *queue, int item)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 41


// This algorithm deletes a node from the queue.

// Local Declarations

QUEUE *temp;

1) // check whether queue is empty

If (QUEUE count = 0)

return 0;

2) // Move front data to item

item = QUEUEàfront àinfo;

3) // Deleting only item in the queue

If (QUEUE àfront = QUEUE  rear) then

QUEUE àrear = QUEUEàfront = NULL;

else

4) // Set queue front to queue next

temp= QUEUEfront;

QUEUE àfront = QUEUE àfront àlink;

(QUEUE àcount)--;

free(temp);

return 1;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 42


} // End of Deletequeue

Deletion at the Rear

Algorithm int Deletequeuerear(QUEUE *queue, int item)

// This algorithm deletes a node from the queue.

// Local Declarations

QUEUE *temp;

1) // check whether queue is empty

If (QUEUE count = 0)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 43


return 0;

2) // Move front data to item

item = QUEUEàrear àinfo;

3) // Deleting only item in the queue

If (QUEUE àrear = QUEUE  front) then

QUEUE àrear = QUEUEàfront = NULL;

else

4) // Set queue rear to queue next

temp= QUEUErear;

QUEUE àrear = QUEUE àrear àlink;

(QUEUE àcount)--;

free(temp);

return 1;

} // End of Deletequeue

Implementation of Output restricted Double –Ended queues using Linked lists

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 44


Insertion at the Rear

Algorithm int Enqueuerear(QUEUE *queue, int element)

// This algorithm inserts data into a queue.

// Local Declarations

QUEUE *newnode;

1) // Dynamically create newnode

newnode = (QUEUE * ) malloc(sizeof(QUEUE));

2) // Allocate new node and move data into the new node

newnodeàdata = info;

newnode ànext = NULL;

3) // Inserting into an empty queue

If (QUEUEàcount == 0)

QUEUE àfront = newnode;

QUEUE àrear = newnode;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 45


}

else

4) QUEUE à rear àlink = newnode;

5) newnodeàlink = NULL;

6) // Set queue rear to the address of new node

QUEUE àrear = newnode;

7) QUEUE àcount) ++;

return 1 ;

} End of if

} // End of enqueue

Insertion at the Front

Algorithm int Enqueuefront(QUEUE *queue)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 46


// This algorithm inserts data into a queue.

// Local Declarations

QUEUE *newnode;

1) newnode = (QUEUE *) malloc(sizeof(QUEUE));

2) // Allocate new node and move data into the new node

newnodeàdata = info;

newnode ànext = NULL;

3) // Inserting into an empty queue

If (QUEUEàcount == 0)

QUEUE àfront = newnode;

QUEUE àrear = newnode;

else

4) QUEUE à front àlink = newnode;

newnodeàlink = QUEUE àfront;

6) // Set queue front to the address of new node

QUEUE àfront = Pnew;

7) (QUEUE àcount) --;

return 1 ;

} // End of if

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 47


} // End of enqueue

Deletion at the Front

Algorithm int Deletequeuefront(QUEUE *queue, int item)

// This algorithm deletes a node from the queue.

// Local Declarations

QUEUE *temp;

1) // check whether queue is empty

If (QUEUE count = 0)

return 0;

2) // Move front data to item

item = QUEUEàfront àinfo;

3) // Deleting only item in the queue

If (QUEUE àfront = QUEUE  rear) then

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 48


QUEUE àrear = QUEUEàfront = NULL;

else

4) // Set queue front to queue next

temp= QUEUEfront;

QUEUE àfront = QUEUE àfront àlink;

(QUEUE àcount)--;

free(temp);

return 1;

} // End of Deletequeue

PRIORITY QUEUES

A priority queue is a queue in which each element has been assigned a priority such that the
order in which elements are deleted and processed comes from the following rules:

1) An element of higher priority is processed before any element of lower priority.

2) Two elements with the same priority are processed according to the order in which they were
added to the queue.

Representation of Priority Queues in memory

Priority queues may be represented in the computer in various ways:

1) Array representation

2) Linked list representation

1) Array Implementation: Multiple Queues

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 49


Multiqueue is a data structure where multiple queues are maintained. This type of data structures
are used for process Scheduling. We use one dimensional array or multidimensional array to
represent a multiple queue.

Assume there are a fixed number of priorities; say M .Create an array of M queues.

A multiqueue implementation using a single dimensional array with ‘m’ elements is depicted in
the above figure. Each queue has ‘n’ elements which are mapped to a linear array of ‘m’
elements.

Operations on Priority Queues using Arrays

Inserting an element into a Priority Queue using Arrays

Algorithm insertpriorityqueue(i, x)

// This algorithm inserts an item with priority i into the ith entry of the array.

/ * Add x to queue i * /

int i,x;

1) ++rear[i];

2) if(rear[i] == front[i+1])

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 50


printf(“Queue is full”);

else

3) rear[i] = rear[i] + 1;

4) mqueue[rear[i]] = x;

Complexity

Time complexity: O(n)

Space complexity: O(n)

Deleting an element from a Priority queue

Algorithm delmq(i,x)

/ * Delete an element from queue i */

int i,x;

1) if(front[i] == rear[i])

printf(“Queue is empty”);

return 0;

2) x = mqueue[front[i]];

3) front[i] = front[i] – 1;

return x;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 51


}

Complexity

Time complexity: O(n)

Space complexity: O(n)

Linked list Implementation of Priority Queues

One way to maintain a priority queue in memory is by means of a one-way list as follows :

a) Each node in the list will contain 3 items of information: an information field INFO, a priority
number PRN and a link number LINK.

b) A node X precedes a node Y in the list.When X has higher priority than Y or

ii) When both have the same priority but X was added to the list before Y.

The order in the one-way list corresponds to the order of the priority queue. The lower the
priority number, the higher the priority. The main property of the one-way list representation of a
priority queue is that the element in the queue that should be processed first always appear at the
beginning of the one-way list.

Operations on Priority Queues using Linked lists

Inserting an element into a Priority Queue using Linked lists

Algorithm Insertitem(NODE *front, NODE *rear, int value, int priority)

1) Check for memory is empty

if(temp==NULL)

print("No Memory available ");

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 52


2) Create a new node using data value

newnode=(NODE *)malloc(sizeof(NODE));

newnode->data = value;

newnode->priority = priority;

newnode->link=NULL;

3) / * Is this the first node in the list * /

if (QUEUEàrear == NULL)

QUEUEàrear = newnode;

QUEUEàfront = QUEUEàrear = newnode;

else

4) / * The element to be inserted has highest priority

hence should be the first element * /

if((QUEUEàfront)->priority < priority)

newnode->link = QUEUEàfront;

QUEUEàfront = newnode;

else

/ * The element to be inserted has lowest priority hence should be the

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 53


last element * /

5) if( (QUEUEàrear)->priority < priority)

QUEUEàrear->link = newnode;

QUEUEàrear = newnode;

else

// find the position and insert the new element

6) Repeat steps while(currentàlink != null)

7) current=current->link;

8) for(int i=0;i<pos;i++)

9) if (currentàlink == pos)

newnode->link = current->link;

current->link = newnode;

Complexity

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 54


Time complexity: O(n)

Space complexity: O(n)

Deleting an element from a Priority queue using Linked lists

Algorithm delete(QUEUE *queue, QUEUE *front, QUEUE *rear, int item, int priority)

QUEUE *queue;

1) if(QUEUEcount=0)

printf(" The queue is empty cannot delete ");

return 0;

2) // Saves data in the first node

item = QUEUEfront->info;

priority = QUEUEfront ->priority;

temp = QUEUEfront;;

3) // Delete the first node

If (QUEUEfront= QUEUErear) then

QUEUEfront= QUEUErear=NULL;

QUEUEfront= QUEUEfrontlink;

free(temp);

return 1;

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 55


}

Complexity

Time complexity: O(n)

Space complexity: O(n)

APPLICATIONS OF QUEUES

Queuing Theory

Queuing theory is a field of applied mathematics that is used to predict the performance of
queues.

Queue types

 Single server queues - Provide service to only one customer at a time.

 Multiserver queues - Provide service to many customers at a time.

 Multiqueues, multiple single server queues

Common queue elements

One or more customers - A customer is any person or thing needing service.

Service - The service is any activity needed to accomplish the required result.

Affected factors

Arrival rate - The rate at which customers arrive in the queue for service.

Service time - The average time required to complete the processing of a customer request.

Queuing theory model

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 56


SIMULATION

Simulation is the use of one system to initiate the behavior of another system. Computer
simulation uses the steps of a program to initiate the behavior of system under study. In
computer simulation the objects being studied are represented as data, data structures whose
entries describe the properties of the objects. Actions in the system are represented as operations
on the data, and the rules describing these actions are translated into computer algorithms.
QUEUES are important data structures for use in computer simulations.

APPLICATION – 1

SIMULATION OF SINGLE SERVER CUSTOMER QUEUES IN A STORE

We build a model of a single-server queue. The store is open 8 hours per day, 7 days a week. To
simulate a day, we build a model that runs for 480 minutes (8 hours × 60 minutes). The
simulation uses a digital clock that events start and stop in 1-minute intervals.

In each minute of operation, three events will be checked:

 The arrival of customers

 The start of customer processing

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 57


 The completion of customer processing

Events

• newCustomer

• svcFree (svcStart)

• svcComplete

Queue Data Structures

Design for Queue simulation

Algorithm customersimulation

// This algorithm simulates a queue for a customer store.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 58


1) CreateQueue(queue)

2) Loop (clock <= endtime OR moreCusts)

newcustomer(queue, clock, custNum)

serverFree(queue, clock, custStatus, moreCusts)

svcComplete(queue, clock, custStatus, runStats, moreCusts)

3) If (queue not empty)

set moreCusts to true

4) increment Clock

5) PrintStats(runStats)

Algorithm newCustomer(queue, clock, custNum)

// This algorithm determines if a new customer has arrived.

1) Randomly determine if a new customer has arrived.

2) If (new customer)

increment custNum

store custNum in custData

store arrival time in custData

enqueue(queue, custData)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 59


}
}

Algorithm serverFree(queue, clock, status, moreCusts)

// This algorithm determines if the server is idle and if so starts servicing the new customer.

1) If (clock > status StartTime + status svcTime - 1)

// Server is idle.

2) If (not emptyQueue (queue))

dequeue(queue,custData)

set status custNum to custData number

set status arriveTime to custData arriveTime

set status startTime to clock

set status svcTime to random service time

set moreCusts true

Algorithm svcComplete(queue, clock, status, stats, moreCusts)

1) If (clock equals status StartTime + status svcTime - 1)

// Current Call complete

set waitTime to status startTime – status arriveTime

increment stats numCust

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 60


set stats totSvcTime to totSvcTime + status svcTime

set stats totWaitTime to totWaitTime + waitTime

set queueSize to queueCount (queue)

If (stats maxQueueSize < queueSize)

set stats maxQueueSize to queueSize

Print (status custNum, status arriveTime, status

startTime status svcTime waitTime

queueCount(queue))

set moreCusts to false

Algorithm printStats(stats)

// This algorithm prints the statistic’s for simulation.

1) Print (“Simulation statistic’s : )

2) Print (“ Total Customer’s : “ stats numCust)

3) Print (“ Total service time : “ stats totSvcTime)

4) set avgSvcTime to stats totSvcTime / stats numCust)

5) Print (“ Average service time : “ stats avgSvcTime)

6) set avgWaitTime to stats totwaitTime / stats numCust)

7) Print (“ Average wait time : “ stats avgWaitTime)

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 61


8) Print(“ Maximum Queuesize : “ stats maxQueueSize)

APPLICATION -2

AIRPORT SIMULATION PROBLEM

To simulate airport traffic control for a small but busy airport assuming the following
specifications.

 There is only one runway for both landing and take off.
 In each unit time, only one plane can land or take off but not both.
 Planes arrive and take off at random time, so that at any given unit, the runway may be
idle or a plane may be landing or taking off.
 Number of planes waiting to take off or land should not exceed a certain fixed limit.
 Landing planes should have high priority than Taking-off plane.
 After running for some period, the program should print statistics such as:
 Number of planes processed,
 Number of planes landed, and
 Number of planes took off
 Number of planes refused
 Average landing and take-off waiting times, and
 Average run-way idle time.

Algorithm

1) Create two queues, landing and takeoff


2) Initialize statistics counters to zero
3) Loop for time unit from 1 to some fixed number
4) Loop from 1 to some random number of landing planes
5) If landing queue is not full, add plane to the queue

else

refuse landing and increment number of refused.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 62


6) Loop from 1 to some random number of taking off planes
7) If takeoff queue is not full, add plane to the queue
else

refuse landing and increment number of refused.

8) If landing queue is not empty process next landing plane and increment count of planes
landed

else

9) if takeoff queue is not empty, process next plane and increment count of take off planes

else

increment count of run-way idle time.

10) Print the statistics.

Prepared by Dr.B.GOPI, Asst.Professor, KIST, Kakutur, Nellore, A.P Page 63

You might also like