Wednesday, 12 February 2014

Queue using array cprogram

Aim: To Implement queue using arrays in cprogram

program:

#include<stdio.h>

#define MAXSIZE 3

void Insert(int);

void delete();

void Display();

int queue[MAXSIZE],rear=0,front=0;

main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.Insert\t");

printf("2.Delete\t");

printf("3.Dispiay\t");

printf("4.Exit\n");

printf("Enter your chioce : ");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("Enter the item you want to Insert : ");

      scanf("%d",&item);

      Insert(item);

      break;

case 2:item=Delete();

      if(item>0)

      printf("Item deleted from queue is %d\n",item);

      break;

case 3:Display();

      break;

      default:exit(0);

}

}

}

void Insert(int item)

{

if(rear==MAXSIZE)

printf("overflow\n");

else

{

rear=rear+1;

queue[rear]=item;

if(front==0)

front=1;

}

}

int Delete()

{

int item;

if(front==0)

{

printf("underflow\n");

return -1;

}

else

{

item=queue[front];

if(front==rear)

{

front=0;

rear=0;

}

else

front=front+1;

return item;

}

}

void Display()

{

int i;

printf("Elements of queue are :  ");

if(front==rear)

printf("NULL\n");

else

{

for(i=front;i<=rear;i++)

printf("%4d",queue[i]);

printf("\n");

}

}

output:


No comments:

Post a Comment