Header Ads

Header ADS

Program for Circular Queue using Array

Introduction

Hello friends, In this post, we will learn how to write a simple program in C for circular queue using array.
This tutorial is for those people who want to learn programming in C and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.
Here we are calling two standard library functions. Whenever we call the library functions we must write their prototype before making the call.This helps the compiler in checking whether the values being passed and returned are as per the prototype declaration.But since we don’t define the library functions (we merely call them) we may not know the prototypes of library functions. Hence when the library of functions is provided a set of ‘.h’ files is also provided.These files contain the prototypes of library functions. But why multiple files? Because the library functions are divided into different groups and one file is provided for each group. For example, prototypes of all input/output functions are provided in the file ‘stdio.h’, prototypes of all mathematical functions are provided in the file ‘math.h’, etc.
On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. You can even open this file and look at the prototypes.

Program Briefing

First, we call the standard library function 'stdio.h' because prototypes of all input/output functions are provided in the file ‘stdio.h’. Then we call the standard library function 'conio.h' because On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. Then comes the program's main body. We declare a data structure 'queue' with an integer array 'data', two integer variables 'rear' and 'front'. We also declare a pointer 'p' for this data structure. Now, we declare a integer variable 'ch' and character variable 'opt'. Then we ask the user weather he/she wants to insert, delete or display the queue items. For each case we declare a different subroutine and switch to that subroutine on user's choice.

Program

// Program for Circular Queue using Array :

 #include<stdio.h>
 #include<conio.h>

 struct queue
 {
  int data[5];
  int rear,front;
 }queue;
 struct queue *p;

 void insert();
 void del();
 void display();

 void main()
 {
  int ch;
  char opt;
  p->front=-1;
  p->rear=-1;
  do
  {
   clrscr();
   printf("\nPress\n1...Insert\n2...Delete\n3...Display");
   printf("\nMaka a Choice : ");
    scanf("%d",&ch);
   switch(ch)
   {
    case 1:
  insert();
  display();
  break;
    case 2:
  del();
  display();
  break;
    case 3:
  display();
   }
   printf("\n\n\nDo U want to Continue(y/n) : ");
    scanf("%s",&opt);
  }while(opt=='y');
  getch();
 }

 void insert()
 {
  int x;
  if((p->rear+1)%5==p->front)
  printf("\nQueue Overflowing, Can't Insert.");
  else
  {
   printf("\nEnter the element to Insert : ");
    scanf("%d",&x);
   if(p->rear==-1)
   {
    p->front=0;
    p->rear=0;
    p->data[p->rear]=x;
   }
   else
   {
    p->rear=(p->rear+1)%5;
    p->data[p->rear]=x;
   }
  }
 }

 void del()
 {
  int x;
  if(p->rear==-1)
  printf("\nQueue Underflowing, Can't Delete.");
  else
  {
   x=p->data[p->front];
   if(p->rear==p->front)
   {
    p->front=-1;
    p->rear=-1;
   }
   else
    p->front=(p->front+1)%5;
   printf("\nDeleted element is %d.",x);
  }
 }

 void display()
 {
  int i;
  if(p->rear==-1)
  printf("\nQueue Underflowing, Nothing to Display.");
  else
  {
   printf("\n\n");
   i=p->front;
   while(i!=p->rear)
   {
    printf("\t%d",p->data[i]);
    i=(i+1)%5;
   }
   printf("\t%d",p->data[p->rear]);
  }
 }



No comments

Powered by Blogger.