...::: Recent Updates :::...

Tuesday, April 5, 2011

pqueue_linklist_Manjoor_sir method

Hello this program particular priority wise linklist created and if same priority found than another node is created behind that particular node

/*
Thanx Manjoor sir to give awsome idea and please any error or new thing please reply as soon as possible
authore Maulin Desai
Licence Agreement:- GNU/GPL :) :)
*/
#include

#include

#include
struct maulin

{

int no;

int prt;

struct maulin *itself;

struct maulin *next;

};

int flag=0;

int main()

{

char dec='y';

int ch;

struct maulin *head;

void add(struct maulin **);

void display(struct maulin **);
void del(struct maulin **);

head = (struct maulin *)malloc(sizeof(struct maulin));

head=NULL;

do

{

printf("\n1)ADD\n2)DISPLAY\n3)Delete\n4)exit\nEnter Your Choice:-\t");

scanf("%d",&ch);

switch(ch)

{

case 1:

add(&head);
break;
case 2:
display(&head);
break;
case 3:
del(&head);
break;
case 4:
exit(0);
default:
printf("\nEnter proper choice:-\t");

}
printf("\nDo You want to continue:-\t");
scanf("%s",&dec);
}while(dec=='y'||dec=='Y');

return 0;
}
void del(struct maulin **h)
{
int no;
struct maulin *t;
struct maulin *k1 =(struct maulin *)malloc(sizeof(struct maulin));
struct maulin *k =(struct maulin *)malloc(sizeof(struct maulin));
t=(*h);
printf("Enter Which priority no you want to delete:-\t");
scanf("%d",&no);
if((*h)==NULL)
{
printf("\nlink list is Empty");
return;
}
while(t!=NULL)
{
if((*h)->prt==no)
{
printf("\nHead Node:-\t");
k1=(*h);
(*h)=(*h)->next;
free(k1);
return;
}
if((t->next)->prt==no)
{
if((t->next)->next==NULL)
{
printf("\n Last Node");
k1=(t->next);
t->next=NULL;
printf("\tDeleted node is:-\t%d",k1->no);
free(k1);
return;
}
else
{
printf("\nNot a Head Node\n");
k=t->next;
t->next=(t->next)->next;
free(k);
return;
}
}
t=t->next;
}
}

void add(struct maulin **h)

{

int cnt=0;
struct maulin *t1=(struct maulin *)malloc(sizeof(struct maulin));

struct maulin *t2;

//t2=(*h);

printf("\nEnter priority no:-\t");

scanf("%d",&t1->prt);

printf("\nEnter Number:-\t");

scanf("%d",&t1->no);

t1->itself=NULL;

t1->next=NULL;



if((*h)==NULL)

{

printf("\nAssigning node as head");
(*h)=t1;

}

else

{

t2=(*h);

while(t2!=NULL)

{

if(t2->prt==t1->prt)

{

flag=0;

if(t2->itself==NULL)

{

printf("\n There is no other node below this priority:-");
t2->itself=t1;

return;

}

while(t2->itself!=NULL)

{

//t2->itself=t1;
t2=t2->itself;
printf("\n going to the last node:-\t%d",t2->no);

}

t2->itself=t1;

t1->next=NULL;

return;

}

else

{

if(t2->next==NULL)

{

printf("\nset flag at one:-\t");
flag=1;

}

else

{

flag=0;
printf("\nget to the next priority node:-\t");
t2=t2->next;
}

}

if(flag==1)

{

printf("\nno priority found thats why add priortiy at last:-\t");
t1->itself=NULL;

t1->next=NULL;

t2->next=t1;

return;

}


}

}

}

void display(struct maulin **head)

{

struct maulin *t,*t1;

//printf("\n%d",(*head)->no);

t=(*head);
t1=(*head);

while(t1!=NULL)

{

t=t1;

//printf("\nouter loop");

while(t!=NULL)

{

printf("\n%d %d",t->no,t->prt);

t=t->itself;

}

//printf("\nExit from inner loop");

t1=t1->next;

}

}

No comments:

Post a Comment