#include<stdio.h>
#include<memory.h>
struct Node
{
int taskNo;
int tP1;
int tP2;
int minTime;
struct Node *next;
};
struct Node *Q1=NULL,*Q2=NULL,*head=NULL,*temp=NULL, *FRONT=NULL, *REAR=NULL, *FINAL=NULL;
void createQueue(int);
void printQueue(struct Node *);
void ENQUEUE_Q(struct Node *);
struct Node * johnson_algo(struct Node *);
void sort(struct Node *);
int main()
{
int tno;
printf("\n How many Task you want to Add : ");
scanf("%d",&tno);
createQueue(tno);
printf("\n ==== INITIAL TABLE ====");
printQueue(FRONT);
FINAL = johnson_algo(FRONT);
printf("\n ==== INTERMEDIATE TABLE ====");
printQueue(FINAL);
printf("\n ==== FINAL TABLE ====");
sort(FINAL);
printQueue(FINAL);
return 0;
}
void sort(struct Node *Q)
{
int tNo;
head=Q;
while(head->next!=NULL)
{
temp=head->next;
while(temp!=NULL)
{
if(head->minTime > temp->minTime)
{
tNo=head->taskNo;
head->taskNo=temp->taskNo;
temp->taskNo=tNo;
tNo=head->tP1;
head->tP1=temp->tP1;
temp->tP1=tNo;
tNo=head->tP2;
head->tP2=temp->tP2;
temp->tP2=tNo;
tNo=head->minTime;
head->minTime=temp->minTime;
temp->minTime=tNo;
}
temp=temp->next;
}
head=head->next;
}
}
struct Node * johnson_algo(struct Node *Q)
{
head=Q;
while(head!=NULL)
{
if(head->tP1 <= head->tP2)
{
head->minTime=head->tP1;
if(Q1==NULL)
{
Q1=head;
head=head->next;
Q1->next=NULL;
}
else
{
temp=Q1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
head=head->next;
temp->next->next=NULL;
}
}
else
{
head->minTime=head->tP2;
if(Q2==NULL)
{
Q2=head;
head=head->next;
Q2->next=NULL;
}
else
{
temp=head;
head=head->next;
temp->next=Q2;
Q2=temp;
}
}
}
//Merging two queue's
if(Q1!=NULL)
{
temp=Q1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=Q2;
return Q1;
}
else
{
return Q2;
}
}
void createQueue(int no)
{
int i=0;
while(i<no)
{
temp=(struct Node *)malloc(sizeof(struct Node)*1);
temp->next=NULL;
temp->taskNo=i;
printf("\n Enter Task Time for P1 : ");
scanf("%d",&temp->tP1);
printf("\n Enter Task Time for P2 : ");
scanf("%d",&temp->tP2);
ENQUEUE_Q(temp);
i++;
}
}
void ENQUEUE_Q(struct Node *temp)
{
if(FRONT==NULL)
{
FRONT=temp;
REAR=temp;
}
else
{
REAR->next=temp;
REAR=REAR->next;
}
}
void printQueue(struct Node *Q)
{
head=Q;
while(head!=NULL)
{
printf("\n| %2d | %2d | %2d | %2d | ->",head->taskNo,head->tP1,head->tP2,head->minTime);
head=head->next;
}
}
#include<memory.h>
struct Node
{
int taskNo;
int tP1;
int tP2;
int minTime;
struct Node *next;
};
struct Node *Q1=NULL,*Q2=NULL,*head=NULL,*temp=NULL, *FRONT=NULL, *REAR=NULL, *FINAL=NULL;
void createQueue(int);
void printQueue(struct Node *);
void ENQUEUE_Q(struct Node *);
struct Node * johnson_algo(struct Node *);
void sort(struct Node *);
int main()
{
int tno;
printf("\n How many Task you want to Add : ");
scanf("%d",&tno);
createQueue(tno);
printf("\n ==== INITIAL TABLE ====");
printQueue(FRONT);
FINAL = johnson_algo(FRONT);
printf("\n ==== INTERMEDIATE TABLE ====");
printQueue(FINAL);
printf("\n ==== FINAL TABLE ====");
sort(FINAL);
printQueue(FINAL);
return 0;
}
void sort(struct Node *Q)
{
int tNo;
head=Q;
while(head->next!=NULL)
{
temp=head->next;
while(temp!=NULL)
{
if(head->minTime > temp->minTime)
{
tNo=head->taskNo;
head->taskNo=temp->taskNo;
temp->taskNo=tNo;
tNo=head->tP1;
head->tP1=temp->tP1;
temp->tP1=tNo;
tNo=head->tP2;
head->tP2=temp->tP2;
temp->tP2=tNo;
tNo=head->minTime;
head->minTime=temp->minTime;
temp->minTime=tNo;
}
temp=temp->next;
}
head=head->next;
}
}
struct Node * johnson_algo(struct Node *Q)
{
head=Q;
while(head!=NULL)
{
if(head->tP1 <= head->tP2)
{
head->minTime=head->tP1;
if(Q1==NULL)
{
Q1=head;
head=head->next;
Q1->next=NULL;
}
else
{
temp=Q1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
head=head->next;
temp->next->next=NULL;
}
}
else
{
head->minTime=head->tP2;
if(Q2==NULL)
{
Q2=head;
head=head->next;
Q2->next=NULL;
}
else
{
temp=head;
head=head->next;
temp->next=Q2;
Q2=temp;
}
}
}
//Merging two queue's
if(Q1!=NULL)
{
temp=Q1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=Q2;
return Q1;
}
else
{
return Q2;
}
}
void createQueue(int no)
{
int i=0;
while(i<no)
{
temp=(struct Node *)malloc(sizeof(struct Node)*1);
temp->next=NULL;
temp->taskNo=i;
printf("\n Enter Task Time for P1 : ");
scanf("%d",&temp->tP1);
printf("\n Enter Task Time for P2 : ");
scanf("%d",&temp->tP2);
ENQUEUE_Q(temp);
i++;
}
}
void ENQUEUE_Q(struct Node *temp)
{
if(FRONT==NULL)
{
FRONT=temp;
REAR=temp;
}
else
{
REAR->next=temp;
REAR=REAR->next;
}
}
void printQueue(struct Node *Q)
{
head=Q;
while(head!=NULL)
{
printf("\n| %2d | %2d | %2d | %2d | ->",head->taskNo,head->tP1,head->tP2,head->minTime);
head=head->next;
}
}
Comments
Post a Comment