24
Data Structure Lecture-4 Prepared by: Shipra Shukla Assistant Professor Kaziranga University

Data Structure Lecture-4

Embed Size (px)

DESCRIPTION

Data Structure Lecture-4. Prepared by: Shipra Shukla Assistant Professor Kaziranga University. DEFINITION OF QUEUE. - PowerPoint PPT Presentation

Citation preview

Page 1: Data Structure Lecture-4

Data StructureLecture-4

Prepared by:Shipra Shukla

Assistant ProfessorKaziranga University

Page 2: Data Structure Lecture-4

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

Page 3: Data Structure Lecture-4

Queue

items[MAXQUEUE-1]

. .

. .

. .

items[2] C

items[1] B

items[0] A Front=0

Rear=2

Page 4: Data Structure Lecture-4

Declaration of a Queue

# define MAXQUEUE 50 /* size of the queue items*/

typedef struct {int front; int rear;int items[MAXQUEUE];

}QUEUE;

Page 5: Data Structure Lecture-4

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

Page 6: Data Structure Lecture-4

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.

Page 7: Data Structure Lecture-4

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

Page 8: Data Structure Lecture-4

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

Page 9: Data Structure Lecture-4

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

Page 10: Data Structure Lecture-4

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

Page 11: Data Structure Lecture-4

Insert Operationvoid insert( int items){

if(rear >= MAXQUEUE-1){

printf("Queue is full!");exit(1);

}else{rear->rear+1;queue[rear]=item;}

}

Page 12: Data Structure Lecture-4

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;}

}

Page 13: Data Structure Lecture-4

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

Page 14: Data Structure Lecture-4

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

Page 15: Data Structure Lecture-4

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

Page 16: Data Structure Lecture-4

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

Page 17: Data Structure Lecture-4

Delete

void delete(){ if(front<0) { printf(“queue is empty”); break; } else {item=queue[front];front=front+1;printf(“item deleted=%d”,item);}

Page 18: Data Structure Lecture-4

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

Page 19: Data Structure Lecture-4

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.

Page 20: Data Structure Lecture-4

REMOVE ITEMitems[MAXQUEUE-1]

. .

. .

items[3] D Rear=3

items[2] C

items[1] B Front=1

items[0] A

Page 21: Data Structure Lecture-4

REMOVE ITEMitems[MAXQUEUE-1]

. .

. .

items[3] D Rear=3

items[2] C

items[1] B Front=1

items[0] B

Page 22: Data Structure Lecture-4

REMOVE ITEMitems[MAXQUEUE-1]

. .

. .

items[3] D Rear=3

items[2] C

items[1] C

items[0] B

Page 23: Data Structure Lecture-4

REMOVE ITEMitems[MAXQUEUE-1]

. .

. .

items[3] D Rear=3

items[2] D

items[1] C

items[0] B

Page 24: Data Structure Lecture-4

REMOVE ITEMitems[MAXQUEUE-1]

. .

. .

items[3] D

items[2] D Rear=2

items[1] C

items[0] B