hello sorting is done through Data not a link i am try to do sorting through link (node change)
here i am using gcc thats why its not support conio.h header file so add it and plese run my prog and give me feedback to add somthing new in it
if Some of the opeartion not done in prog than please send me or suggest me please and thanx sahin to create nice blog.
one day laxmi institute beacame no 1 institute :)
LICENCE :-GNU/GPL :) :) :) :) :)
*/
#include
#include
#include
struct maulin
{
int n;
char name[30];
struct maulin *next;
struct maulin *prev;
};
char ans;
int flag=0;
void main()
{
int choice;
void add(struct maulin **);
void display(struct maulin *);
void add_at(struct maulin **);
void first(struct maulin **);
void del(struct maulin **);
void sort_asc(struct maulin **);
void sort_desc(struct maulin **);
void inode_afterv(struct maulin **);
void inode_beforev(struct maulin **);
void reverse(struct maulin **);
struct maulin *header=(struct maulin *)malloc(sizeof(struct maulin));
header=NULL;
do
{
printf("\n1)Add\n2)insert node at beginning\n3)insert node before value\n4)insert node after value\n5)insert node at specific place\n6) delete\n7) sort linklist ascending\n8)sort linklist descending\n9)display\n10)Exit\n11)Reverse\nEnter choice:-");
scanf("%d",&choice);
switch(choice)
{
case 1:
add(&header);
break;
case 2:
first(&header);
break;
case 3:
inode_beforev(&header);
break;
case 4:
inode_afterv(&header);
break;
case 5:
add_at(&header);
break;
case 6:
del(&header);
break;
case 7:
sort_asc(&header);
display(header);
break;
case 8:
sort_desc(&header);
display(header);
break;
case 9:
display(header);
break;
case 10:
exit(0);
case 11:
reverse(&header);
break;
default:
printf("\nno case found\n");
}
printf("\nDo you want to continue:-");
scanf("%s",&ans);
}
while(ans!='n');
}
void add(struct maulin **head)
{
struct maulin *k,*temp;
temp=(struct maulin *)malloc(sizeof(struct maulin));
printf("\nEnter Number:-\t");
scanf("%d",&temp->n);
printf("\nEnter Name:-\t");
scanf("%s",&temp->name);
temp->next=NULL;
temp->prev=NULL;
if((*head)==NULL)
{
(*head)=temp;
}
else
{
k=(*head);
while(k->next!=NULL)
{
k=k->next;
}
k->next=temp;
temp->prev=k;
}
}
void first(struct maulin **head)
{
struct maulin *temp;
temp=(struct maulin *)malloc(sizeof(struct maulin));
printf("\nEnter Number:-\t");
scanf("%d",&temp->n);
printf("\nEnter Name:-\t");
scanf("%s",&temp->name);
temp->next=NULL;
temp->prev=NULL;
temp->next=(*head);
(*head)=temp;
}
void inode_afterv(struct maulin **head)
{
int value;
struct maulin *temp,*search;
printf("\nEnter Value for Search:-\t");
scanf("%d",&value);
search=(*head);
while(search!=NULL)
{
if(search->n==value)
{
if(search->next==NULL)
{
flag=1;
temp=(struct maulin *)malloc(sizeof(struct maulin));
printf("\nEnter Number:-\t");
scanf("%d",&temp->n);
printf("\nEnter Name:-\t");
scanf("%s",&temp->name);
temp->next=NULL;
temp->prev=NULL;
temp->prev=search;
search->next=temp;
break;
}
flag=1;
temp=(struct maulin *)malloc(sizeof(struct maulin));
printf("\nEnter Number:-\t");
scanf("%d",&temp->n);
printf("\nEnter Name:-\t");
scanf("%s",&temp->name);
temp->next=NULL;
temp->prev=NULL;
temp->prev=search;
temp->next=search->next;
search->next=temp;
(temp->next)->prev=temp;
}
search=search->next;
}
(flag==1)?printf("\nNode Found And New Node Added After Node\n"):printf("\nNo Record Found\n");
flag=0;
}
void inode_beforev(struct maulin **head)
{
int value;
struct maulin *temp,*search;
printf("\nEnter Value for Search:-\t");
scanf("%d",&value);
search=(*head);
while(search!=NULL)
{
if((*head)->n==value)
{
printf("\nmaulin in\n");
flag=1;
temp=(struct maulin *)malloc(sizeof(struct maulin));
printf("\nEnter Number:-\t");
scanf("%d",&temp->n);
printf("\nEnter Name:-\t");
scanf("%s",&temp->name);
temp->next=NULL;
temp->prev=NULL;
search->prev=temp;
temp->next=search;
(*head)=temp;
break;
}
if(search->n==value)
{
flag=1;
temp=(struct maulin *)malloc(sizeof(struct maulin));
printf("\nEnter Number:-\t");
scanf("%d",&temp->n);
printf("\nEnter Name:-\t");
scanf("%s",&temp->name);
temp->next=NULL;
temp->prev=NULL;
temp->prev=search->prev;
temp->next=search;
search->prev=temp;
(temp->prev)->next=temp;
}
search=search->next;
}
(flag==1)?printf("\nNode Found And New Node Added befor Node\n"):printf("\nNo Record Found\n");
flag=0;
}
void del(struct maulin **head)
{
struct maulin *del;
int val;
printf("\nEnter Value for delete:-\t");
scanf("%d",&val);
del=(*head);
while(del!=NULL)
{
if((*head)->n==val)
{
if((*head)->next==NULL)
{
free(*head);
printf("\n LinkList is Empty oops\n :(");
return;
}
flag=1;
(del->next)->prev=NULL;
*head=(del->next);
free(del);
goto a;
}
if(del->n==val)
{
if(del->next==NULL)
{
flag=1;
(del->prev)->next=NULL;
free(del);
goto a;
}
flag=1;
(del->next)->prev=del->prev;
(del->prev)->next=del->next;
free(del);
}
del=del->next;
}
a:
(flag==1)?printf("\nNode Deleted Succesfully\n"):printf("\nNo Node Found");
}
void display(struct maulin *p)
{
while(p!=NULL)
{
printf("%d\t",p->n);
puts(p->name);
p=p->next;
}
printf("End of line:)");
}
void add_at(struct maulin **t)
{
int n,i;
printf("Enter a Postion to Enter a Particular Node:-\t");
scanf("%d",&n);
struct maulin *node =(struct maulin *)malloc(sizeof(struct maulin)),*temp=(struct maulin *)malloc(sizeof(struct maulin));
temp=*t;
for(i=1;i
temp=temp->next;
if(temp==NULL)
{
flag=0;
goto k;
}
else
{
flag=1;
}
}
if(n==1)
{
printf("Enter value in node no:-");
scanf("%d",&node->n);
printf("Enter value in node name:-");
scanf("%s",&node->name);
node->prev=NULL;
node->next=temp;
temp->prev=node;
(*t)=node;
}
else
{
printf("Enter value in node no:-");
scanf("%d",&node->n);
printf("Enter value in node name:-");
scanf("%s",&node->name);
node->prev=temp->prev;
node->next=temp;
temp->prev=node;
(node->prev)->next=node;
}
k:
(flag==1)?printf("\nNode Found And New Node Added befor Node\n"):printf("\nNo Record Found\n");
}
void sort_asc(struct maulin **h)
{
struct maulin *i,*j;
int temp;
char temp_name[30];
i=(*h);
j=(*h);
for(;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->n>=j->n)
{
temp=i->n;
i->n=j->n;
j->n=temp;
strcpy(temp_name,i->name);
strcpy(i->name,j->name);
strcpy(j->name,temp_name);
}
}
}
}
void sort_desc(struct maulin **h)
{
struct maulin *i,*j;
int temp;
char temp_name[30];
i=(*h);
j=(*h);
for(;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->n<=j->n)
{
temp=i->n;
i->n=j->n;
j->n=temp;
strcpy(temp_name,i->name);
strcpy(i->name,j->name);
strcpy(j->name,temp_name);
}
}
}
}
void reverse(struct maulin **head)
{
struct maulin *f,*s;
f=(*head);
s=(*head);
(*head)=(*head)->next;
f->next=NULL;
f->prev=(*head);
while((*head)!=NULL)
{
f=s;
s=(*head);
(*head)=(*head)->next;
s->next=f;
s->prev=(*head);
if((*head)->next==NULL)
{
(*head)->prev=NULL;
(*head)->next=s;
return;
}
}
}
hey maulin..
ReplyDeletegood work..
but its pointer to pointer concept.. rite?
its rite..but mostly not easy to understand to beginners..
i hve done this wit simple pointer concept and just passing its reference during call.. which is somewhat easy for new learners.. isn't it..
nyways ur work is exclusive as ususall..
hey maulin ..
ReplyDeletecan u explain me this reverse of linklist code part.. using comment on each line.? plz.