IMPLEMENTATION OF QUEUE ADT USING ARRAY

SOURCE CODE:

“qarray.h” File:

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

struct Queue
{
   int capacity;
   int size;
   int front;
   int rear;
   int *array;
};
typedef struct Queue *PtrToNode;
typedef PtrToNode QUEUE;
QUEUE CreateQueue(int max)
{
   QUEUE Q;
   Q=(struct Queue*)malloc(sizeof(struct Queue));
   if(Q==NULL)
      printf("\nFatal error");
   else
   {
      Q->size=0;
      Q->capacity=max;
      Q->front=0;
      Q->rear=-1;
      Q->array=(int*)malloc(sizeof(int)*max);
      if(Q->array==NULL)
     printf("\nFatal error");
      else
     printf("\nQueue is created successfully");
   }
   return Q;
}
void MakeEmpty(QUEUE Q)
{
   Q->size=0;
   Q->front=0;
   Q->rear=-1;
}
int IsEmpty(QUEUE Q)
{
   return Q->size==0;
}
int IsFull(QUEUE Q)
{
   return Q->size==Q->capacity;
}
void EnQueue(int x,QUEUE Q)
{
   if(IsFull(Q))
      printf("\nQueue is Full");
   else
   {
      Q->rear++;
      Q->array[Q->rear]=x;
      Q->size++;
   }
}
void DeQueue(QUEUE Q)
{
   if(IsEmpty(Q))
      printf("\nQueue is empty");
   else
   {
      Q->front++;
      Q->size--;
   }
}
int Front(QUEUE Q)
{
   return Q->array[Q->front];
}
QUEUE DisposeQueue(QUEUE Q)
{
   MakeEmpty(Q);
   free(Q->array);
   free(Q);
   Q=NULL;
   return Q;
}
void Display(QUEUE Q)
{
   int i;
   for(i=Q->front;i<=Q->rear;i++)
   printf("\n%d",Q->array[i]);
}

“qarray.c” File:

#include<stdio.h>
#include"qarray.h"
void main()
{
QUEUE Q=NULL;
int a,size,ch;
printf("\n\n1.CreateQueue\n 2.Enqueue\n 3.Dequeue\n 4.Front\n 5.MakeEmpty\n 6.IsEmpty\n     7.IsFull\n 8.DisposeQueue\n 9.Display\n 10.Exit\n");
X:
printf("\nEnter ur choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
    if(Q==NULL)
    {
       printf("\nEnter the size of queue");
       scanf("%d",&size);
       Q=CreateQueue(size);
    }
    else
       printf("\nQueue is already created");
    break;
case 2:
    if(Q==NULL)
       printf("\nQueue is not yet created");
    else
    {
       printf("\nEnter the element to insert");
       scanf("%d",&a);
       EnQueue(a,Q);
    }
    break;
case 3:
    if(Q==NULL)
       printf("\nQueue is not yet created");
    else
       DeQueue(Q);
    break;
case 4:
    if(Q==NULL)
       printf("\nQueue is not yet created");
    else
    {
       a=Front(Q);
       printf("\n Front element present in the queue is:\t%d",a);
    }
    break;
case 5:
    if(Q==NULL)
       printf("\n Queue is not yet created");
    else
    {
       MakeEmpty(Q);
       printf("\n Now Queue becomes empty");
    }
    break;
case 6:
    if(Q==NULL)
       printf("\n Queue is not yet created");
    else if(IsEmpty(Q))
       printf("\n Queue is empty");


    else
       printf("\n Queue contains some element");
    break;
case 7:
    if(Q==NULL)
       printf("\n Queue is not yet created");
    else if(IsFull(Q))
       printf("\n Queue is full");
    else
       printf("\n Queue is not full");
    break;
case 8:
    if(Q==NULL)
       printf("\n Queue is not yet created");
    else
    {
       Q=DisposeQueue(Q);
       printf("\n Queue is disposed");
    }
    break;
case 9:
    if(Q==NULL)
       printf("\n Queue is not yet created");
    else
    {
       printf("\n The elements in the Queue are:");
       Display(Q);
    }
    break;
case 10:
    exit(0);
default:
    printf("\n*******WRONG CHOICE********");
}
goto X;
}

OUTPUT:

1.CreateQueue
2.Enqueue
3.Dequeue
4.Front
5.MakeEmpty
6.IsEmpty
7.ISFull
8.DisposeQueue
9.Display
10.Exit

Enter ur choice:            1
Enter the size of queue:    3
Queue is created successfully

Enter ur choice:            2
Enter the element to insert:    100

Enter ur choice:            2
Enter the element to insert:    200

Enter ur choice:    2
Enter the element to insert:    300

Enter ur choice:     2
Enter the element to insert:    400
Queue is Full

Enter ur choice:     6
Queue contains some element

Enter ur choice:    4
Front element present in the queue is:    100

Enter ur choice:    7
Queue is Full

Enter ur choice:    9
The Elements in the queue are:
100
200
300

Enter ur choice:    3

Enter ur choice:    9
The Elements in the queue are:
200
300


Enter ur choice:    7
Queue is not Full

Enter ur choice:    4
Front element present in the queue is:    200

Enter ur choice:    5
Now Queue becomes empty

Enter ur choice:    8
Queue is Disposed

Enter ur choice:    12
            ***********WRONG ENTRY********

Enter ur choice:    10
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: