Title: Queues
1Queues
2DEFINITION OF QUEUE
- 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).
3Queue
itemsMAXQUEUE-1 .
. .
. .
. .
items2 C Rear2
items1 B
items0 A Front0
4Declaration of a Queue
- define MAXQUEUE 50 / size of the queue items/
- typedef struct
- int front
- int rear
- int itemsMAXQUEUE
- QUEUE
5QUEUE OPERATIONS
- Initialize the queue
- Insert to the rear of the queue
- Remove (Delete) from the front of the queue
- Is the Queue Empty
- Is the Queue Full
- What is the size of the Queue
6INITIALIZE THE QUEUE
- The queue is initialized by having the rear set
to -1, and front set to 0. Let us assume that
maximum number of the element we have in a queue
is MAXQUEUE elements as shown below.
itemsMAXQUEUE-1
.
.
.
items1
items0 front0
rear-1
7Queue
- a new item (D) is inserted at the Rear of the
queue
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B
items0 A Front0
8Insert Operation
- void insert(QUEUE qptr, int x)
-
- if(qptr-gtrear MAXQUEUE-1)
-
- printf("Queue is full!")
- exit(1)
-
- else
-
- qptr-gtrear
- qptr-gtitemsqptr-gtrearx
-
-
9Queue
- an item (A) is removed (deleted) from the Front
of the queue
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B Front1
items0 A
10Remove Operation
- int remove(struct queue qptr)
-
- int p
- if(qptr-gtfront gt qptr-gtrear)
- printf("Queue is empty")
- exit(1)
-
- else
- pqptr-gtitemsqptr-gtfront
- qptr-gtfront
- return p
-
-
11INSERT / REMOVE ITEMS
- Remove two items from the front of the queue.
itemsMAXQUEUE-1
. .
. .
items3 D FrontRear3
items2 C
items1 B
items0 A
12INSERT / REMOVE ITEMS
- Assume that the rear MAXQUEUE-1
itemsMAXQUEUE-1 X rearMAXQUEUE-1
. .
. .
items3 D front3
items2 C
items1 B
items0 A
- What happens if we want to insert a new item into
the queue?
13INSERT / REMOVE ITEMS
- What happens if we want to insert a new item F
into the queue? - Although there is some empty space, the queue is
full. - One of the methods to overcome this problem is to
shift all the items to occupy the location of
deleted item.
14REMOVE ITEM
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B
items0 A
itemsMAXQUEUE-1
. .
. .
items3
items2 D Rear2
items1 C
items0 B
15Modified Remove Operation
- int remove(struct queue qptr)
-
- int p,i
- if(qptr-gtfront gt qptr-gtrear)
- printf("Queue is empty")
- exit(1)
-
- else
- pqptr-gtitemsqptr-gtfront
- for(i1iltqptr-gtreari)
- qptr-gtitemsi-1qptr-gtitemsi
- qptr-gtrear--
- return p
-
-
16INSERT / REMOVE ITEMS
- Since all the items in the queue are required to
shift when an item is deleted, this method is not
preferred. - The other method is circular queue.
- When rear MAXQUEUE-1, the next element is
entered at items0 in case that spot is free.
17Initialize the queue.
itemsMAXQUEUE-1 frontrearMAXQUEUE-1
.
.
items3
items2
items1
items0
18Insert items into circular queue
- Insert A,B,C to the rear of the queue.
itemsMAXQUEUE-1 frontMAXQUEUE-1
.
.
items3
items2 C rear2
items1 B
items0 A
19Remove items from circular queue
- Remove two items from the queue.
itemsMAXQUEUE-1
.
.
items3
items2 C rear2
items1 B front1
items0 A
20itemsMAXQUEUE-1
.
items4 E rear4
items3 D
items2 C
items1 B front1
items0 A
21itemsMAXQUEUE-1 X
.
items4 E
items3 D
items2 C
items1 B front1
items0 Y rear0
22- Insert Z to the queue. (queue is full!)
itemsMAXQUEUE-1 X
. .
items4 E
items3 D
items2 C
items1 ?? frontrear1
items0 Y
23Declaration and Initialization of a Circular
Queue.
- define MAXQUEUE 10 / size of the queue items/
- typedef struct
- int front
- int rear
- int itemsMAXQUEUE
- QUEUE
- QUEUE q
- q.front MAXQUEUE-1
- q.rear MAXQUEUE-1
24Insert Operationfor circular Queue
- void insert(QUEUE qptr, int x)
-
- if(qptr-gtrear MAXQUEUE-1)
- qptr-gtrear0
- else
- qptr-gtrear
- if(qptr-gtrear qptr-gtfront)
- printf("Queue overflow")
- exit(1)
-
- qptr-gtitemsqptr-gtrearx
-
25Remove Operationfor circular queue
- int remove(struct queue qptr)
-
- if(qptr-gtfront qptr-gtrear)
- printf("Queue underflow")
- exit(1)
-
- if(qptr-gtfront MAXQUEUE-1)
- qptr-gtfront0
- else
- qptr-gtfront
- return qptr-gtitemsqptr-gtfront
-
26Example
- Following program is an example of circular queue
insertion and deletion operation.
27include ltstdlib.hgt include ltstdio.hgt define
MAXELEMENTS 50 define TRUE 1 define FALSE
0 typedef struct int itemsMAXELEMENTS
int front , rear QUEUE void
qinsert( QUEUE , int) int qdelete(QUEUE )
int empty(QUEUE )
28int main() char operation int x QUEUE q
q.front q.rear MAXELEMENTS - 1 do
printf("s\n","Insert Operation type I(nsert)
D(elete) or E(xit) ") scanf("\nc",operation)
switch (operation) case 'I' case
'i'printf("s\n","Insert an element")
scanf("\nd",x) qinsert(q , x)
break case 'D' case 'd'xqdelete(q)
printf("\n d is deleted \n",x) break
default printf(Incorrect Opeartion type\n)
break while (operation
! 'E'operation!'e') return 0
29int empty(QUEUE qptr) return((qptr-gtfront
qptr-gtrear) ? TRUE FALSE) int qdelete(QUEUE
qptr) if (empty(qptr)) printf("Queue
underflow ") exit(1) qptr-gtfront(qptr-gtfr
ont1)(MAXELEMENTS) return(qptr-gtitemsqptr-gtfron
t) void qinsert(QUEUE qptr , int x) /
make room for new element / printf("\n d is
inserted \n",x) qptr-gtrear(qptr-gtrear1)(MAXELE
MENTS) if (qptr-gtrear qptr-gtfront)
printf("Queue overflow") exit(1)
qptr-gtitemsqptr-gtrear x return
30PRIORITY QUEUES
- The priority queue is a data structure in which
intrinsic ordering of the elements determines the
results of its basic operations. - An ascending priority queue is a collection of
items into which items can be inserted
arbitrarily and from which only the smallest item
can be removed. On the other hand a descending
priority queue allows only the largest item to be
removed.
31PRIORITY QUEUES
- How do we implement insert and delete?
- Remember that members of a queue need not be
numbers or characters which can be compared
directly.
32Array implementation of PQ
- Insertion
- note order of insertion is not tampered with
- Deletion
- requires a search for the element of highest
priority - what happens if we delete an element in the
centre of the array
33Possible Solutions
- An empty indicator replaces deleted elements.
When the array is full compact the array to the
front. Inefficient in search. - Use the empty indicator and insert into the empty
slots. Inefficient in insert. - On each deletion move elements up the array,
decrementing rear. - Maintain array as an ordered circular array