-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
executable file
·97 lines (93 loc) · 1.79 KB
/
1.cpp
File metadata and controls
executable file
·97 lines (93 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
typedef struct List//创建链表结构体
{
int data;
struct List *next;
} List,*Listlink;
Listlink creatList() //生成链表
{
Listlink current,previous,head;
int length,i=0;
head=NULL;
previous=NULL;
current=NULL;
scanf("%d",&length);//输入链表的长度
for(i=0;i<length;i++)//通过循环控制链表的创建
{
current=(Listlink)malloc(sizeof(List));
if(head==0)
{
head=current;
}
if(previous!=NULL)
{
previous->next=current;
}
scanf("%d",¤t->data);
//cin>>current->data;
current->next=NULL;
previous=current;
}
return head;
}
Listlink mergeLinklist(Listlink la, Listlink lb)//链表的合并
{
Listlink cHead;
cHead=NULL;
Listlink cCurrent;
cCurrent=NULL;
if(la->data < lb->data)
{
cHead=la;
la=la->next;
}
else
{
cHead=lb;
lb=lb->next;
}
cCurrent=cHead;
while((la!=NULL) && (lb!=NULL))
{
if(la->data < lb->data)
{
cCurrent->next=la;
la=la->next;
}
else
{
cCurrent->next=lb;
lb=lb->next;
}
cCurrent=cCurrent->next;
}
if(la!=NULL)
{
cCurrent->next=la;
}
if(lb!=NULL)
{
cCurrent->next=lb;
}
return cHead;
}
void printList(const Listlink head)//打印链表
{
const List* phead ;
phead=head;
while ( phead!= NULL)
{ printf("%d ",phead->data);
//cout << phead->data<<" ";
phead = phead->next;
}
}
int main()
{
Listlink La, Lb, Lc;
La = creatList();
Lb = creatList();
Lc = mergeLinklist(La, Lb);
printList(Lc);
return 0;
}