in output it doesn't show sorted list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
void strand_sort(struct node **,struct node **,struct node **);
void insert(struct node **);
void merge(struct node **,struct node **);
void display(struct node **);
void main()
{
struct node *first=NULL;
struct node *strand=NULL;
struct node *sort=NULL;
struct node *te;
printf("enter numbers\n");
insert(&first);
te=first;
while(te!=NULL)
{
printf("\n %d",te->info);
te=te->next;
}
strand_sort(&first,&strand,&sort);
display(&sort);
}
void insert(struct node **f)
{
struct node *p;
struct node *temp;
int n,k;
do
{
p=*f;
temp=(struct node *)malloc(sizeof(struct node));
printf("enter elem\n");
scanf("%d", &n);
temp->info=n;
temp->next=NULL;
if(*f==NULL)
*f=temp;
else{
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
printf("enter 1 to cont\n");
scanf("%d", &k);
}
while(k==1);
}
void strand_sort(struct node **f,struct node **g,struct node **h)
{
struct node *p,*temp,*temp2,*temp1,*q,*r,*s;
p=*f;
q=*g;
r=*h;
s=NULL;
if((*g)==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->next=NULL;
temp->info=p->info;
(*g)=temp;
q=temp;
(*f)=(*f)->next;
p=*f;
s=p;
printf("\n the 1st element in strand is %d",(*g)->info);
printf("\nfirst points to %d", (*f)->info);
printf("\ns point to %d",s->info);
}
else
{
while(p->next!=NULL)
{
if(p->info>q->info)
{
if((*f)==p)
{
temp1=(struct node *)malloc (sizeof(struct node));
temp1->info=p->info;
temp1->next=NULL;
q->next=temp1;
q=temp1;
(*f)=(*f)->next;
p=*f;
s=p;
printf("\nfirst points to %d", (*f)->info);
printf("s point to %d",s->info);
}
else
{
temp2=(struct node *)malloc(sizeof(struct node));
temp2->next=NULL;
temp2->info=p->info;
q->next=temp2;
q=temp2;
s->next=p->next;
p=s;
printf("\nthe first non first element intnserted %d",q->info);
}
}
else
{
s=p;
p=p->next;
}
}
}
printf("elemnet eneterd in d the first strand are");
r=*g;
while(r->next!=NULL)
{
printf("%d",r->info);
r=r->next;
}
merge(g,h);
}
void merge(struct node **q,struct node **r)
{
struct node *a,*b,*temp4,*temp5,*temp6,*temp7,*p;
a=*q;
if(*r==NULL)
{ temp4=(struct node *)malloc(sizeof(struct node));
temp4->info=a->info;
temp4->next=NULL;
*r=temp4;
p=temp4;
a=a->next;
while(a->next!=NULL)
{
temp5=(struct node *)malloc(sizeof(struct node));
temp5->info=a->info;
temp5->next=NULL;
p->next=temp5;
p=temp5;
a=a->next;
}
}
else
{
a=*q;
b=*r;
while(a->info<b->info&&a!=NULL)
{
temp6=(struct node *)malloc(sizeof(struct node));
temp6->next=NULL;
temp6->info=a->info;
temp6->next=b;
*r=temp6;
a=a->next;
}
if(a==NULL)
{
return ;
}
else if(a->info>b->info)
{
temp7=(struct node *)malloc(sizeof(struct node));
temp7->info=a->info;
temp7->next=b->next;
b=temp7;
a=a->next;
}
else{
b=b->next;
}
}
*q==NULL;
}
void display(struct node **z)
{
struct node *m;
m=*z;
while(m!=NULL)
{
printf("%d\n", m->info);
m=m->next;
}
}
This post has been edited by baavgai: 04 December 2012 - 08:10 AM
Reason for edit:: tag fixed

New Topic/Question
Reply



MultiQuote





|