文章
145
粉丝
217
获赞
21
访问
90.0k
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}LNode,*LinkList;
//初始化链表
void InitList(LinkList &L,int s)
{
L = (LNode *)malloc(sizeof(LinkList));
L->next = NULL;
struct node *p = L->next;
while(s > 0)
{
struct node *t = (LNode *)malloc(sizeof(LinkList));
scanf("%d",&t->data);
if(!p)
L->next = t;
else
p->next = t;
p = t;
s--;
}
p->next = NULL;
}
//合并两个有序链表为L
void Merge_List(LinkList &L1,LinkList &L2)
{
struct node *p,*pre,*q,*qnext;
p = L1->next;
q = L2->next;
pre = L1;
while(p && q)
{
if(p->data < q->data)
{
pre = p;
p = p->next;
}
else
{
qnext = q->next;
pre->next = q;
q->next = p;
pre = q;
q = qnext;
}
}
if(!p && q)
pre->next = q;
}
//打印输出链表
void PrintList(LinkList L)
{
struct node *p = L->next;
while(p)
...
登录后发布评论
大佬代码规整又清晰,tql