Queue using Array Data Structure C Program

This Program is for Queue using Array. This is a part of Mumbai University MCA Colleges Data Structure C program MCA Sem 2 


The Below program inserts, deletes and displays the elements of a queue.

# include<stdio.h>
# include<conio.h>
# define MAX 5
void del();
void display();
void insert();
void display();

int queue_arr[MAX];
int rear = -1;
int front = -1;

void main()
{
int choice;
                  clrscr();
                  printf("QUEUE USING ARRAY \n");
while(1)
{
printf("SELECT YOUR CHOICE\n");
printf("1.Insert\n\n");
printf("2.Delete\n\n");
printf("3.Display\n\n");
printf("4.Quit\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
      printf("INVALID CHOICE\n");
}
}
}

void insert()
{
int add_item;
if (rear==MAX-1)
{
   printf("\n");
printf("Queue Overflow\n");
   printf("\n");
}
else
{
if (front==-1)
front=0;
printf("\nInput the element for adding in queue : ");
scanf("%d", &add_item);
rear=rear+1;
queue_arr[rear] = add_item ;
}
}

void del()
{
if (front == -1 || front > rear)
{
 printf("\n");
 printf("Queue Underflow\n");
   printf("\n");
 front=-1;
 return ;
}
else
{
    printf("\n");
    printf("Element %d is deleted from Queue \n", queue_arr[front]);
    printf("\n");
  front=front+1;
}
}

void display()
{
int i;
if (front == -1)
{
     printf("\n");
   printf("Queue is empty\n");
     printf("\n");
}
else
{
     printf(" Queue elements \n");
   for(i=front;i<= rear;i++)
     printf("%d ",queue_arr[i]);
   printf("\n");
}
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment