".C" file is available in downloadable format at :
(Open the link and click on the download arrow on the top right to download the file.)
Given below is the code for "C Program for Circular queue using array" :
-----------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define size 50
typedef struct queue
{
int a[size];
int front;
int rear;
}ob;
ob q;
void qdelete(int*, int*);
void insert ()
{
int ele;
printf("\nEnter the element to be inserted:\t");
scanf("%d", &ele);
if((q.rear+1)%size==q.front)
// same as this : if((q.front==0)&&(q.rear=size-1)||(q.rear+1==q.front))
{
printf("\nQUEUE IS FULL !!\n 50 elements have been inserted, no more space :( ");
return;
}
else
{
if(q.rear==size-1)
q.rear=0;
else
q.rear++;
}
q.a[q.rear]= ele;
if(q.front==-1)
{
q.front=0;
}
printf("\n--------------------------| %d Inserted | --------------------------\n",q.a[q.rear]);
}
void qdelete(int* front, int* rear)
{
if(q.front==-1)
{
printf("\nQueue empty");
return;
}
else
{
printf("\n--------------------------| %d Deleted | --------------------------\n", q.a[q.front] );
if(q.front==q.rear)
{
q.front=-1;
q.rear=-1;
}
else
{
q.front=(q.front+1)%size;
/* same as this:
if(q.front==size-1)
q.front=0;
else
q.front++; */
}
}
}
void display ()
{
int i=q.front, j=q.rear;
printf("\n--------------------------DISPLAY--------------------------\n");
while(i!=j)
{
printf(" %d ", q.a[i++]);
}
printf (" %d", q.a[i]);
printf("\n");
}
void main()
{
int choice;
q.front = -1;
q.rear = -1;
while(1)
{
printf("\n\n\n1. Add data.\n2. Delete data.\n3. Display data.\n4. Exit program.\n\nEnter your choice:\t");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(); break;
case 2: qdelete(&q.front, &q.rear); break;
case 3: display (); break;
case 4: exit(0);
default: printf("\n\nHey!! Choose an option from the choices mentioned!!\n And stop testing my program!!");
}
}
}
No comments:
Post a Comment