Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)

Similar presentations


Presentation on theme: "Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)"— Presentation transcript:

1 Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)

2 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 (called the rear of the 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 (called the rear of the queue)

3 Definitions Data structure which implements a first-in, first-out list; e.g. print queue, which contains a list of jobs to be printed in order. Data structure which implements a first-in, first-out list; e.g. print queue, which contains a list of jobs to be printed in order. In p, a queue is a data structure in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out). In programming, a queue is a data structure in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out). In contrast, a stack is a data structure in which elements are removed in the reverse order from which they were entered. This is referred to as LIFO (last in, first out). In contrast, a stack is a data structure in which elements are removed in the reverse order from which they were entered. This is referred to as LIFO (last in, first out).

4 Introduction to Queues The queue data structure is very similar to the stack. The queue data structure is very similar to the stack. In a stack, all insertions and deletions occur at one end, the top, of the list. In a stack, all insertions and deletions occur at one end, the top, of the list. In the queue, as in the stack, all deletions occur at the head of the list. In the queue, as in the stack, all deletions occur at the head of the list. However, all insertions to the queue occur at the tail of the list. However, all insertions to the queue occur at the tail of the list.

5 Introduction to Queues Basically, data enters the queue at one end and exits at the other end. Basically, data enters the queue at one end and exits at the other end.

6 ABC Front RearBC Front Rear

7 Introduction to Queues You get in line at the end and get serviced when you get to the front of the line. You get in line at the end and get serviced when you get to the front of the line. This characteristic gives a queue its first-in, first-out (FIFO) behavior. This characteristic gives a queue its first-in, first-out (FIFO) behavior.

8 Applications Ticketing counter Ticketing counter Bus stop line Bus stop line Bank Customers Bank Customers Printer SPOOL Printer SPOOL CPU Scheduling (at times) CPU Scheduling (at times) Etc etc etc Etc etc etc

9 Queue Operations Initialize the queue, Q, to be the empty queue. Initialize the queue, Q, to be the empty queue. Determine whether or not if the queue Q is empty. Determine whether or not if the queue Q is empty. Determine whether or not if the queue Q is full. Determine whether or not if the queue Q is full. Insert (enqueue) a new item onto the rear of the queue Q. Insert (enqueue) a new item onto the rear of the queue Q. Remove (dequeue) an item from the front of Q, provided Q is nonempty. Remove (dequeue) an item from the front of Q, provided Q is nonempty.

10 Queue Operations  isEmpty() – check to see if the queue is empty.  isFull() – check to see if the queue is full.  enqueue(element) - put the element at the end of the queue.  dequeue() – take the first element from the queue.  first() – return the first element in the queue without removing it.

11 Enqueue The queue insert is known as enqueue. The queue insert is known as enqueue. After the data has been inserted, this new element becomes the rear of the queue. After the data has been inserted, this new element becomes the rear of the queue.

12 Dequeue The queue delete operation is known as dequeue. The queue delete operation is known as dequeue. The data at the front of the queue is returned to the user and deleted from the queue. The data at the front of the queue is returned to the user and deleted from the queue.

13 Array Implementation Any implementation of a queue requires: storage for the data as well as markers (“pointers”) for the front and for the back of the queue. An array-based implementation would need structures like items, an array to store the elements of the queue Front, an index to track the front queue element Rear, an index to track the position following last queue element Additions to the queue would result in incrementing Rear. Deletions from the queue would result in incrementing Front. Clearly, we’d run out of space soon!

14 Queue using Arrays #define length 10 Struct queue { int items[length]; int front, rear; } Insert(q,x) Insert(q,x)q.items[++q.rear]=x; X=remove(q) X=remove(q)x=q.items[q.front++];

15 C B A q.front=0 q.rear=-1 q.front=0 q.rear=2 The queue is empty whenever q.rear<q.front The number of elements in the queue at any time is equal to the value of q.rear –q.front +1 0 1 2 3 4 0 1 2 3 4

16 = q.front C q.rear=2 C E q.front=2 q.rear=4 D Now there are 3 elements the queue but there is room for 5 If to insert F in the queue the q.rear must be increased by 1 to 5 and q. items[5] must be set to the value F. but q.items is an array of only 5 elements so this insertion cannot be made 0 1 2 3 4 0 1 2 3 4

17 Solution to Problem Clearly, we’have run out of space Solutions include: Shifting the elements downward with each deletion Viewing array as a circular buffer, i.e. wrapping the end to the front

18 Solution 1 For arrays there are two methods For arrays there are two methods First is do it as we do in real world First is do it as we do in real world  Check if array is not empty  Simply dequeue from the first location of array say array[0] i.e. the zero th index  After dequeue shift all the elements of the array from array[index] to array[index-1]

19 DequeueABCD RearBCD DE-QUEUE 01234 01234

20 Dequeue Operation x=q.items[0]; for(i=0; i<q.rear; i++) q.items[i]=q.items[i+1]q.rear--; Method is costly as we have to move all the elements of the array Method is costly as we have to move all the elements of the array Do we need Front Index in this case? Do we need Front Index in this case?  No, Because we are always dequeue(ing) from the first index

21 Solution2: Circular Queue To avoid the costly operation of coping all the elements again we employ another method called circular queue To avoid the costly operation of coping all the elements again we employ another method called circular queue Simply dequeue the element and move the front pointer to next index Simply dequeue the element and move the front pointer to next index If q.rear==q.front queue is empty If q.rear==q.front queue is empty q.front=q.rear=max_size-1; q.front=q.rear=max_size-1; q[3] ? q[4] ? q[5] ? q[0] ? q[1]q[2] ?? front MAX_SIZE = 6 back

22 Implementation Struct queue { int items[length]; int front, rear; }; Struct queue q; q.front=q.rear=Max_size –1; bool IsEmpty(struct queue *pq) {return(pq->front==pq->rear)} int remove (struct queue *pq) { if (empty(pq)){ if (empty(pq)){ cout<<“queue underflow” return 0; } If (pq->front==Max_size-1) pq->front=0;else(pq->front)++;Return(pq->items[pq->front])}

23 Implementation cont. int insert(struct queue *pq, int x) { //make room for new element If (pq->rear==Max_size-1) pq->rear=0;else(pq->rear)++; //Check for overflow If (pq->rear==pq->front) { cout<<“queue overflow”; exit(1);}pq->items[pq->rear]=x;return;}

24 Queue Operations q[3] ? q[4] ? q[5] ? q[0] ? q[1]q[2] ?? back front q[3] ? q[4] ? q[5] ? q[0] A q[1]q[2] ?? front back q[3] ? q[4] ? q[5] ? q[0] A q[1]q[2] D? front back q[3] ? q[4] ? q[5] ? q[0] A q[1]q[2] DT front back

25 Queue Operations cont. q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back

26 Queue Operations cont. q[3] E q[4] X q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] X q[5] A q[0] A q[1]q[2] DT front q[3] E q[4] X q[5] A q[0] M q[1]q[2] DT front back wrap-around back

27 Circular Queue [2] [3] [2] [3] [1] [4] [0] [5] [0] [5] J2 J1 J3 Can be seen as a circular queue

28 Problems with above solution How to know if the queue is empty How to know if the queue is full What is the relation between front and back when queue is full? Solution 1. 1.Keep it simple… add counter to the queue 2. 2.Keep an empty slot between Front and Rear i.e., items array uses QUEUE_CAPACITY - 1 elements

29 Solution (a) define MAXQUEUESIZE 100; struct { char personName[25]; int personNIC;//lets assume NIC to be integer type. } Person; struct { int Count; /* number of queue items */ int Head; /* head of queue */ int Tail; /* tail of queue */ Person Items[MAXQUEUESIZE]; }Queue;

30 Solution (a) cont. void InitializeQueue(Queue *Q) { Q->Count = 0; /* zero count of items */ Q->Head = MAXQUEUESIZE-1; Q->Tail = MAXQUEUESIZE-1; } int Empty(Queue *Q) { return (Q->Count == 0); } int Full(Queue *Q) { return (Q->Count == MAXQUEUESIZE); }

31 FULL QUEUE FULL QUEUE [2] [3] [2] [3] [1] [4][1] [4] [0] [5] front =0 rear = 5 front =4 rear =3 J2 J3 J1 J4 J5 J6 J5 J7 J8 J9 Solution b:Leave one empty space when queue is full

32 EMPTY QUEUE [2] [3] [2] [3] [1] [4] [0] [5] [0] [5] front = 0 front = 0 rear = 0 rear = 3 J2 J1 J3


Download ppt "Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)"

Similar presentations


Ads by Google