// queue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int front;
int rear;
int a[5];
int capacity;
};
void enqueue(struct queue *q, int data)
{
if((q->front == ((q->rear+1)%q->capacity)))
{
printf("Queue full");
return ;
}
if(q->front == -1)
q->front=0;
q->rear = (++q->rear)%q->capacity;
q->a[q->rear]=data;
printf("front %d rear %d\n",q->front,q->rear);
}
void dequeue(struct queue *q)
{
if((q->front == -1) && (q->rear == -1))
{
printf("\n queue empty");
return;
}
if(q->front == q->rear)
{
q->front = -1;
q->rear = -1;
return;
}
q->front = (++q->front)%q->capacity;
}
void display(struct queue q)
{
if((q.front == -1) && (q.rear == -1))
{
printf("\n queue empty");
return;
}
for(int i=q.front;;i++)
{
i = i%(q.capacity);
printf("-%d-",q.a[i]);
if(i==q.rear)
break;
}
}
int main(int argc, char* argv[])
{
int ch=0, data = 0;
struct queue q;
q.capacity = 5;
q.front = -1;
q.rear = -1;
printf("1.enqueue\n2.dequeue\n3.display\n4.exit");
while(1)
{
printf("\nEnter the option!\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data:");
scanf("%d",&data);
enqueue(&q,data);
break;
case 2: dequeue(&q);
break;
case 3: display(q);
break;
case 4: exit(0);
}
}
return 0;
}
Tuesday, July 28, 2015
queue implementation in c using array
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment