Saturday, 8 September 2012

Priority Queue

/*Priority Queue based on a explicit priority value "priority"*/
#include<stdio.h>
#include<conio.h>
typedef struct List
{
    char name;
    int priority;
    struct List *next;
}ProcessList;

ProcessList*getnode(char n,int pri)
{
    ProcessList*p=(ProcessList*)malloc(sizeof(ProcessList));
    p->name=n;
    p->priority=pri;
    p->next=NULL;
    return p;
}

ProcessList*new_process(ProcessList*head,char n,int p)
{
    ProcessList *t=head,*prev=NULL,*newnode;
    if(head==NULL)
        head=getnode(n,p);
    else
    {
        while(t!=NULL && t->priority>p)
        {
            prev=t;
            t=t->next;
        }
        newnode=getnode(n,p);
        if(prev==NULL)
        {
            newnode->next=head;
            head=newnode;
        }
        else
        {
            newnode->next=prev->next;
            prev->next=newnode;
        }
    }
    return head;
}

ProcessList next_process(ProcessList**head)
{
    ProcessList*p;
    if((*head)==NULL)
    {
        printf("No processes in the list\n");
        return;
    }
    else
    {
        p=(*head);
        (*head)=(*head)->next;
        return(*p);
    }
}

void info_printer(ProcessList pl)
{
    printf("\nProcess Name:: %c\tProcess Priority:: %d",pl.name,pl.priority);
}

int main()
{
    ProcessList*head=NULL;
    head=new_process(head,'a',10);
    head=new_process(head,'b',6);
    head=new_process(head,'c',11);
    head=new_process(head,'d',8);
    head=new_process(head,'e',6);
    head=new_process(head,'f',4);
    while(head!=NULL)
    info_printer(next_process(&head));
    getche();
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
@Gnosioware Solutions