Job Seek Algorithm using C++
Job Seek Algorithm using c++
#include<stdio.h>
#include<alloc.h>
#include<process.h>
#include<conio.h>
struct job
{
int d,pro;
struct job *pre,*next;
};
typedef struct
job node;
job node;
node *head,*jobc;
int f[100],front=0,rear=0;
void main()
{
clrscr();
int n;
void insert(int,int);
void display();
void sort();
void cal();
int cal2();
while(1)
{
printf("\n\tJob Algorithm \n\t 1. Insert \n\t 2. Display \n\t 3. Get Maximum Profit\n\t 4.Exit");
printf("\n\tEnter Your Choice : ");
scanf("%d",&n);
if(n==4)
break;
switch(n)
{
int d,p;
case 1:
printf("\n\tEnter Deadline : ");
scanf("%d",&d);
printf("\n\tEnter Profit : ");
scanf("%d",&p);
insert(d,p);
sort();
break;
case 2:
display();
break;
case 3:
jobc=NULL;
int profit;
cal();
profit = cal2();
printf("\n\tMax Profit is : %d",profit);
break;
}
getch();
clrscr();
}
}
int getPro()
{
node *tmp;
if(jobc==NULL)
{
printf("\n\tJobc Is Empty.");
return 0;
} else {
tmp=jobc;
int po=0;
while(tmp!=NULL)
{
po+=tmp->pro;
printf("\n\tProfit : %d DeadLine: %d",tmp->pro,tmp->d);
tmp=tmp->next;
}
return po;
}
}
int cal2()
{
int setflag(int);
node *tmp;
if(head==NULL)
printf("\n\tList Is NULL");
else
{
tmp=head;
while(tmp!=NULL)
{
int flag = setflag(tmp->d);
printf("\t%d",flag);
if(flag>0)
{
node *new1,*tmp1;
new1=(node *)malloc(sizeof(node));
new1->d=tmp->d;
new1->pro=tmp->pro;
if(jobc==NULL)
{
new1->next=NULL;
new1->pre=NULL;
jobc=new1;
}
else
{
tmp1=jobc;
while(tmp1->next!=NULL)
tmp1=tmp1->next;
new1->next=NULL;
new1->pre=tmp1;
tmp1->next=new1;
}
}
tmp=tmp->next;
}
int po =getPro();
return po;
}
}
int setflag(int d)
{
if(d<front)
{
return 0;
}
else if(f[d]==1)
{
int n=setflag(d-1);
return n;
}
else
{
f[d]=1;
return d;
}
}
void cal()
{
node *tmp;
int m;
tmp=head;
m=tmp->d;
while(tmp->next!=NULL)
{
if(m<tmp->d)
m=tmp->d;
tmp=tmp->next;
}
rear=m;
for(int i=0;i<rear;i++)
f[i]=0;
}
void sort()
{
node *tmp,*tmp1;
if(head!=NULL)
{
tmp=head;
while(tmp->next!=NULL)
{
tmp1=tmp->next;
while(tmp1!=NULL)
{
int c,d;
if(tmp->pro<tmp1->pro)
{
d=tmp1->d;
tmp1->d=tmp->d;
tmp->d=d;
c=tmp1->pro;
tmp1->pro=tmp->pro;
tmp->pro=c;
}
tmp1=tmp1->next;
}
tmp=tmp->next;
}
}
}
void display()
{
node *tmp;
if(head==NULL)
printf("\n\tList Is Empty.");
else
{
tmp=head;
while(tmp!=NULL)
{
printf("\n\tDead Line : %d \t Profit : %d",tmp->d,tmp->pro);
tmp=tmp->next;
}
}
}
void insert(int d,int p)
{
node *new1,*tmp;
new1=(node *)malloc(sizeof(node));
new1->d=d;
new1->pro=p;
if(head==NULL)
{
new1->pre=NULL;
new1->next=NULL;
head=new1;
}
else
{
tmp=head;
while(tmp->next!=NULL)
tmp=tmp->next;
new1->next=NULL;
new1->pre=tmp;
tmp->next=new1;
}
}
Comments
Post a Comment