#include<stdio.h>
#include<stdlib.h>
int MAX=0;//Max elements in the queue
typedef struct mynode
{
char info;
int pri;
struct mynode *Link;
}Node;
Node *start=NULL,*start2=NULL,*Last=NULL;
//Full
int isFull()
{
int flag=0;
Node *tptr=start;
while(tptr)
{
flag++;
tptr=tptr->Link;
}
if(flag==MAX)
return 1;
else
return 0;
}
//Empty
int isEmpty()
{
int flag=0;
Node *tptr=start;
while(tptr)
{
flag++;
tptr=tptr->Link;
}
/*if(start==NULL)
return 1;
else
return 0;*/
if(flag==0)
return 1;
else
return 0;
}
//Display Function
void display()
{
Node *tptr=start;
printf("\nQUEUE:\n");
while(tptr)
{
printf("%c->%d\n",tptr->info,tptr->pri);
tptr=tptr->Link;
}
}
//High Low Display Function
void display_high_low()
{
int max=0;
int min=100;
char max_info,min_info;
Node *tptr=start,*tptr1=start;
while(tptr)
{
if(tptr->pri>max)
{
max_info=tptr->info;
max=tptr->pri;
}
tptr=tptr->Link;
}
while(tptr1)
{
if(tptr1->pri<min)
{
min_info=tptr1->info;
min=tptr1->pri;
}
tptr1=tptr1->Link;
}
printf("Highest Priority:%c->%d\n",max_info,max);
printf("Lowest Priority:%c->%d\n",min_info,min);
}
//Add
void add(char v1,int v2)
{
Node *newp=NULL,*tptr=NULL;
newp=(Node*)malloc(sizeof(Node));
newp->info=v1;
newp->pri=v2;
newp->Link=NULL;
if(start==NULL)
{
start=newp;
}
else
{
tptr=start;
while(tptr->Link!=NULL)
{
tptr=tptr->Link;
}
tptr->Link=newp;
Last=newp;
}
printf("Value Added\n");
} //Delete void del() { int max=0; Node *tptr=start,*tptr1=start,*temp=NULL; while(tptr) { if(tptr->pri>max) { max=tptr->pri; } tptr=tptr->Link; } while(tptr1->pri!=max) { temp=tptr1; tptr1=tptr1->Link; } temp->Link=tptr1->Link; if(tptr1==Last) { Last=temp; } free(tptr1); printf("Value wid max priority deleted\n"); } //Main int main() { char v1; int v2; char choice; printf("Enter the max length of the queue:"); scanf("%d",&MAX); fflush(stdin); printf("MENU:\nA)ADD\nB)REMOVE\nC)DISPLAY\nD)HIGH AND LOW ELEMENT\nE)EXIT\n"); scanf("%c",&choice); fflush(stdin); while(choice!='E') { switch(choice) { case 'A': if(isFull()) { printf("\nQueue Overflow"); } else { printf("Enter the value:"); scanf("%c",&v1); printf("Enter the priority:"); scanf("%d",&v2); add(v1,v2); } break; case 'B': if(isEmpty()) { printf("\nQueue Empty"); } else { del(); } break; case 'C': display(); break; case 'D': display_high_low(); break; default: printf("Invalid Input"); } fflush(stdin); printf("\nMENU:\nA)ADD\nB)REMOVE\nC)DISPLAY\nD)HIGH AND LOW ELEMENT\nE)EXIT\n"); scanf("%c",&choice); fflush(stdin); } printf("Thank You for using our service\n"); return 0; }
Contributed by: Abhishek Banerjee, CSE, JISCE
Comments