/* static implementation of queue */
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define MAX 5
typedef struct
{
int info[MAX];
int front , rear;
} queue;
queue que;
main()
{
int n, ch;
clrscr();
que.front = 0;
que.rear = -1;
while(1)
{
clrscr();
f_disp(que);
puts("1. Enqueue\n2. Dequeue\n3. Display\4. Exit");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&que, n);
break;
case 2: n = dequeue(&que);
if(n!=-1)
printf("Dequeued %d", n);
else
printf("Queue underflow...");
getch();
break;
case 3: disp(que);
break;
case 4: exit(0);
default : printf("Invalid choice.");
}
}
}
enqueue(queue *q, int n)
{
if(q->rear == MAX)
{
puts("Queue overflow...");
getch();
return;
}
q->info[++q->rear] = n;
}
dequeue(queue *q)
{
int n;
if(q->fornt > q->rear)
{
puts("Queue underflow...");
getch();
return -1;
}
n = q->inf[q->front++];
return n;
}
disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("\n %d", q->info[i]);
getch();
}
f_disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("%d, ", q->info[i]);
}
0 comments:
Post a Comment