A program for queue usimg an array

Monday, 2 December 2013

/* A program for queue usimg an array */

#include<conio.h>
#include<stdio.h>
#define MAX 5

typedef struct Q
{
int R,F;
int data[MAX];
}Q;

void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);
void main()
{ Q q;
       int op,x;
     initialise(&q);
     clrscr();
     do{
printf("\n\n1)Insert\n2)Delete\n3)Print\n4)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: printf("\n Enter a value:");
  scanf("%d",&x);
  if(!full(&q))
   enqueue(&q,x);
  else
   printf("\nQueue is full !!!!");
  break;
  case 2:if(!empty(&q))
   {x=dequeue(&q);
    printf("\Deleted Data=%d",x);
   }
  else
   printf("\nQueue is empty !!!!");
  break;
  case 3: print(&q);break;
}
      }while(op!=4);
}

void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}

int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}

int full(Q *P)
{
if(P->R==MAX-1)
return(1);
return(0);
}

void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=P->R+1;
P->data[P->R]=x;
}
}

int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=P->F+1;
return(x);
}

void print(Q *P)
{
int i;
if(!empty(P))
{
printf("\n");
for(i=P->F; i <= P->R; i=i+1)
printf("%d\t",P->data[i]);
}
}


OUTPUT:-


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:45


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:67


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:78


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:3

45      67      78

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=45

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=67

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=78

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2

Queue is empty !!!!

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:4

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels