Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Queues

Description:

Queues EENG212 Algorithm And Data Structures – PowerPoint PPT presentation

Number of Views:217
Avg rating:3.0/5.0
Slides: 62
Provided by: Musta53
Category:
Tags: queues

less

Transcript and Presenter's Notes

Title: Queues


1
Queues
  • EENG212
  • Algorithm
  • And
  • Data Structures

2
DEFINITION 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).

3
Queue
itemsMAXQUEUE-1
. .
. .
. .
items2 C
items1 B
items0 A
4
Declaration of a Queue
  • define MAXQUEUE 50 / size of the queue items/
  • typedef struct
  • int front
  • int rear
  • int itemsMAXQUEUE
  • QUEUE

5
QUEUE 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

6
INITIALIZE 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
7
insert(Queue, A)
  • an item (A) is inserted at the Rear of the queue

itemsMAXQUEUE-1
. .
. .
items3
items2
items1
items0 A Front0, Rear0
8
insert(Queue, B)
  • A new item (B) is inserted at the Rear of the
    queue

itemsMAXQUEUE-1
. .
. .
items3
items2
items1 B Rear1
items0 A Front0
9
insert(Queue, C)
  • A new item (C) is inserted at the Rear of the
    queue

itemsMAXQUEUE-1
. .
. .
items3
items2 C Rear2
items1 B
items0 A Front0
10
insert(Queue, D)
  • A new item (D) is inserted at the Rear of the
    queue

itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B
items0 A Front0
11
Insert Operation
  • void insert(QUEUE qptr, char x)
  • if(qptr-gtrear MAXQUEUE-1)
  • printf("Queue is full!")
  • exit(1)
  • else
  • qptr-gtrear
  • qptr-gtitemsqptr-gtrearx

12
char remove(Queue)
  • an item (A) is removed (deleted) from the Front
    of the queue

itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B Front1
items0 A
13
char remove(Queue)
  • Remove two items from the front of the queue.

itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C Front2
items1 B
items0 A
14
char remove(Queue)
  • Remove two items from the front of the queue.

itemsMAXQUEUE-1
. .
. .
items3 D FrontRear3
items2 C
items1 B
items0 A
15
char remove(Queue)
  • Remove one more item from the front of the queue.

itemsMAXQUEUE-1
. .
items4 Front4
items3 D Rear3
items2 C
items1 B
items0 A
16
Remove Operation
  • char remove(struct queue qptr)
  • char p
  • if(qptr-gtfront gt qptr-gtrear)
  • printf("Queue is empty")
  • exit(1)
  • else
  • pqptr-gtitemsqptr-gtfront
  • qptr-gtfront
  • return p

17
INSERT / 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?

18
INSERT / 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.

19
REMOVE ITEM
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B Front1
items0 A
20
REMOVE ITEM
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 B Front1
items0 B
21
REMOVE ITEM
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 C
items1 C
items0 B
22
REMOVE ITEM
itemsMAXQUEUE-1
. .
. .
items3 D Rear3
items2 D
items1 C
items0 B
23
REMOVE ITEM
itemsMAXQUEUE-1
. .
. .
items3 D
items2 D Rear2
items1 C
items0 B
24
Modified Remove Operation
  • char remove(struct queue qptr)
  • char p
  • int 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

25
INSERT / 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.

26
Initialize the queue.
items6 frontrear6
items5
items4
items3
items2
items1
items0
27
Insert items into circular queue
  • Insert A,B,C to the rear of the queue.

items6 front6
items5
items4
items3
items2
items1
items0 A rear0
28
Insert items into circular queue
  • Insert A,B,C to the rear of the queue.

items6 front6
items5
items4
items3
items2
items1 B rear1
items0 A
29
Insert items into circular queue
  • Insert A,B,C to the rear of the queue.

items6 front6
items5
items4
items3
items2 C rear2
items1 B
items0 A
30
Remove items from circular queue
  • Remove two items from the queue.

items6
items5
items4
items3
items2 C rear2
items1 B
items0 A front0
31
Remove items from circular queue
  • Remove two items from the queue.

items6
items5
items4
items3
items2 C rear2
items1 B front1
items0 A
32
Remove items from circular queue
  • Remove one more item from the queue.

items6
items5
items4
items3
items2 C rearfront2
items1 B
items0 A
33
  • Insert D,E,F,G to the queue.

items6 G rear6
items5 F
items4 E
items3 D
items2 C front2
items1 B
items0 A
34
  • Insert H and I to the queue.

items6 G
items5 F
items4 E
items3 D
items2 C front2
items1 B
items0 H rear0
35
  • Insert H and I to the queue.

items6 G
items5 F
items4 E
items3 D
items2 C front2
items1 I
items0 H rear0
36
  • Insert J to the queue.

items6 G
items5 F
items4 E
items3 D
items2 ?? frontrear2
items1 I
items0 H
37
Declaration 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

38
Insert Operationfor circular Queue
  • void insert(QUEUE qptr, char x)
  • if(qptr-gtrear MAXQUEUE-1)
  • qptr-gtrear0
  • else
  • qptr-gtrear
  • / or qptr-gtrear(qptr-gtrear1)MAXQUEUE) /
  • if(qptr-gtrear qptr-gtfront)
  • printf("Queue overflow")
  • exit(1)
  • qptr-gtitemsqptr-gtrearx

39
Remove Operationfor circular queue
  • char 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

40
Example
  • Following program is an example of circular queue
    insertion and deletion operation.

41
include 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 )
42
int main() char operation int x QUEUE q
q.front q.rear MAXELEMENTS - 1 do
printf("s\n",Queue 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 Operation type\n)
break while (operation
! 'E'operation!'e') return 0
43
int 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
44
Example
  • Write a program to simulate a queue structure
    with following specifications
  • User will be able to insert an item, remove an
    item, display all of the queue items and clear
    the queue.
  • The items will be floating point numbers.

45
includeltstdio.hgt includeltstdlib.hgt define
MAXQUEUE 5 / size of the queue items/ typedef
struct int front int rear float
itemsMAXQUEUE queue void insert(queue ,
float) float remove(queue ) void display(
queue ) void clear( queue ) void menu()
46
int main() int choice float x queue
q q.front MAXQUEUE-1 q.rear
MAXQUEUE-1 do menu() scanf("d",choice) switc
h(choice) case 1 printf("Enter the item to be
inserted") scanf("f",x) insert(q,x) break
case 2 xremove(q) printf("\nRemoved
itemf",x) break case 3 display(q) break ca
se 4 clear(q) break default printf("\nWrong
entry, Try again!!") break while(choice !
5) return 0
47
void menu() printf("Press 1 to insert
\n") printf("Press 2 to remove
\n") printf("Press 3 to display
\n") printf("Press 4 to clear \n") printf("Press
5 to quit\n") printf("\n\nEnter your
choice\n") void insert(queue qptr, float
x) if(qptr-gtrear MAXQUEUE-1) qptr-gtrear0 el
se qptr-gtrear if(qptr-gtrear
qptr-gtfront) printf("Queue overflow") exit(1)
qptr-gtitemsqptr-gtrearx
48
float remove(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
void clear(queue qptr) qptr-gtfrontMAXQUEUE-
1 qptr-gtrear MAXQUEUE-1 printf("Now the Queue
is Empty\n")
49
void display(queue qptr) int
f,r fqptr-gtfront rqptr-gtrear while(qptr-gtfron
t !qptr-gtrear) if(qptr-gtfront
MAXQUEUE-1) qptr-gtfront 0 else qptr-gtfront pr
intf("5.2f",qptr-gtitemsqptr-gtfront) printf("
\n") qptr-gtfrontf qptr-gtrearr
50
EXAMPLE
  • Assume that we have a queue of integer numbers.
    Write a function, QueueSearch to search a given
    key element in the queue until the search key is
    found. Once the search key is found, the function
    returns its position in the queue, otherwise
    returns -1 to indicate that the searched key is
    not in the queue.

51
Assume that the Queue contains integer elements
and has the following structure
typedef struct int front int rear int
itemsMAXQUEUE / Assume that MAXQUEUE is
defined/ QUEUE
52
Then the following function can be written
int QueueSearch(QUEUE qptr, int
searchkey) int pos -1,f fqptr-gtfront while(
qptr-gtfront !qptr-gtrear) if(qptr-gtfront
MAXQUEUE-1) qptr-gtfront 0 else qptr-gtfront if
(qptr-gtitemsqptr-gtfront searchkey) pos
qptr-gtfront qptr-gtfront f return
pos qptr-gtfrontf return pos
53
Example
  • Write a function, QueueCopyReverse, to copy the
    integer elements of Queue 1 to Queue 2 in reverse
    order.
  • Assume that there is enough space in Queue 2 for
    copying and the size of both of the Queues is
    MAXQUEUE.

54
void QueueCopyReverse(QUEUE qptr1, QUEUE
qptr2) int r,x rqptr1-gtrear do x
qptr1-gtitemsqptr1-gtrear insert(qptr2,x) if(qpt
r1-gtrear 0) qptr1-gtrear MAXQUEUE-1 else qptr1
-gtrear -- while(qptr1-gtrear !
qptr1-gtfront) qptr1-gtrear r void
insert(QUEUE qptr, int x) if(qptr-gtrear
MAXQUEUE-1) qptr-gtrear0 else qptr-gtrear if(qp
tr-gtrear qptr-gtfront) printf("Queue
overflow") exit(1) qptr-gtitemsqptr-gtrearx

55
PRIORITY 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.

56
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

57
Priority Queue Declaration
  • Queue data type of Priority Queue is the same as
    the Non-priority Queue.
  • define MAXQUEUE 10 / size of the queue items/
  • typedef struct
  • int front, rear
  • int itemsMAXQUEUE
  • QUEUE

58
REMOVE OPERATION
  • PriQremove Operation using removing the element
    with highest priority and shifting the elements
    up in the array and decrementing rear. Consider
    Ascending Priority Queue.

59
int PRiQremove(QUEUE qptr) int smallest, loc,
f, i fqptr-gtfront if(qptr-gtfront
qptr-gtrear) printf("Queue underflow") exit(1)
smallest qptr-gtitems(qptr-gtfront1)MAXQUEUE
loc (qptr-gtfront1)MAXQUEUE (qptr-gtfront)M
AXQUEUE / Circular increment/ while(qptr-gtfront
! qptr-gtrear) if(qptr-gtitems(qptr-gtfront1)MA
XQUEUE ltsmallest) smallest qptr-gtitems(qptr-gt
front1)MAXQUEUE loc (qptr-gtfront1)MAXQUEUE
qptr-gtfront (qptr-gtfront1)MAXQUEUE /
Circular inc./
60
while(loc ! qptr-gtrear) qptr-gtitemsloc
qptr-gtitems(loc1)MAXQUEUE (loc)MAXQUEUE
qptr-gtfrontf if(qptr-gtrear 0) /Decrement
rear after removing one item/ qptr-gtrear
MAXQUEUE -1 else qptr-gtrear-- return smallest
61
Insert Operation of Priority Queue is the same as
the insert of the non-priority queues.
void insert(struct queue qptr, int
x) qptr-gtrear (qptr-gtrear)MAXQUEUE
/Circular increment/ if(qptr-gtrear
qptr-gtfront) printf("Queue overflow") exit(1)
qptr-gtitemsqptr-gtrearx
Write a Comment
User Comments (0)
About PowerShow.com