Upload
sopoline-knowles
View
30
Download
2
Embed Size (px)
DESCRIPTION
Data Structure Lecture-4. Prepared by: Shipra Shukla Assistant Professor Kaziranga University. DEFINITION OF QUEUE. - PowerPoint PPT Presentation
Citation preview
Data StructureLecture-4
Prepared by:Shipra Shukla
Assistant ProfessorKaziranga University
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).
Queue
items[MAXQUEUE-1]
. .
. .
. .
items[2] C
items[1] B
items[0] A Front=0
Rear=2
Declaration of a Queue
# define MAXQUEUE 50 /* size of the queue items*/
typedef struct {int front; int rear;int items[MAXQUEUE];
}QUEUE;
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
INITIALIZE THE QUEUE
items[MAXQUEUE-1]
. .
. .
.
items[1]
items[0] front=-1
rear=-1
•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.
insert(&Queue, ‘A’)• an item (A) is inserted at the Rear of the queue
items[MAXQUEUE-1]
. .
. .
items[3]
items[2]
items[1]
items[0] A Front=0, Rear=0
insert(&Queue, ‘B’)• A new item (B) is inserted at the Rear of the queue
items[MAXQUEUE-1]
. .
. .
items[3]
items[2]
items[1] B Rear=1
items[0] A Front=0
insert(&Queue, ‘C’)• A new item (C) is inserted at the Rear of the queue
items[MAXQUEUE-1]
. .
. .
items[3]
items[2] C Rear=2
items[1] B
items[0] A Front=0
insert(&Queue, ‘D’)• A new item (D) is inserted at the Rear of the queue
items[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B
items[0] A Front=0
Insert Operationvoid insert( int items){
if(rear >= MAXQUEUE-1){
printf("Queue is full!");exit(1);
}else{rear->rear+1;queue[rear]=item;}
}
Insert Operationvoid insert(QUEUE *qptr, char x){
if(qptr->rear == MAXQUEUE-1){
printf("Queue is full!");exit(1);
}else{qptr->rear++;qptr->items[qptr->rear]=x;}
}
char remove(&Queue)• an item (A) is removed (deleted) from the Front of
the queue
items[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] A
char remove(&Queue)
• Remove two items from the front of the queue.
items[MAXQUEUE-1]
. .
. .
items[3] D Front=Rear=3
items[2] C
items[1] B
items[0] A
char remove(&Queue)
• Remove two items from the front of the queue.
items[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C Front=2
items[1] B
items[0] A
char remove(&Queue)
• Remove one more item from the front of the queue.
items[MAXQUEUE-1]
. .
items[4] Front=4
items[3] D Rear=3
items[2] C
items[1] B
items[0] A
Delete
void delete(){ if(front<0) { printf(“queue is empty”); break; } else {item=queue[front];front=front+1;printf(“item deleted=%d”,item);}
INSERT / REMOVE ITEMS• Assume that the rear= MAXQUEUE-1
•What happens if we want to insert a new item into the queue?
items[MAXQUEUE-1] X rear=MAXQUEUE-1
. .
. .
items[3] D front=3
items[2] C
items[1] B
items[0] A
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.
REMOVE ITEMitems[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] A
REMOVE ITEMitems[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] B
REMOVE ITEMitems[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] C
items[0] B
REMOVE ITEMitems[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] D
items[1] C
items[0] B
REMOVE ITEMitems[MAXQUEUE-1]
. .
. .
items[3] D
items[2] D Rear=2
items[1] C
items[0] B