Queue Implementation

#include<stdio.h>
#include<conio.h>
#define max 5
#include<process.h>

typedef struct queue
{       int item[max];
int rear, front;
}queuetype;

void enqueue(queuetype *);
void dequeue(queuetype *);
void traverse(queuetype *);
void main()
{clrscr();
int n;
queuetype *pq;
pq->rear=-1;
pq->front=0;
do
       { printf("\n1)Enqueue\n 2)Dequeue \n 3)Traverse\n 4) Exit\n\n");
printf("Enter your choice:");
scanf("%d", &n);
switch (n)
     {
case 1:
enqueue(pq);
break;
case 2:
dequeue(pq);
break;
case 3:
traverse(pq);
break;
case 4:
exit(0);
default:
printf("Input error");

     }
       } while (n<5);
getch();
}

/***************** Enqueue Operation ******************/
void enqueue(queuetype *pq)
{
int i=0 , e;
if (pq->rear== max-1)
printf("Queue is full Enqueue process is invalid\n");
else
{
printf("Enter the number you want in a queue:");
scanf("%d", &e);
if (e<=max)
      {
  printf("Enter %d number in queue\n", e);
  for (i=0; i<e; i++)
  {
    pq->rear=pq->rear+1;
    scanf("%d",&pq->item[i]);
  }
      }
else
printf("Your Queue system can't exceed %d", max);
}

}
/****************** Dequeue Operation *********************/
void dequeue (queuetype *pq)
{
if(pq->rear<pq->front)
printf("\nThe Queue is empty dequeue process is invalid\n");
else
{       int del;
del= pq->item[pq->front];
pq->front ++;
printf("\nDeleted item is %d", del);
}
}
/******************** Display Operation **********************/
void traverse(queuetype *pq)
{       int i;
if (pq->rear<pq->front)
printf("No element in a queue\n");
else
{       printf("\nThe items in a Queue are\n");
for(i= pq->front; i<= pq->rear; i++)
printf("%d\n", pq->item[i]);
}
}

Other C Programming:

Powered by Blogger.