c语言尾插法函数输入数据 c语言尾插法代码( 二 )


node_t *p = l-head.next, *last = l-head, *t;
for (; p; last = p, p = p-next) {
if (value = https://www.04ip.com/post/p-value) {
t = (node_t*)malloc(sizeof(node_t));
t-value = https://www.04ip.com/post/value;
t-next = p;
last-next = t;
return;
}
}
/*如果没有小于或等于 value 的节点 , 则直接插入到末尾*/
list_push_back(l, value);
}
/*使用数组初始化有序链表*/
void list_init(list_t* l, int* p, int s) {
int i = 0;
l-head.next = NULL;
l-tail = l-head;
for (; is; ++i) {
list_push_sort(l, p[i]);
}
}
/*清空链表*/
void list_clear(list_t *l) {
node_t *p = l-head.next, *t;
while (p) {
t = p;
p = p-next;
free(t);
}
l-head.next = NULL;
l-tail = l-head;
}
/*合并有序链表*/
void list_merge(list_t *l, list_t *r, list_t *o) {
node_t *pl = l-head.next, *pr = r-head.next;
while (pl || pr) {
if (plpr) {
if (pl-value = https://www.04ip.com/post/pr-value) {
list_push_back(o, pl-value);
pl = pl-next;
} else {
list_push_back(o, pr-value);
pr = pr-next;
}
} else if (pl) {
list_push_back(o, pl-value);
pl = pl-next;
} else {
list_push_back(o, pr-value);
pr = pr-next;
}
}
}
/*删除相同结点*/
void list_duplicate_delete(list_t *l) {
if (l-head != l-tail) {
node_t *p = l-head.next, *last, *t;
int value = https://www.04ip.com/post/p-value;
last = p;
p = p-next;
while (p) {
if (value =https://www.04ip.com/post/= p-value) {
t = p;
last-next = p-next;
p = p-next;
free(t);
} else {
value = https://www.04ip.com/post/p-value;
last = p;
p = p-next;
}
}
}
}
/*打印链表*/
void list_show(char* name, list_t *l) {
node_t *p = l-head.next;
printf("%s: ", name);
for (; p; p = p-next) {
printf("%d, ", p-value);
}
printf("\n");
}
/*主函数*/
void main() {
list_t list1, list2, list3;
int a[] = { 10, 4, 6, 12, 1, 8, 14, 10, 14, 6 };
int b[] = { 7, 11, 6, 1, 13, 5, 1, 14 };
/*所有链表需要初始化后才能使用*/
list_init(list1, a, sizeof(a) / sizeof(int));
list_init(list2, b, sizeof(b) / sizeof(int));
list_init(list3, NULL, 0);
printf("初始值:\n");
list_show("List1", list1);
list_show("List2", list2);
list_show("List3", list3);
/*合并链表*/
list_merge(list1, list2, list3);
printf("合并后:\n");
list_show("List1", list1);
list_show("List2", list2);
list_show("List3", list3);
/*去重复*/
list_duplicate_delete(list3);
printf("去重复后:\n");
list_show("List1", list1);
list_show("List2", list2);
list_show("List3", list3);
/*所有链表都需要释放空间*/
list_clear(list1);
list_clear(list2);
list_clear(list3);
}
//这可是血汗钱啊.....
【c语言尾插法函数输入数据 c语言尾插法代码】关于c语言尾插法函数输入数据和c语言尾插法代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读