/* Priority Queue */
#include<stdio.h>
#include<conio.h>
#define size 10
struct queue
{
int Q[size];
int front,rear;
}q;
int full()
{
if(q.rear==size-1)
{
return 1;
}
else
{
return 0;
}
}
int empty()
{
if(q.front>q.rear)
{
return 1;
}
else
{
return 0;
}
}
void insert(int x)
{
int j;
j=q.rear;
++q.rear;
while(j>=0 && q.Q[j]>=x)
{
q.Q[j+1]=q.Q[j];
j--;
}
q.Q[j+1]=x;
}
void del()
{
int x;
x=q.Q[q.front];
q.front++;
}
void display()
{
int i;
for(i=q.front;i<=q.rear;i++)
{
printf("%d ",q.Q[i]);
}
}
void main()
{
int ch,val;
q.rear=-1;
q.front=0;
clrscr();
while(1)
{
printf("\n1.insert\n2.delete\n3.display\n4.exit:");
scanf("%d",&ch);
switch(ch)
{
case 1:if(full())
{
printf("\nFULL!");
}
else
{
printf("\nEnter data");
scanf("%d",&val);
insert(val);
}
break;
case 2:if(empty())
{
printf("\Aalready empty\n");
}
else
del();
break;
case 3:
if(empty())
{
printf("\nalready empty\n");
}
else
display();
break;
case 4:exit(0);
break;
default:printf("\nproper choice plz!\n");
}
}
}
#include<stdio.h>
#include<conio.h>
#define size 10
struct queue
{
int Q[size];
int front,rear;
}q;
int full()
{
if(q.rear==size-1)
{
return 1;
}
else
{
return 0;
}
}
int empty()
{
if(q.front>q.rear)
{
return 1;
}
else
{
return 0;
}
}
void insert(int x)
{
int j;
j=q.rear;
++q.rear;
while(j>=0 && q.Q[j]>=x)
{
q.Q[j+1]=q.Q[j];
j--;
}
q.Q[j+1]=x;
}
void del()
{
int x;
x=q.Q[q.front];
q.front++;
}
void display()
{
int i;
for(i=q.front;i<=q.rear;i++)
{
printf("%d ",q.Q[i]);
}
}
void main()
{
int ch,val;
q.rear=-1;
q.front=0;
clrscr();
while(1)
{
printf("\n1.insert\n2.delete\n3.display\n4.exit:");
scanf("%d",&ch);
switch(ch)
{
case 1:if(full())
{
printf("\nFULL!");
}
else
{
printf("\nEnter data");
scanf("%d",&val);
insert(val);
}
break;
case 2:if(empty())
{
printf("\Aalready empty\n");
}
else
del();
break;
case 3:
if(empty())
{
printf("\nalready empty\n");
}
else
display();
break;
case 4:exit(0);
break;
default:printf("\nproper choice plz!\n");
}
}
}
No comments:
Post a Comment