Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Queues A queue is a linear data structure. It is a list in which all insertions to the queue are performed at one end and all deletions are performed at the other end. The elements in the queue are processed in the same order in which they were received. Hence it is called a First In First Out (FIFO) list. The representation of queue is, Front Rear Deletion Insertion Examples: 1. Check out the line at a supermarket cash register. 2. The line of cars waiting to proceed in some fixed direction at an intersection of streets. 3. In computer system, queue of task waiting for the line printer, for access disk storage or even in a time-sharing system for the use of the CPU. Definition: A queue is an ordered collection of homogenous data elements where an insertion operation takes place at one end and a deletion operation takes place at the other end. The number of elements that a queue can accommodate is termed the Length of the queue. To insert an element into the queue, the following steps should be followed, 1. Increment the rear position by 1. 2. Check whether it is within the limit 3. If it is within the limit, insert a new element at this position. To remove an element from the queue, 1. The element at the position out by front will be removed. 2. Then the front position will be incremented. Operations on a Queue: The operations that can be performed on a queue are, 1. Insertion 2. Deletion 3. IsEmpty 4. IsFull Insertion Operation: An element can be inserted into the queue only when there is some space in the queue or only when the queue is not full. Before inserting an element into the queue, check whether the queue is full or not. If the queue is not full insert the element into the queue otherwise insertion is not possible. The insertion operation can be specified as follows, insert(q,element) where q is the queue in which insertion operation has to be done element item that has to be inserted at the rear end of the queue. Deletion Operation: An element can be removed from the queue only when the queue is not empty. Before removing an element from the queue, check whether the queue is empty or not. If the queue is not empty, the remove operation can be done at the front end of the queue, otherwise it cannot be performed. The remove operation can be specified as, Item=remove(q) Isempty Operation: The empty condition of the queue can be identified by executing this operation. The empty operation is as follows, bv=Isempty(q) This operation can return either TRUE or FALSE value. If the queue is empty, TRUE is returned otherwise it returns FALSE. IsFull Operation: The Full condition of the queue can be identified by executing this operation. The full operation is as follows, bv=IsIspty(q) This operation can return either TRUE or FALSE value. If the queue is full, TRUE is returned otherwise it returns FALSE. ADT for Queues: The ADT for queues are, 1. Enqueue() – Adds an item to the rear of the queue. 2. Dequeue() – Removes and return an item from the front end of the queue. 3. Front() – Verifies an item on the front end of the queue. Representation of a Queue: There are two ways by which queues can be represented in the memory. They are, 1. Using an array 2. Using a linked list. Representation of a queue using an array: This method can be used only when there is a need to use a queue of fixed size and that size is known. The array representation is, 1 2 ………………… N-1 Deletion operation occurs at this end N Insertion operation occurs at this end It requires, 1. One – dimensional array 2. A variable to indicate the FRONT position 3. Another variable to indicate the REAR position. Advantage: The implantation of queue using array is easy. Disadvantage: 1. It is possible to store only a fixed number of elements in the queue 2. Wastage of memory space can occur. \\ QUEUE USING ARRAY\\ #include<stdio.h> #include <conio.h> #define MAX 5 int front=-1,rear=-1; int q[MAX] ; main() { int choise; clrscr(); printf ("\nprogram foer queue in array\n"); while(1) { printf ("\n1=enqueue \n"); printf ("2=dequeue \n"); printf ("3=display\n"); printf ("enter the choise="); scanf ("%d",&choise); switch(choise) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit (1); defalt: printf("invalid choise"); break; } } getch(); } enqueue() { int item; if(rear==(MAX-1)) printf ("the QUEUE IS OVERFLOW"); else { printf ("\nEnter the element="); scanf ("%d",&item) ; rear=rear+1; q[rear]=item; } } dequeue () { if (front==rear) printf("the QUEUE IS EMPTY"); else { printf ("\nthe dequeued elelent is %d",q[front+1]); q[front]=0; front =front+1; } } display() { int i; if (front==rear) printf ("the QUEUE IS EMPTY"); else { printf ("\nthe elelent is\n"); for (i=front+1;i<=rear;i++) { printf (" %d \n",q[i]); } } } Representation of a Queue using linked list: In many applications, it is not possible to predict the size of the queue in advance and it may vary from time to time. In those situations, the representation of queue using linked list can be used. It overcomes all the problems of array representation of the queue. Data 1 HEADER Data 2 FRONT To represent the queue using linked list , the following are required, 1. Linked list 2. Front pointer 3. Rear pointer. Advantage: 1. There is no wastage of memory space 2. Insertion and deletion are easy to do. 3. Size of the queue is not fixed. Disadvantage: ………… Data n REAR It needs extra pointer fields used along with each data item in the queue. Program: Queue using Linked List. #include<Stdio.h> #include<conio.h> struct node { int item; struct node *no; }*fp,*rp; void main() { int ch,i; do { printf(“1.Create\n2. Insert\n 3. Delete \n 4. Display\n 5.Exit”); printf(“Enter the choice:”); scanf(“%d”,&ch); switch(ch) { case 1: create(); break; case 2: printf(“Enter the item:”); scanf(“%d”,&i); insert(i); break; case 3: i=delete(); printf(“Item deleted is%d” ,i); break; case 4: display(); break; case 5: exit(0); } }while(ch!=5); } create() { fp=NULL; rp=NULL; } insert(int item) { if((rp==NULL)&&(fp==NULL)) fp=rp=(struct node)malloc(sizeiof(struct mode)); if(rp np==null) { rpno=(struct node)malloc(sizeiof(struct mode)); rp=rpnp; } rpitem=item; rpnp=NULL; printf(item is inserted into the queue”); } int delete() { int item; if(fp==NULL) { printf(“\n Queue is empty”); return 0; } else { Item=fpitem; If(fp==rp) { rp==NULL; fp==NULL; } else { fp=fpitem; } } return item; } display() { struct node*tfp; tfp=fp; do { printf(“%d””,tfpitem); tfp=tfpnp; }while(tfp!=rp); printf(“%d\n”,tfpitem); } Various Other Queue Structures: The other queue structures are, 1. Circualr queue 2. Deque 3. Priority Queue Circular Queue: Representation of a queue using an array has the disadvantage that when the rear pointer reaches the end of the array, insertion operation is denied even if there is some space at the front of the array. This disadvantage can be overcome by using the circular – array representation of the queue. Rear Front n n-1 n-2 … .. 3 2 1 Physical Structure of Circular Array n 1 n-1 2 3 Rear …… Front Logical Structure of Circular Array Here, both the front and rear pointers move in the same direction. When the queue is at the ith position, the next position will be found out using the relation. I mod (length + 1), where length is the maximum number of elements that can be placed on the queue. In this circular queue the empty or full conditions are verified as follows, If the circular queue is EMPTY then FRONT = REAR = 0 If the circular is FULL then FRONT + (REAR MOD LENGTH) +1 The operations are as same of normal queue. Program Code: #include<stdio.h> scanf("%d",&choice); switch(choice) #include<conio.h> { void enqueue(); case 1:enqueue();break; void dequeue(); case 2:dequeue();break; void display(); case 3:display();break; int cqueue[100],front=-1,rear=0,size; case 4:exit();break; void main() default:printf("wrong choice"); { exit(); int choice; } clrscr(); printf("\n\t\t\tCIRCULAR QUEUE"); printf("\n\t\t\t----------------------------"); printf("\n\nEnter the size of the circular queue:"); scanf("%d",&size); while(1) { printf("\n\nMENU"); printf("\n----\n"); printf("1.Enqueue\n"); printf("2.Dequeue\n"); printf("3.Display\n"); printf("4.Exit\n"); printf("\nEnter the choice:"); } } void enqueue() { int x; if(front==(rear+1)%size) printf("\nCIRCULAR QUEUE IS FULL\n"); else { printf("\nEnter the element to be inserted:"); scanf ("%d",&x); if(front== -1) front=rear=0; else rear=(rear+1)%size; cqueue[rear]=x; }} void dequeue() { if(front== -1) printf("\nCIRCULAR QUEUE IS EMPTY\n"); else { printf("\nThe deleted number is %d",cqueue[front]); if(front==rear) front=rear= -1; else front=(front+1)%size; }} void display() { int i=front; if(i== -1) printf("\nCIRCULAR QUEUE IS EMPTY"); else { printf("\nItems in the circular queue:"); while(i!=rear) { printf ("\t%d",cqueue[i]); i=(i+1)%size; } printf ("\t%d",cqueue[i]); }} Priority Queue: A priority queue is a data structure in which each element has been assigned a value called the priority of the element and an element can be inserted (or) deleted not only at the ends but at any position on the queue. Rear Front A P1 B P2 … … … … X Pi … … P Pn Here, an element X of priority Pi may be deleted before an element which is at the position pointed out by FRONT. The insertion of an element is also based on the priority. There are two types of priority queues, they are, 1. An ascending priority queue and 2. a descending priority queue. Ascending Priority Queue: It is a collection of items in which items can be inserted arbitrarily and from which only the smallest item can be removed. Descending Priority Queue: It is a collection of items in which items can be inserted arbitrarily and from which only the largest item can be removed. Operations on a Priority Queue: The various operations that can be performed on a priority queue are the following, 1. Create a priority queue. 2. Check whether the priority queue is empty or full. 3. Insert an element to the priority queue. 4. Remove an element from the priority queue. Representation of the Priority Queue: There are two representations for the priority queue, 1. Array representation of priority queue 2. Linked list representation of a priority queue. Array Representation of a priority queue: Here, an array can be maintained to hold an item and its priority value. The element will be inserted at the REAR end as usual. The element can be deleted in any one of the following two ways, Method – I: 1. Starting from the FRONT position, search for the highest priority element. 2. Delete this element from the priority queue. 3. If the deleted element is not the element at the FRONT position, shift all the elements after the deleted element to fill up the vacant position made by the deleted element. Rear X X Y . P . Front Insertion Deletion Method – II: 1. Add the element at the REAR end position. 2. After every addition of an element to the priority queue, run a sort algorithm so that the highest priority element occupies the FRONT end position. 3. If the deletion has to be done then the element at the FRONT end position will be Front Rear removed. Deletion X Insertion Advantage: No Shifting of elements takes place. Disadvantage: Elements in the priority queue have to be sorted after every addition. So it may take some extra time. Linked List Representation of Priority Queue: This uses four fields for each node structure. They are. LLINK DATA PRIORITY RLINK Where, LLINK – hold address of the previous node. DATA – holds the actual content PRIORITY – holds priority value of the item. RLINK - hold address of the next node In this representation nodes are arranged according to the priority value. The steps to be followed to insert an item into the priority queue, 1. Start from the FRONT pointer position 2. Search for a node with the priority p to be inserted. Deque: In a deque , both insertion and deletion operations an be performed at either ed of the queue. Deque is a short form for the term double-ended queue. This deque can be used as a stack and as a queue. The operations on a Deque are 1. PUSHDQ(item) – insert an item at the front end 2. POPDQ() – Remove item from front end of the deque. 3. INJECT(item) – Used to insert item at the rear end of the deque. 4. EJECT(item) – Used to remove item at the rear end of the deque. Deletion Insertion ………….. Program Code for Priority Queue: #include<stdio.h> #include<conio.h> #define size 5 int rear,front; int Queue[size]; QueueFull() { if(rear==size-1) return 1; else return 0; } QueueEmpty() { if((front==-1)||(Front>rear)) return 1; else return 0; } void insert() { int item; void priority(); printf(“Enter the item:”); scanf(“%d”,&item); if(front==-1) Deletion Insertion front++; queue[++rear]=item; priority(); } void priority() { int i,j,temp; for(i=front;i<rear-1;i++) for(j=front;j<rear;j++) if(queue[j]>queue[j+]) { temp=queue[j]; queue[j]=queue[j+1]; queue[j+1]=temp; } } void delet() { int item; item=queue[front]; printf(“Item deleted is %d”,item); front++; } void display() { int i; printf(“\n The queue is:”); for(i=front;i<=rear;i++) printf(“%d”,queue[i]); } case 4: exit (1); default: printf("invalid choise"); break; } } getch(); } void main() { Applications of Queue: int choise; 1. Operating System clrscr(); 2. Multiprogramming environments printf ("\nprogram for priority queue using array\n"); 3. Implementing various Scheduling while(1) algorithms { printf ("\n1. Insert \n"); printf ("\n2.Delete \n"); printf ("\n3.Display\n"); printf ("\n4.Exit\n"); printf ("enter the choise="); scanf ("%d",&choise); switch(choise) { case 1: if(QueueFull()) printf(“\n Queue is full”); else insert(); break; case 2: if(QueueEmpty()) printf(“\n Queue is Empty”); else delete(); break; case 3: display(); break;