CIRCULAR-QUEUE
Program
/*circular-queue using array*/
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct circular{
int item[max], rear;
}c;
int index;
/*displaying circular queue*/
void display(){
if(c.rear == -1){
printf("\nCircular-Queue is empty");
}else{
printf("\nThe elements of Circular-Queue is: ");
for(index=0;index<=c.rear;index++){
printf("%d\t",c.item[index]);
}
}
}
/*inserting elements into circular-queue*/
void ins_que(){
int element;
if(c.rear == (max-1)){
printf("\nCircular-Queue is full");
}else{
printf("\nEnter new element: ");
scanf("%d",&element);
for(index=++c.rear;index>=0;index--){
c.item[index]=c.item[index-1];
}
c.item[0] = element;
printf("\nInserted new element into Circular-Queue successfull");
display();
}
}
//deleted elements from Circular-Queue*
void del_que(){
if(c.rear == -1){
printf("\nCircular-queue is empty");
}else{
printf("\nDeleted Circular-Queue is: %d",c.item[0]);
for(index=0;index<=c.rear;index++){
c.item[index] = c.item[index+1];
}
c.item[c.rear--];
printf("\nDeleted Circular-Queue element successfull");
display();
}
}
/*main function*/
int main(){
int ch;
while(1){
printf("\n1.insert;\n2.delete;\n3.display;\n4.exit;\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
ins_que();
break;
case 2:
del_que();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("\nWrong choice, try again....");
break;
}
}
}
Output
Program
/*circular-queue using array*/
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct circular{
int item[max], rear;
}c;
int index;
/*displaying circular queue*/
void display(){
if(c.rear == -1){
printf("\nCircular-Queue is empty");
}else{
printf("\nThe elements of Circular-Queue is: ");
for(index=0;index<=c.rear;index++){
printf("%d\t",c.item[index]);
}
}
}
/*inserting elements into circular-queue*/
void ins_que(){
int element;
if(c.rear == (max-1)){
printf("\nCircular-Queue is full");
}else{
printf("\nEnter new element: ");
scanf("%d",&element);
for(index=++c.rear;index>=0;index--){
c.item[index]=c.item[index-1];
}
c.item[0] = element;
printf("\nInserted new element into Circular-Queue successfull");
display();
}
}
//deleted elements from Circular-Queue*
void del_que(){
if(c.rear == -1){
printf("\nCircular-queue is empty");
}else{
printf("\nDeleted Circular-Queue is: %d",c.item[0]);
for(index=0;index<=c.rear;index++){
c.item[index] = c.item[index+1];
}
c.item[c.rear--];
printf("\nDeleted Circular-Queue element successfull");
display();
}
}
/*main function*/
int main(){
int ch;
while(1){
printf("\n1.insert;\n2.delete;\n3.display;\n4.exit;\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
ins_que();
break;
case 2:
del_que();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("\nWrong choice, try again....");
break;
}
}
}
Output
Comments
Post a Comment