1
#include
<
stdio.h
>
2
#include
<
malloc.h
>
3
#define
NULL 0
4
5
typedef
struct
lnode
{
6
char data;
7
struct lnode *next;
8
}
*
link,
*
position;
9
10
11
typedef
struct
{
12
link head,tail;
13
int len;
14
}
linklist;
15
16
/**/
/*=================================*/
17
void
print(linklist l)
{ /**//*---print---在屏幕上输出链表的所有元素*/
18
link p=NULL;
19
p=l.head;
20
if(!p->next)
{
21
printf("\nThe linklist is empty.\n\n");
22
return ;
23
}
24
printf("The list:");
25
while(p)
{
26
printf("%c-",p->data);
27
p=p->next;
28
}
29
}
30
31
32
int
compare(
char
a,
char
b)
{ /**//*---compare---比较两个元素的大小关系*/
33
return a-b;
34
}
35
36
37
int
length(link s)
{ /**//*---length---求链的长度*/
38
int i=0;/**//*i不赋初值,编译错误“可能在i定义以前使用了它在length函数中”*/
39
link p=NULL;
40
p=s;
41
while(p->next!=NULL)
{
42
p=p->next;
43
i++;
44
}
45
return i;
46
}
47
48
49
position makenode(
char
e)
{ /**//*---分配由p指向的结点并赋值为e---*/
50
link p=NULL;
51
p=(link)malloc(sizeof(struct lnode));
52
/**//*---struct lnode 才能表示一个结构体类型并可用于分配空间的元素类型定义-*/
53
if(p)
{
54
p->data=e;
55
p->next=NULL;
56
}
57
else return;
58
return p;
59
}
60
61
62
char
getcurelem(link p)
{ /**//*---返回p所指结点中元素的值-*/
63
return p->data;
64
}
65
66
67
int
initlist(linklist
*
l)
{ /**//*---构造一个由l指向的空的线性表-*/
68
l->head=makenode('L');
69
l->head->next=NULL;/**//*不是l->head=NULL*/
70
l->tail=l->head;
71
l->len=0;
72
return 1;
73
}
74
75
76
int
append(linklist
*
l,link s)
{ /**//*---将指针s所的一串结点链接在线性表L的最后一个结点-*/
77
link q;
78
q=l->head;
79
if(!l->tail)
{/**//*考虑到链表为空的情况*/
80
l->head->next=s;
81
while(q->next) q=q->next;/**//*尾结点的处理*/
82
l->tail=q;
83
}
84
l->tail->next=q=s;
85
while(q->next) q=q->next;/**//*不能忘了对尾结点的处理*/
86
l->tail=q;
87
l->len+=length(s);
88
89
}
90
91
92
position delfirst(linklist
*
l,link q)
{ /**//*---删除表中第一个结点并以q返回-*/
93
if(!l->head->next)
{
94
printf("\nThe linklist is empty,can not delete..\n");
95
return 0;
96
}
97
q=l->head->next;
98
l->head->next=l->head->next->next;
99
return q;
100
}
101
102
103
104
105
int
mergelist_l(linklist
*
la,linklist
*
lb,linklist
*
lc)
{ /**//* 算法2.19*/
106
char a,b;
107
link q=NULL;
108
109
position pa=NULL,pb=NULL;
110
111
pa=la->head->next;
112
pb=lb->head->next;
113
114
if(!initlist(lc)) return 0;
115
printf("oopop"); print(*la);
116
while(pa&&pb)
{
117
a=getcurelem(pa); b=getcurelem(pb);
118
119
if(compare(a,b)<=0)
{
120
121
q=delfirst(la,q);
122
append(lc,q);
123
pa=la->head->next;
124
125
}
126
else
{
127
q=delfirst(lb,q);
128
append(lc,q);
129
pb=lb->head->next;
130
131
}
132
}
133
134
135
if(pa) append(lc,pa);
136
else append(lc,pb);
137
138
return 1;
139
}
140
141
142
143
/**/
/*============主函数部分=========*/
144
145
main()
{
146
147
int i=0;
148
char temp;
149
link s,n;
150
linklist *l,*la=NULL,*lb,*lc=NULL;
151
initlist(la);initlist(lb);initlist(lc);
152
153
154
155
temp='A';
156
la->head=s=makenode('X');
157
for(i=1;i<=10;i++,temp+=2)
{
158
s->next=makenode(temp);
159
s=s->next;
160
}/**//*构造以(*la).head为头结点的链表*/
161
printf("\nla="); print(*la);
162
163
164
temp='B';
165
lb->head=n=makenode('X');
166
for(i=1;i<=10;i++,temp+=2)
{
167
n->next=makenode(temp);
168
n=n->next;
169
}/**//*构造以(*lb).head为头结点的链表*/
170
printf("\nlb="); print(*lb);
171
172
mergelist_l(la,lb,lc);
173
/**//*归并la、lb指向的线性链表为lc*/
174
printf("\nThe mergelisted link lc,"); print(*lc);
175
176
177
178
getch();
179
}
180
181

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

98

99

100

101

102

103

104

105



106

107

108

109

110

111

112

113

114

115

116



117

118

119



120

121

122

123

124

125

126



127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143


144

145



146

147

148

149

150

151

152

153

154

155

156

157



158

159

160


161

162

163

164

165

166



167

168

169


170

171

172

173


174

175

176

177

178

179

180

181
