Thursday, 13 August 2015

DS: Queues

C program to implement queue.

#include<stdio.h>
#define MAX 10
int front=0;
int rear=-1;

int isfull(){
if(rear==MAX-1) return 1;
else return 0;
}

int isempty(){
if(rear<front) return 1;
else return 0;
}

void push(int item, int q[])
{
if(isfull()) printf("queue is full \n");
else
{
    q[++rear]=item;
}
}

void pop(int q[])
{
if(isempty()) printf("queue is empty \n");
else
{
printf("item deleted is %d",q[front++]);
}
}

void display(int q[])
{

if(rear<front) printf("queue is empty \n");
else{
int i;
for(i=front;i<=rear;i++)
printf("%d \n",q[i]);
}
}
void main()
{


int m;
int item;
int q[MAX];

for(;;){

printf("enter your choice \n");
printf("1.PUSH 2.POP 3.DISPLAY 4.EXIT \n");
scanf("%d",&m);

switch(m)
{

case 1: printf("enter the element to be pushed \n");
    scanf("%d",&item);
    push(item,q);
    break;
case 2: printf(" U have selected pop option \n");
    pop(q);
    break;

case 3: printf(" elements of queue are \n");
    display(q);
    break;
case 4: exit(0);    break;

}
}
}

No comments:

Post a Comment

Total Pageviews