Queues EENG212 Algorithm And Data Structures. DEFINITION OF QUEUE A Queue is an ordered collection of items from which items may be deleted at one end.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Data Structure Dr. Mohamed Khafagy.
STACKS AND QUEUES. A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO)
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
UNIT-2. Singly Linked List Doubly Linked List Circular Linked List Representing Stack with Linked List. Representing Queue with Linked List.
Linked List Spring 2013Programming and Data Structure1.
UNIT - I Linear structures. What is an abstract data type? A data type consists of a collection of values together with a set of basic operations on these.
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
Introduction Queue is a linear Data Structure in which the operations are performed based on FIFO (First In First Out) principle.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CE 221 Data Structures and Algorithms
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
UNIT-1 UNIT – I Introduction to data structures: Abstract data type (ADT), Stacks and Queues circular queues and their implementation with arrays. Stack.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
STACKS & QUEUES for CLASS XII ( C++).
STACKS & QUEUES II Array Based Approach.
Review Array Array Elements Accessing array elements
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Queues.
UNIT II Queue.
Basic Data Structures – Continued (Queues)
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
EENG 212 ALGORITHMS And DATA STRUCTURES
Abstract Data Type Abstract Data Type as a design tool
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Presentation transcript:

Queues EENG212 Algorithm And Data Structures

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=0 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 Operation void 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]DRear=3 items[2]C items[1]BFront=1 items[0]A

char remove(&Queue) Remove two items from the front of the queue. items [MAXQUEUE-1].... items[3]DRear=3 items[2]CFront=2 items[1]B items[0]A

char remove(&Queue) Remove two items from the front of the queue. items [MAXQUEUE-1].... items[3]DFront=Rear=3 items[2]C 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]DRear=3 items[2]C items[1]B items[0]A

Remove Operation char remove(struct queue *qptr) { char p; if(qptr->front > qptr->rear){ printf("Queue is empty"); exit(1); } else {p=qptr->items[qptr->front]; qptr->front++; return p; }

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]Xrear=MAXQUEUE items[3]Dfront=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 ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]BFront=1 items[0]A

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]BFront=1 items[0]B

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]C items[0]B

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]D items[1]C items[0]B

REMOVE ITEM items[MAXQUEUE-1].... items[3]D items[2]DRear=2 items[1]C items[0]B

Modified Remove Operation char remove(struct queue *qptr) { char p; int i; if(qptr->front > qptr->rear){ printf("Queue is empty"); exit(1); } else {p=qptr->items[qptr->front]; for(i=1;i rear;i++) qptr->items[i-1]=qptr->items[i]; qptr->rear-- return p; }

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 items[0] in case that spot is free.

Initialize the queue. items [6] front=rear=6 items[5] items[4] items[3] items[2] items[1] items[0]

Insert items into circular queue items [6] front=6 items[5] items[4] items[3] items[2] items[1] items[0]A rear=0 Insert A,B,C to the rear of the queue.

Insert items into circular queue items [6] front=6 items[5] items[4] items[3] items[2] items[1]B rear=1 items[0]A Insert A,B,C to the rear of the queue.

Insert items into circular queue Insert A,B,C to the rear of the queue. items [6] front=6 items[5] items[4] items[3] items[2]C rear=2 items[1]B items[0]A

Remove items from circular queue Remove two items from the queue. items [6] items[5] items[4] items[3] items[2] Crear=2 items[1] B items[0] Afront=0

Remove items from circular queue Remove two items from the queue. items [6] items[5] items[4] items[3] items[2] Crear=2 items[1] Bfront=1 items[0] A

Remove items from circular queue Remove one more item from the queue. items [6] items[5] items[4] items[3] items[2] Crear=front=2 items[1] B items[0] A

Insert D,E,F,G to the queue. items [6] G rear=6 items[5]F items[4] E items[3] D items[2] Cfront=2 items[1] B items[0] A

Insert H and I to the queue. items [6] G items[5]F items[4] E items[3] D items[2] Cfront=2 items[1] B items[0] Hrear=0

Insert H and I to the queue. items [6] G items[5]F items[4] E items[3] D items[2] Cfront=2 items[1] I items[0] Hrear=0

Insert J to the queue. items [6] G items[5]F items[4] E items[3] D items[2] ??front=rear=2 items[1] I items[0] H

Declaration and Initialization of a Circular Queue. #define MAXQUEUE 10 /* size of the queue items*/ typedef struct { int front; int rear; int items[MAXQUEUE]; }QUEUE; QUEUE q; q.front = MAXQUEUE-1; q.rear= MAXQUEUE-1;

Insert Operation for circular Queue void insert(QUEUE *qptr, char x) { if(qptr->rear == MAXQUEUE-1) qptr->rear=0; else qptr->rear++; /* or qptr->rear=(qptr->rear+1)%MAXQUEUE) */ if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=x; }

Remove Operation for circular queue char remove(struct queue *qptr) { if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1); } if(qptr->front == MAXQUEUE-1) qptr->front=0; else qptr->front++; return qptr->items[qptr->front]; }

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

#include #define MAXELEMENTS 50 #define TRUE 1 #define FALSE 0 typedef struct { int items[MAXELEMENTS]; int front, rear ; } QUEUE; void qinsert( QUEUE *, int); int qdelete(QUEUE *); int empty(QUEUE *);

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("\n%c",&operation); switch (operation) { case 'I' :case 'i':printf("%s\n","Insert an element"); scanf("\n%d",&x); qinsert(&q, x); break; case 'D' :case 'd':x=qdelete(&q); printf("\n %d is deleted \n",x); break; default: printf(“Incorrect Operation type\n”); break; } while (operation != 'E'&&operation!='e'); return 0; }

int empty(QUEUE *qptr) { return((qptr->front == qptr->rear) ? TRUE : FALSE); } int qdelete(QUEUE *qptr) { if (empty(qptr)) { printf("Queue underflow "); exit(1); } qptr->front=(qptr->front+1)%(MAXELEMENTS) return(qptr->items[qptr->front]); } void qinsert(QUEUE *qptr, int x) { /* make room for new element */ printf("\n %d is inserted \n",x); qptr->rear=(qptr->rear+1)%(MAXELEMENTS) if (qptr->rear == qptr->front) { printf("Queue overflow"); exit(1); } qptr->items[qptr->rear] = x; return; }

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.

#include #define MAXQUEUE 5 /* size of the queue items*/ typedef struct{ int front; int rear; float items[MAXQUEUE]; }queue; void insert(queue *, float); float remove(queue *); void display( queue *); void clear( queue *); void menu();

int main() { int choice; float x; queue q; q.front = MAXQUEUE-1; q.rear= MAXQUEUE-1; do{ menu(); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the item to be inserted:"); scanf("%f",&x); insert(&q,x); break; case 2: x=remove(&q); printf("\nRemoved item:%f",x); break; case 3: display(&q); break; case 4: clear(&q); break; default: printf("\nWrong entry, Try again!!"); break; } }while(choice != 5); return 0; }

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->rear == MAXQUEUE-1) qptr->rear=0; else qptr->rear++; if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=x; }

float remove(queue *qptr) { if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1); } if(qptr->front == MAXQUEUE-1) qptr->front=0; else qptr->front++; return qptr->items[qptr->front]; } void clear(queue *qptr) { qptr->front=MAXQUEUE-1; qptr->rear =MAXQUEUE-1; printf("Now the Queue is Empty\n"); }

void display(queue *qptr) { int f,r; f=qptr->front; r=qptr->rear; while(qptr->front !=qptr->rear){ if(qptr->front == MAXQUEUE-1) qptr->front =0; else qptr->front++; printf("%5.2f",qptr->items[qptr->front]); } printf("\n"); qptr->front=f; qptr->rear=r; }

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.

typedef struct{ int front; int rear; int items[MAXQUEUE]; /* Assume that MAXQUEUE is defined*/ }QUEUE; Assume that the Queue contains integer elements and has the following structure:

int QueueSearch(QUEUE *qptr, int searchkey) { int pos = -1,f; f=qptr->front; while(qptr->front !=qptr->rear){ if(qptr->front == MAXQUEUE-1) qptr->front =0; else qptr->front++; if(qptr->items[qptr->front] == searchkey){ pos = qptr->front; qptr->front = f; return pos; } qptr->front=f; return pos; } Then the following function can be written:

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.

void QueueCopyReverse(QUEUE *qptr1, QUEUE *qptr2) { int r,x; r=qptr1->rear; do{ x = qptr1->items[qptr1->rear]; insert(qptr2,x); if(qptr1->rear ==0) qptr1->rear = MAXQUEUE-1; else qptr1->rear --; }while(qptr1->rear != qptr1->front); qptr1->rear = r; } void insert(QUEUE *qptr, int x) { if(qptr->rear == MAXQUEUE-1) qptr->rear=0; else qptr->rear++; if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=x; }

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.

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

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 items[MAXQUEUE]; }QUEUE;

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.

int PRiQremove(QUEUE *qptr) { int smallest, loc, f, i; f=qptr->front; if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1); } smallest = qptr->items[(qptr->front+1)%MAXQUEUE]; loc = (qptr->front+1)%MAXQUEUE; (qptr->front++)%MAXQUEUE; /* Circular increment*/ while(qptr->front != qptr->rear){ if(qptr->items[(qptr->front+1)%MAXQUEUE] <smallest){ smallest = qptr->items[(qptr->front+1)%MAXQUEUE]; loc = (qptr->front+1)%MAXQUEUE; } qptr->front =(qptr->front+1)%MAXQUEUE; /* Circular inc.*/ }

while(loc != qptr->rear){ qptr->items[loc] = qptr->items[(loc+1)%MAXQUEUE]; (loc++)%MAXQUEUE; } qptr->front=f; if(qptr->rear == 0) /*Decrement rear after removing one item*/ qptr->rear = MAXQUEUE -1; else qptr->rear--; return smallest; }

Insert Operation of Priority Queue is the same as the insert of the non-priority queues. void insert(struct queue *qptr, int x) { qptr->rear = (qptr->rear++)%MAXQUEUE; /*Circular increment*/ if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=x; }