/* First Look at the single-linked list of knowledge will soon be able to make
This uses three struct pointers, two p1,p2 points to the established list, and one P3 points to the new linked list
Judge P1p2 point to the data that small, small assignment to P3->next (ascending), the small P1 (p2) down one
The order of assignment is going to be right, or it's wrong.
*/
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
typedef struct NODE
{
int data;
struct node *next;
}lnode,*linklist;
int Len (linklist l);
void sort (linklist l);
void print (linklist l);
Linklist creat ();
linklist GB (linklist p,linklist Q);
int main ()
{
Linklist l,p,q,r,s;
L= (linklist) malloc (sizeof (Lnode));
l->next=null;
P=l;q=l;
P=creat ();
Sort (p);
Print (p);
printf ("\ n");
Q=creat ();
Sort (q);
Print (q);
printf ("\ n");
R=GB (P,Q);
Print (R);
Sort (r);
Print (R);
return 0;
}
Linklist creat ()
{
Linklist L,p,s;int I,n;
L= (linklist) malloc (sizeof (Lnode));
if (l==null)
{
printf ("Failed to allocate memory");
Exit (-1);
}
l->next=null;
l->data=-1;
P=l;
printf ("Length of the input list:");
scanf ("%d", &n);
for (i=0;i<n;i++)
{
S= (linklist) malloc (sizeof (Lnode));
if (s==null)
{
printf ("Failed to allocate memory");
Exit (-1);
}
s->next=null;
scanf ("%d", &s->data);
p->next=s;
P=s;
}
return l;
}
linklist GB (linklist p,linklist Q)
{
Linklist r,s,t,c;
C= (linklist) malloc (sizeof (Lnode));
c->next=null;
T=c;
r=p->next;
s=q->next;
while (R&&s)
{
if (R->data>=s->data)
{
t->next=s;
T=s;
s=s->next;
}
Else
{
t->next=r;
T=r;
r=r->next;
}
}
t->next=r?r:s;//put the remaining number in the list C
return C;
}
void print (linklist l)
{
Linklist p;
int n,j,i;
p=l->next;
while (P!=null)
{
printf ("%d", p->data);
p=p->next;
}
printf ("\ n");
}
void sort (linklist l)
{
Linklist q,p,s;
int i,j,temp;
For (I=0,p=l->next;i<len (L)-1; i++,p=p->next)
For (J=i+1,q=p->next;j<len (l); j++,q=q->next)
{
if (P->data > Q->data)
{temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
int Len (linklist l)
{
Linklist p,s;
int j=0;
p=l->next;
while (P!=null)
{
j + +;
p=p->next;
}
Return J;
}
Two single-linked lists combined and sorted