Compiler used: Turbo C
Language C
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
clrscr();
//queue deletion from front, insertion from rear
//check for overflow/underflow
int queue[10], front=-1, rear=-1, choice=0, entry=0, i;
while(choice!=4){
printf("\nChoose from the following options:\nPress 1 to insert.\nPress 2 to delete.\nPress 3 to display.\nPress 4 to exit.\n\nYour choice is? ");
scanf("%d",&choice);
switch(choice){
case 1:
clrscr();
if(rear==9 && front==0 || rear==front-1){
printf("\nOverflow! Queue is full.\n");
}
else if(front==-1 && rear==-1){
printf("\nPlease enter your number: ");
scanf("%d",&entry);
queue[++rear] = entry;
front =0;
printf("\nYour number has been entered!\n");
}
else{ printf("\nPlease enter your number: ");
scanf("%d",&entry);
++rear;
rear=rear%10;
queue[rear]=entry;
printf("\nYour number has been entered.\n");
}
break;
case 2:
clrscr();
if(rear==-1){
printf("\nUnderflow! Queue is empty.\n");
}
else if(front==rear){
printf("\nThe number has been deleted!\n");
front=-1;
rear=-1;
}
else{
front++;
front = front%10;
printf("\nThe number has been deleted from the queue!\n");
}
break;
case 3:
clrscr();
if(rear==-1){
printf("\nThe queue is empty.\n");
}
else if(front<=rear){
for(i=front;i<=rear;i++){
printf("%d ",queue[i]);
}
}
else{
for(i=front;i<=9;i++)
printf("%d ", queue[i]);
for(i=0;i<=rear;i++)
printf("%d ", queue[i]);
}
break;
case 4:
exit(0);
default:
clrscr();
printf("\nInvalid Choice!\n");
break;
}
}//end of while
}//end of main
#include<conio.h>
#include<stdlib.h>
void main(){
clrscr();
//queue deletion from front, insertion from rear
//check for overflow/underflow
int queue[10], front=-1, rear=-1, choice=0, entry=0, i;
while(choice!=4){
printf("\nChoose from the following options:\nPress 1 to insert.\nPress 2 to delete.\nPress 3 to display.\nPress 4 to exit.\n\nYour choice is? ");
scanf("%d",&choice);
switch(choice){
case 1:
clrscr();
if(rear==9 && front==0 || rear==front-1){
printf("\nOverflow! Queue is full.\n");
}
else if(front==-1 && rear==-1){
printf("\nPlease enter your number: ");
scanf("%d",&entry);
queue[++rear] = entry;
front =0;
printf("\nYour number has been entered!\n");
}
else{ printf("\nPlease enter your number: ");
scanf("%d",&entry);
++rear;
rear=rear%10;
queue[rear]=entry;
printf("\nYour number has been entered.\n");
}
break;
case 2:
clrscr();
if(rear==-1){
printf("\nUnderflow! Queue is empty.\n");
}
else if(front==rear){
printf("\nThe number has been deleted!\n");
front=-1;
rear=-1;
}
else{
front++;
front = front%10;
printf("\nThe number has been deleted from the queue!\n");
}
break;
case 3:
clrscr();
if(rear==-1){
printf("\nThe queue is empty.\n");
}
else if(front<=rear){
for(i=front;i<=rear;i++){
printf("%d ",queue[i]);
}
}
else{
for(i=front;i<=9;i++)
printf("%d ", queue[i]);
for(i=0;i<=rear;i++)
printf("%d ", queue[i]);
}
break;
case 4:
exit(0);
default:
clrscr();
printf("\nInvalid Choice!\n");
break;
}
}//end of while
}//end of main
PS: Many improvements are possible in the program. You are welcome to suggest them in comments! :)
No comments:
Post a Comment