Saturday, April 23, 2011

Queue (Priority)

/* priority queue */

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

struct q
{
int info;
struct q *next;
} queue, *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueuePqueue(front, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */

enqueuePqueue(struct q *q, int n)
{
struct q *temp, *p = NULL;
temp = (struct q*)malloc(sizeof(struct q));
temp->info = n;
if(q==NULL)
{
front = rear = temp;
temp->next = NULL;
}
else if(ninfo)
{
temp->next = q;
front = temp;
}
else
{
while(q!=NULL&&q->infonext;
}
p->next = temp;
temp->next = q;
}
}



/* delete node from queue list */
dequeue(struct q *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(struct q *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(struct q *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites