C++ or C program for Insertion, Deletion, Traversing operations of Linear or Static Queue

File Name:- queue.cpp

#include<iostream.h>
#include<conio.h>
#define N 5
int queue[N],front=-1,rear=-1,item;
//insertion function
void insertion(){
if(rear==N-1){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter an Item to insert"<<endl;
cin>>item;
if(rear==-1){
front=rear=0;
}
else{
rear++;
}
queue[rear]=item;
cout<<"Insertion completed"<<endl<<item<<" is inserted in queue"<<endl;
getch();
return;
}
//deletion function
void deletion(){
if(front==-1){
cout<<"underflow Condition"<<endl;
getch();
return;
}
item=queue[front];
if(front==rear){
front=rear=-1;
}
else
{
front++;
}
cout<<"Deletion completed"<<endl<<item<<" is deleted from queue"<<endl;
getch();
return;
}
//traversing function
void traversing(){
int i;
if(front==-1){
cout<<"Queue is Empty"<<endl;
getch();
return;
}
cout<<"Linear Queue is: ";
for(i=front;i<=rear;i++){
cout<<queue[i]<<" ";
}
cout<<endl<<"front= "<<front<<" and rear= "<<rear<<endl<<"Traversing completed"<<endl;
getch();
return;
}
//main function
void main(){
int ch;
while(1){
clrscr();
cout<<"Queue Operation:-\n1.INSERTION\n2.DELETION\n3.TRAVERSING\n4.EXIT\nenter your choice"<<endl;
cin>>ch;
switch(ch){
case 1: insertion();break;
case 2: deletion();break;
case 3: traversing();break;
case 4: exit();
default: cout<<"Wrong Choice"<<endl;
getch();
}
}
}

File Name:- queue.c

#include<stdio.h>
#include<conio.h>
#define N 5
int queue[N],front=-1,rear=-1,item;
void insertion(){
if(rear==N-1){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter an Item to insert\n");
scanf("%d",&item);
if(rear==-1){
front=rear=0;
}
else{
rear++;
}
queue[rear]=item;
printf("insertion completed\n");
getch();
return;
}
void deletion(){
if(front==-1){
printf("underflow Condition\n");
getch();
return;
}
item=queue[front];
if(front==rear){
front=rear=-1;
}
else
{
front++;
}
printf("Deletion completed\n");
printf("%d is deleted\n",item);
getch();
return;
}
void traversing(){
int i;
if(front==-1){
printf("Queue is Empty\n");
getch();
return;
}
printf("Linear Queue is:");
for(i=front;i<=rear;i++){
printf(" %d ",queue[i]);
}
printf("front=%d and rear=%d\n",front,rear);
printf("Traversing completed");
getch();
return;
}
void main(){
int ch;
while(1){
clrscr();
printf("Queue Operation:-\n1.INSERTION\n2.DELETION\n3.TRAVERSING\n4.EXIT\nenter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1: insertion();break;
case 2: deletion();break;
case 3: traversing();break;
case 4: exit();
default:
printf("Wrong Choice\n");
getch();
}
}
}

No comments:

Post a Comment

Priority Queue

Priority queue:-  It is a special type of queue which stores group of elements. Each element has a priority number associated with it. Prior...