Two single-linked lists combined and sorted

Source: Internet
Author: User

/* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.