Description
This program implements the queue data structure. A queue is a data structure in which new items are entered to the rear position and items are removed from the front. Therefore a queue is also known as First in first out, which means that the items entered the first will be removed first. This program implements insertion, deletion and display of a queue using a switch case.
Program
#include<stdio.h> #include<conio.h> # define max 3 void main() { int i,q[max],front=0,rear=-1,ch,item; clrscr(); printf("\n1.Insert\n2.Delete\n3.Display\n4Exit\n\nEnter your choice"); scanf("%d",&ch); while(ch!=4) { switch(ch) { case 1: if(rear==(max-1)) { printf("Queue Full"); break; } else { printf("Enter the item to be inserted"); scanf("%d",&item); rear++; q[rear]=item; break; } case 2: if(front>rear) { printf("Queue is empty!!!"); break; } else { item=q[front]; printf("Item deleted is %d",item); front++; break; } case 3: if(front>rear) { printf("Queue is empty!!!"); break; } else for(i=front;i<=rear;i++) { printf("%d ",q[i]); } } printf("\n1.Insert\n2.Delete\n3.Display\n4Exit\n\nEnter your choice"); scanf("%d",&ch); } }
No comments:
Post a Comment