0% found this document useful (0 votes)
16 views30 pages

CH 3 - Queue

Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue).

Uploaded by

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

CH 3 - Queue

Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue).

Uploaded by

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

CH 3

Queue
Queue

Definition

● A Queue is an ordered collection of items from which items may be deleted at


one end (called the front of the queue) and into which items may be inserted
at the other end (the rear of the queue).
● The first element inserted into the queue is the first element to be removed.
● For this reason a queue is sometimes called a fifo (first-in first-out) list as
opposed to the stack, which is a lifo (last-in first-out).
● The process of inserting an item to queue is called Enqueue and the process
of removing an item from the queue is called Dequeue.
Queue
Operations on queue

● MakeEmpty(q): To make q as an empty queue


● Enqueue(q, x): To insert an item x at the rear of the queue, this is also called
by names add, insert.
● Dequeue(q): To delete an item from the front of the queue q. this is also
known as Delete, Remove.
● IsFull(q): To check whether the queue q is full.
● IsEmpty(q): To check whether the queue q is empty
● Traverse (q): To read entire queue that is display the content of the queue.
Operation on queue
Queue as an ADT

● A queue q of type T is a finite sequence of elements with the operations:


● MakeEmpty(q): To make q as an empty queue
● Enqueue(q, x): To insert an item x at the rear of the queue, this is also called
by names add, insert.
● Dequeue(q): To delete an item from the front of the queue q. this is also
known as Delete, Remove.
● IsFull(q): To check whether the queue q is full.
● IsEmpty(q): To check whether the queue q is empty
● Traverse (q): To read entire queue that is display the content of the queue.
Implementation of queue

There are two techniques for implementing the queue:

● Array implementation of queue(static memory allocation)


● Linked list implementation of queue(dynamic memory allocation)
Static Implementation
# define MAXQUEUE /* size of the queue items*/
struct queue {
int front;
int rear;
int items[MAXQUEUE];
};
typedef struct queue qt;
Static Implementation [2]

Algorithm MakeEmpty(q): void makeEmpty(qt *q)


1. Start {
2. Initialize front and rear of q as:
q->rear=-1;
q->rear = -1
q->front = 0 q->front=0;
3. Stop
}
Static Implementation [3]

Algorithm isEmpty(q): int IsEmpty(qt *q)


1. Start {
2. Check the rear and front as:
if(q->rear < q->front)
If rear < front, then display empty
Else display not empty return 1;
3. Stop
else

return 0;

}
Static Implementation [4]

Algorithm isFull(q): int IsFull(qt *q)


1. Start {
2. Check if rear is equal to max size:
if(q-
If rear == MAXSIZE -1, then display full
>rear==MAXQUEUEZIZE-1)
Else display not full
3. Stop return 1;
else

return 0;

}
Static Implementation [5]

Algorithm Enqueue(q,x):

1. Start
2. Check if queue is full, if not go to step 3,
Else go to step 5:
3. Increment rear: rear++;
4. Insert x into q->items[rear]
5. Stop
Static Implementation [5]

Algorithm Dequeue(q):

1. Start
2. Check if queue is empty, if not go to step 3,
Else go to step 5:
3. Read from q->items[front]
4. Increment front.
5. Stop
Problems with linear queue

● Both rear and front indices are increased but never decreased.
● As items are removed from the queue, the storage space at the beginning of
the array is discarded and never used again.
● Wastage of the space is the main problem with linear queue.
Dynamic Implementation[1]
Circular Queue
● A queue where the start and end of the queue are joined together.
● A circular queue overcomes the problem of unutilized space in linear queue
implementation as array.
● A circular queue is one in which the insertion of a new element is done at very
first location of the queue if the last location of the queue is full.
● It is also called Ring Buffer.
● In circular queue we sacrifice one element of the array. Thus to insert n
elements in a circular queue we need an array of size n+1.(or we can insert
one less than the size of the array in circular queue).
Dynamic Implementation[2]
● Declaration of a Circular Queue:
# define MAXSIZE 50 /* size of the circular queue items*/
Struct cqueue{
int front;
int rear;
int items[MAXSIZE];
};
typedef struct cqueue cq;
● Initialization of Circular queue:
rear=front=MAXSIZE-1
Dynamic Implementation[3]

Algorithm for Inserting an element in a circular queue:


Dynamic Implementation[4]

Algorithm for Deleting an element in a circular queue:

● This algorithm assumes that rear and front are initially set to MAZSIZE-1.
Dynamic Implementation[5]
Dynamic Implementation[6]
Priority Queue

● A priority queue is a collection of elements such that each element has been
assigned a priority and the order in which elements are deleted and
processed comes from the following rules:.
○ An element of higher priority is processed before any element of lower priority.
○ If two elements has same priority then they are processed according to the order in which they
were added to the queue.
● The best application of priority queue is observed in CPU scheduling.
○ The jobs which have higher priority are processed first.
○ If the priority of two jobs is same this jobs are processed according to their position in queue.
○ A short job is given higher priority over the longer one.
Priority Queue Types

● Ascending priority queue(min priority queue):


○ An ascending priority queue is a collection of items into which items can be inserted arbitrarily
but from which only the smallest item can be removed.
● Descending priority queue(max priority queue):
○ A descending priority queue is a collection of items into which items can be inserted arbitrarily
but from which only the largest item can be removed.
Priority Queue ADT

A ascending priority queue of elements of type T is a finite sequence of elements


of T together with the operations:

● MakeEmpty(p): Create an empty priority queue p


● Empty(p): Determine if the priority queue p is empty or not
● Insert(p,x): Add element x on the priority queue p
● DeleteMin(p): If the priority queue p is not empty, remove the minimum
element of the queue and return it.
● FindMin(p): Retrieve the minimum element of the priority queue p.
Array implementation of priority queue:

Unordered array implementation:

● To insert an item, insert it at the rear end of the queue.


● To delete an item, find the position of the minimum element and
○ Either mark it as deleted (lazy deletion) or
○ Shift all elements past the deleted element by one position and then decrement rear.
Array implementation of priority queue:

Unordered array implementation:


Array implementation of priority queue:

Ordered array implementation:

● Set the front as the position of the smallest element and the rear as the
position of the largest element.
● To insert an element, locate the proper position of the new element and shift
preceding or succeeding elements by one position.
● To delete the minimum element, increment the front position.
Array implementation of priority queue:

Ordered array implementation:


Priority Queue Operations
● Declaration:
Data type of Priority Queue is the same as the Non-priority Queue.
#define MAXQUEUE 10 /* size of the queue items*/
struct pqueue { struct item{
int front;
int value;
int rear;
int priority;
struct item items[MAXQUEUE]; }
};
struct pqueue *pq;
Priority Queue Operations
● Insertion:
The insertion in Priority queues is the same as in non-priority queues.
● Deletion :
Deletion requires a search for the element of highest priority and deletes the
element with highest priority.
The following methods can be used for deletion/removal from a given Priority
Queue:
○ An empty indicator replaces deleted elements.
○ After each deletion elements can be moved up in the array decrementing the rear.
○ The array in the queue can be maintained as an ordered circular array
Applications of Priority Queue

● Resource Scheduling algorithm


● Sorting of a file
● CPU Scheduling algorithm

You might also like