1 / 61

Queues

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

dena
Télécharger la présentation

Queues

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. Queues EENG212 Algorithm And Data Structures

  2. DEFINITION OF QUEUE • A Queue is an ordered collection of items from which items may be deleted at one end (called the frontof 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).

  3. Rear=2 Front=0 Queue

  4. Declaration of a Queue # define MAXQUEUE 50 /* size of the queue items*/ typedef struct { int front; int rear; int items[MAXQUEUE]; }QUEUE;

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

  6. INITIALIZE THE QUEUE • 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.

  7. insert(&Queue, ‘A’) • an item (A) is inserted at the Rear of the queue

  8. insert(&Queue, ‘B’) • A new item (B) is inserted at the Rear of the queue

  9. insert(&Queue, ‘C’) • A new item (C) is inserted at the Rear of the queue

  10. insert(&Queue, ‘D’) • A new item (D) is inserted at the Rear of the queue

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

  12. char remove(&Queue) • an item (A) is removed (deleted) from the Front of the queue

  13. char remove(&Queue) • Remove two items from the front of the queue.

  14. char remove(&Queue) • Remove two items from the front of the queue.

  15. char remove(&Queue) • Remove one more item from the front of the queue.

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

  17. INSERT / REMOVE ITEMS • Assume that the rear= MAXQUEUE-1 • What happens if we want to insert a new item into the queue?

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

  19. REMOVE ITEM

  20. REMOVE ITEM

  21. REMOVE ITEM

  22. REMOVE ITEM

  23. REMOVE ITEM

  24. 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<=qptr->rear;i++) qptr->items[i-1]=qptr->items[i]; qptr->rear-- return p; } }

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

  26. Initialize the queue.

  27. Insert items into circular queue • Insert A,B,C to the rear of the queue.

  28. Insert items into circular queue • Insert A,B,C to the rear of the queue.

  29. Insert items into circular queue • Insert A,B,C to the rear of the queue.

  30. Remove items from circular queue • Remove two items from the queue.

  31. Remove items from circular queue • Remove two items from the queue.

  32. Remove items from circular queue • Remove one more item from the queue.

  33. Insert D,E,F,G to the queue.

  34. Insert H and I to the queue.

  35. Insert H and I to the queue.

  36. Insert J to the queue.

  37. Initialization 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;

  38. Insert Operationfor 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; }

  39. Remove Operationfor 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]; }

  40. Example • Following program is an example of circular queueinsertion and deletion operation.

  41. #include <stdlib.h> #include <stdio.h> #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 *);

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

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

  44. Example • Write a program to simulate a queue structure with followingspecifications: • User will be able to insert an item, remove an item, display allof the queue items and clear the queue. • The items will be floating point numbers.

  45. #include<stdio.h> #include<stdlib.h> #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();

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

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

  48. 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"); }

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

  50. EXAMPLE • Assume that we have a queue of integer numbers. Write a function, QueueSearch to search agiven key element in the queue until the search key is found. Once the search key is found, thefunction returns its position in the queue, otherwise returns -1 to indicate that the searched key is not inthe queue.

More Related