1 / 32

Data Structures

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

dixie
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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)

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

  4. Introduction to Queues • 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 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.

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

  6. Front Rear 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. • This characteristic gives a queue its first-in, first-out (FIFO) behavior.

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

  9. Queue Operations • 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 full. • 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.

  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. • 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 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) q.items[++q.rear]=x; • X=remove(q) x=q.items[q.front++];

  15. 4 4 3 3 2 C 2 q.rear=2 1 1 B 0 0 A q.front=0 q.front=0 q.rear=-1 • 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

  16. 4 4 E q.rear=4 3 3 D C C 2 2 q.front=2 q.rear=2 = q.front 1 1 0 0 • 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

  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 • 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 zeroth index • After dequeue shift all the elements of the array from array[index] to array[index-1]

  19. Dequeue 0 1 2 3 4 Rear 0 1 2 3 4 DE-QUEUE Rear

  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 • Do we need Front Index in this case? • No, Because we are always dequeue(ing) from the first index

  21. Solution2: Circular Queue front back • 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 • If q.rear==q.front queue is empty • q.front=q.rear=max_size-1; MAX_SIZE = 6 q[0] q[1] q[2] q[3] q[4] q[5] ? ? ? ? ? ?

  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)){ 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 front front front front back back back back q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5] ? ? ? ? ? ? A ? ? ? ? ? q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5] A D ? ? ? ? A D T ? ? ?

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

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

  27. Circular Queue [3] [2] [3] [2] J2 J3 [1] [4] [1] [4] J1 [0] [5] [0] [5] 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 • Keep it simple… add counter to the queue • 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. Solution b:Leave one empty space when queue is full FULL QUEUE FULL QUEUE [2] [3] [2] [3] J8 J9 J2 J3 J7 [1] [4][1] [4] J1 J4 J6 J5 J5 [0] [5] [0] [5] front =0 rear = 5 front =4 rear =3

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

More Related