Queue: Basic Operation On Queue
Queue: Basic Operation On Queue
A Queue is defined as linear list in which insertion is taking place at one end and deletion is taking place at the other end . The end where insertion is taking place is called as rear end. The end where deletion is taking place is called as front end. The Queue is a linear data structure where element which is inserted first is deleted first , hence queue is also called as FIFO structure(First In First Out).The fig. Shown below is an sample type queue. Basically queue is nothing but an array or a vector with a maximum capacity of size. Basic operation on queue: The important operation performed on the queue is INSERTION and DELETION . The insertion operation is called inserting an element onto rear end of the queue and deletion operation is called as deleting front element of the queue.. The pointer used to perform insertion and deletion operation on a queue is REAR and FRONT respectively. The following is the example which explains how insertion and deletion opearation can be done on the queue. Insertion Front=1 10 REAR=1 FRONT=0 REAR=0 Q is Empty 10 20 30 REAR=3 10 20 30 40 Front=1 10 20 REAR=2
Front=1 DELETION
FRONT=1 REAR=4 10 20 30 40
FRONT=2 REAR=4 20 30 40
FRONT=3 REAR=4 30 40
FRONT=4 REAR=4 40
A queue can be represented as a simple one dimensional vector and also using structure. 1. Using vector definition: Int que[80] Here que is a vector of reserved with 80 location to store integer data. Out of which user can define any number of location form 0 to 80 as size of stack. The elements in above queue can be accessed as simple as vector access. 2. Using structure definition: Struct que{ Int Int Int st[80]; front; rear;
}; Typedef struct que QUEUE; Here QUEUE represents a stack of type struct que. Member of structure can be accessed as QUEUE . item[rear] or QUEUE.item[front].
INSERT() { int ele; printf(Enter the element to be insert \n); scanf(%d,&ele); if(rear >= n) { printf(Queue is Over flow\n); return; } else { rear++; QUEUE[rear]=ele; if(front = = 0) front = 1; }
DELETE() { int ele; if(front = = 0) { printf(Queue is Under flow \n); return(0); } else { ele = QUEUE[front]; if(front = = rear) { front=0; rear=0; } else front++; return(ele); } } Write a program in C to simulate or to implement a queue using a static vector or one dimensional array of size n
#include <stdio.h> int LQ[80],front=0,rear=0; int n; main() { int choice=0,l; printf("enter the size of the linear Q\n"); scanf("%d",&n); while(choice!=4) { printf("1 INSERT \n"); printf("2 DELETE \n"); printf("3 DISPLAY \n"); printf("4 EXIT \n"); printf("Enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("LQ after insertion \n"); DISPLAY(); break; case 2: l=DELETE();
printf("DELETED element is %d\n",l); printf("LQ after deletion\n"); DISPLAY(); break; case 3: printf("Content of the LQ\n"); DISPLAY(); break; case 4: exit(0); } } INSERT() { int ele,t; printf("Enter the element to be insert\n"); scanf("%d",&ele); if(rear == n) { printf("Linear Q is Full \n"); return; } else { rear++; LQ[rear]=ele; } if(front==0) front=1; } DELETE() { int p; if(front == 0) { printf("Linear Q is Empty \n"); return(0); } p=LQ[front]; if(front==rear) { front=0; rear=0; } else front++; } return(p); }
#include <stdio.h> struct q{ int item[10]; int front; int rear; }; typedef struct q que; que QUEUE; int n; main() { int choice = 0,l; printf("Enter the size of the queue\n"); scanf("%d",&n); while(choice!=4) { printf("1. INSERT \n"); printf("2. DELETE \n"); printf("3. DISPLAY \n"); printf("4. Exit \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("The queue after insertion \n"); DISPLAY(); break; case 2: l=DELETE(); printf("The deleted element is %d\n",l); printf("The queue after the deletion\n"); DISPLAY(); break; case 3: printf("the content of the queue is \n"); DISPLAY(); break; case 4: exit(0); } } }
INSERT() { int ele; printf("Enter the element\n"); scanf("%d",&ele); if(QUEUE.rear == n) { printf("Queue is overflow\n"); return; } QUEUE.rear++; QUEUE.item[QUEUE.rear]=ele; if(QUEUE.front == 0) QUEUE.front=1; } DELETE() { int ele; if(QUEUE.front == 0) { printf("Queue is underflow\n"); return(0); } ele=QUEUE.item[QUEUE.front]; if(QUEUE.front == QUEUE.rear) { QUEUE.front=0; QUEUE.rear=0; } else QUEUE.front++; return(ele); } DISPLAY() { int i; if(QUEUE.front == 0) { printf("Queue is empty \n"); return; } printf("FRONT -> "); for(i=QUEUE.front;i<=QUEUE.rear;i++) printf("%d-> ",QUEUE.item[i]); printf("REAR\n"); }
CIRCULAR QUEUE: The major disadvantage of linear queue is the wastage of memory location . The above wastage of memory location can be over come by implementing the circular queue. The following example explains functions of circular queue . Consider a circular queue of size n=5 initially circular queue is empty with front and rear value is equal to 0. The following operation explains function of the circular queue. Implementation of Circular queue Insertion function: CQINSERT() { int ele; int temp; printf(Enter the element to be insert \n); scanf(%d,&ele); temp=rear; if(rear = = n) rear=1; else rear++; if(front = = rear) { printf(Circular Queue is Over flow\n); rear=temp; } else { CQ[rear]=ele; if(front = = 0) front=1; } }
Implementation of Circular queue Delete function: CQDELETE() { int ele; if(front = = 0) { printf(The Circular Queue is Under flow\n); return(0); } ele=CQ[front]; if(front = = n) front = 1; else { if(front = = rear) { front = 0; rear = 0; } else front++; } return(ele);
Write a program in C to simulate or to implement a circular queue using a static vector or one dimensional array of size n
#include <stdio.h> int CQ[80],front=0,rear=0; int n; main() { int choice=0,l; printf("enter the size of the circular Q\n"); scanf("%d",&n); while(choice!=4) { printf("1 INSERT \n"); printf("2 DELETE \n"); printf("3 DISPLAY \n"); printf("4 EXIT \n");
printf("Enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("CQ after insertion \n"); DISPLAY(); break; case 2: l=DELETE(); printf("DELETED element is %d\n",l); printf("CQ after deletion\n"); DISPLAY(); break; case 3: printf("Content of the CQ\n"); DISPLAY(); break; case 4: exit(0); } } }
INSERT() { int ele,t; printf("Enter the element to be insert\n"); scanf("%d",&ele); t=rear; if(rear == n) rear=1; else rear++ if(rear!=front) CQ[rear]=ele; else { printf("CQ is over flow\n"); rear=t; } if(front==0) front=1; } DELETE() { int p; if(front == 0) { printf("CQ is underflow\n"); return(0); } p=CQ[front]; if(front==n) front=1; else { if(front==rear) { front=0; rear=0; }
else front++; } return(p); } DISPLAY() { int t; t=front; while(rear!=front) { printf("CQ[%d]=%d ",front,CQ[front]); if(front==n) front=1; else front++; } printf("CQ[%d]=%d ",front,CQ[front]); printf("\n"); front=t; }
PRIORITY QUEUE: An ordinary queue is works on the principle FIFO principle . In a priority queue insertion depends on the element priority. Elements are deleted either in increasing or decreasing order of priority rather than in the order in which they have arrived in the queue. A priority queue is a collection of elements each one having an assigned priority. Insertion and deletion are two basic operations to be done on this queue. The deletion operation is different from ordinary queue . The deleletion operation is classified in two types 1. Ascending priority queue (Min Priority Queue) (Elements with a minimum priority is to be deleted first) 2. Descending priority queue(Max Priority Queue) ( Elements with a maximum priority is to be deleted first) Implementation of Priority queue Insertion function:
QINSERT(Q,p,x) queue Q[]; int p,x; { if(Q[p].rear == 4) { printf("Queue %d is overflow\n",p); return; } Q[p].rear++; Q[p].items[Q[p].rear] = x; if(Q[p].front == 0) Q[p].front=1; return(Q); } Implementation of Priority queue Deletion function: QDELETE(Q) queue Q[]; { int temp = 0; int empty , p,l; for(p=1;p<=n;p++) { if(Q[p].front == 0) { printf("the Q[%d] is empty\n",p); l=0; }
else { temp=Q[p].items[Q[p].front]; l=1; if(Q[p].front == Q[p].rear) { Q[p].front=0; Q[p].rear=0; } else Q[p].front++; break; } } if(l==1) printf("Element %d is deleted from %d queue\n",temp,p); } Write a program in C to simulate or to implement a Priority queue using a structure in C
#include <stdio.h> #define n 3 struct que{ int items[5]; int front; int rear; }; typedef struct que queue; main() { queue q[n]; int ele,choice=0; int l,i; int pri; clrscr(); for(i=1;i<=n;i++) { q[i].front=0; q[i].rear=0; } while(choice!=4) { printf("1. insert \n"); printf("2. delete \n"); printf("3. display \n"); printf("4. exit \n"); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the priority and element \n");
scanf("%d %d",&pri,&ele); QINSERT(q,pri,ele); printf("the content of the priority Q after insertion\n"); QDISPLAY(q); break; case 2: QDELETE(q); printf("the content of priority Q after deletion is \n"); QDISPLAY(q); break; case 3: printf("The content of the Priority Q is \n"); QDISPLAY(q); break; case 4: exit(0); } } } QINSERT(Q,p,x) queue Q[]; int p,x; { if(Q[p].rear == 4) { printf("Queue %d is overflow\n",p); return; } Q[p].rear++; Q[p].items[Q[p].rear] = x; if(Q[p].front == 0) Q[p].front=1; return(Q);
QDELETE(Q) queue Q[]; { int temp = 0; int empty , p,l; for(p=1;p<=n;p++) { if(Q[p].front == 0) { printf("the Q[%d] is empty\n",p); l=0; } else { temp=Q[p].items[Q[p].front]; l=1; if(Q[p].front == Q[p].rear) { Q[p].front=0; Q[p].rear=0; } else Q[p].front++; break; } } if(l==1)
QDISPLAY(Q) queue Q[]; { int i,p; for(p=1;p<=n;p++) { printf("Queue : %d\n",p); if(Q[p].front == 0) printf("Empty\n"); else { for(i=Q[p].front;i<=Q[p].rear;i++) printf("%d ",Q[p].items[i]); printf("\n"); } } printf("\n \n"); }