go语言合并两个有序链表 合并两个有序的单链表,合并后依然有序
实现两个链表的合并
#include
创新互联主营汉源网站建设的网络公司,主营网站建设方案,重庆APP开发,汉源h5成都微信小程序搭建,汉源网站营销推广欢迎汉源等地区企业咨询
#include
#define OK 1
#define NULL 0
#define TURE 1
#define FLASE 0
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
typedef int Status;
LinkList CreateLinkList_1(void) //产生连表
{
LinkList L,p,q;
int i,Len;
printf( "\nInput the Length of the LinkList: ");
scanf( "%d ",Len);
L=(LinkList)malloc(sizeof(LNode));
L- next=NULL;
L- data=Len;
p=L;
for(i=0;i next=NULL;
printf( "Input the data: ");
scanf( "%d ",q- data);
p- next=q;
p=p- next;
}
return L;
}
Status PrintLinkList(LinkList L) //打印链表
{
LinkList q;
q=L;
if(q!=NULL)
{
printf( "The Link 's Length is:%4d\n ",q- data);
q=q- next;
printf( "L ");
do
{
printf( "-- %d ",q- data);
q=q- next;
}
while(q!=NULL);
printf( "\n ");
}
return OK;
}
LinkList LinkListSort(LinkList L) //链表排序(冒泡)
{
int i,change;
LinkList p,q,s;
for(i=L- data-1,change=TURE;i =1change;i--)
{
change=FLASE;
p=L;
q=p- next;
while(q- next!=NULL)
{
if(p- next- data q- next- data)
{
p- next=q- next;
s=q;q=p- next;
if(q- next==NULL)
{
s- next=NULL;
q- next=s;
}
else
{
s- next=q- next;
q- next=s;
}
change=TURE;
}
p=p- next;
q=q- next;
}
}
return L;
}
LinkList LinkListMerge(LinkList La,LinkList Lb) //链表的合并
{
LinkList pa,pb,pc,Lc;
Lc=(LinkList)malloc(sizeof(LNode));
Lc- data=La- data+Lb- data;
pa=La- next;
pb=Lb- next;
Lc=pc=La;
while(papb)
{
if(pa- data =pb- data)
{
pc- next=pa;pc=pa;pa=pa- next;
}
else
{pc- next=pb;pc=pb;pb=pb- next;}
}
pc- next=pa?pa:pb;
free(Lb);
return Lc;
}
void main()
{
LinkList L1,L2,L3;
int n;
L1=CreateLinkList_1(); //产生链表L1 L2
PrintLinkList(L1);
L2=CreateLinkList_1();
PrintLinkList(L2);
LinkListSort(L1); //排序L1 L2
LinkListSort(L2);
printf( "\nAfter Sort:\n ");
PrintLinkList(L1); //打印排序后的链表
PrintLinkList(L2);
L3=LinkListMerge(L1,L2); //合并两个链表为一个新的有序的链表
PrintLinkList(L3);
printf( "\nPress any key to continue...\n ");
getch();
}
将两个有序单链表合并为一个有序单链表并输出其长度求大神指教
#include "stdlib.h"
#include "time.h"
struct link
{
int data;
struct link *next;
};
//不带头节点的链表
struct link *CreateLink(int n)
{ int temp,i;
struct link *head=NULL,*p=NULL,*q=NULL;
srand( n*1000);
temp = rand()%10;
head = (struct link *)malloc(sizeof(struct link));
head-data = temp;
head-next = NULL;
p = head;
for (i=1; in; i++)
{
q = (struct link *)malloc(sizeof(struct link));
temp = temp+rand()%8;
q-data = temp;
q-next = NULL;
p-next = q;
p = q;
}
return head;
}
void PrintLink(struct link *head)
{ int n=0;
struct link *p=head;
printf("Data: ");
while (p!=NULL)
{
printf("%d ",p-data);
p = p-next;
n++;
}
printf(" Nodes:%d\n",n);
}
struct link *MergeLink(struct link *h1, struct link *h2)
{
struct link *head,*tmp;
struct link *head1=h1,*head2=h2;
//哪个链表第一个节点值小,则把它的头指针作为合并后的头指针
if (h1-datah2-data)
{
head1 = h2;
head2 = h1;
}
head = head1;
tmp=NULL;
while (head2 != NULL)
{
while ( (head1-next-data head2-data) head1-next!=NULL)
{
head1 = head1-next;
}
tmp = head2;
head2 = head2-next;
tmp-next = head1-next;
head1-next = tmp;
}
return head;
}
int main()
{
struct link *head,*head1=NULL,*head2=NULL;
head1 = CreateLink(10);
PrintLink(head1);
head2 = CreateLink(13);
PrintLink(head2);
head = MergeLink(head1,head2);
PrintLink(head);
return 0;
}
合并两个有序列表
题目描述:
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1-2-4, 1-3-4
输出:1-1-2-3-4-4
Go语言版本解决方案:
名称栏目:go语言合并两个有序链表 合并两个有序的单链表,合并后依然有序
标题URL:http://pwwzsj.com/article/dojjdsg.html