组合两链表,并拷贝至新链表中(C版)
时间渐渐紧张,只能在闲暇时间里写写这类的小程序,不断锻炼编程的思路:
compera_list.h:
#ifndef __COMPERA_LIST_H__ #define __COMPERA_LIST_H__ /* * list链表边界值设定 */ #define MAXSIZE_A 2 #define MAXSIZE_B 3 #define MAXSIZE_C 6 #include <stdio.h> #include <stdlib.h> /* * list可支持方法合集结构体 */ struct list_operations; /* * list结构体 */ struct list_str { unsigned char *name; unsigned int values; unsigned int private_cnt; struct list_str *next; struct list_operations *l_ops; }; /* * list可支持方法 */ struct list_operations { void (*init_list)(struct list_str *pList, int num_nodes); void (*sort_by_inc)(struct list_str *pList, int num_nodes); void (*print_list)(struct list_str *pList); }; /* * 可提供的外部接口API */ struct list_str *register_list(struct list_str *pList, int num_nodes, unsigned char *name); struct list_str *compera_list(struct list_str *pList1, struct list_str *pList2, struct list_str *pList3); #endif
compera_list.c:
#include "compera_list.h" /* * 初始化链表节点函数; * pList ---> 传入链表 * num_nodes ---> 链表节点数 */ void init_list(struct list_str *pList, int num_nodes) { int i; struct list_str *temp = pList; printf("输入%s链表节点值:\n", pList->name); for (i = 0; i < num_nodes; i++) { printf("第%d个节点值: ", i+1); scanf("%d", &pList->values); pList->next = ++temp; pList->private_cnt = num_nodes; if (i != num_nodes - 1) { pList++; } } pList->next = NULL; printf("\n"); } /* * 节点数据交换函数; * i ---> 前级数据 * j ---> 后继数据 */ void values_switch(int *i, int *j) { int temp; temp = *i; *i = *j; *j = temp; } /* * 递增排序函数; * pList ---> 传入链表 * num_nodes ---> 链表节点数 */ void sort_by_inc(struct list_str *pList, int num_nodes) { struct list_str *pTemp = pList; while (pList->next) { while (pTemp->next) { pTemp = pTemp->next; if (pList->values > pTemp->values) { values_switch(&pList->values, &pTemp->values); } else { continue; } } pList = pList->next; pTemp = pList; // 重置pTemp指针指向单元 } } /* * 打印节点数据函数; * pList ---> 传入链表 */ void print_list(struct list_str *pList) { unsigned int i; printf("%s节点值打印:\n", pList->name); for (i = 0; (pList != NULL) && (i < pList->private_cnt); i++) { printf("第%d节点值为: %d\n", i+1, pList->values); pList = pList->next; } printf("\n"); } struct list_operations list_ops = { .init_list = init_list, .sort_by_inc = sort_by_inc, .print_list = print_list, }; /* * 注册结构体函数; * pList ---> 传入链表 * num_nodes ---> 链表节点数 * name ---> 链表名字 */ struct list_str *register_list(struct list_str *pList, int num_nodes, unsigned char *name) { pList = (struct list_str *)malloc(sizeof(struct list_str) * num_nodes); pList->private_cnt = num_nodes; pList->name = name; pList->l_ops = &list_ops; return pList; } /* * 合并链表函数; * pList1 ---> 传入链表1 * pList2 ---> 传入链表2 * pList3 ---> 传入链表3 */ struct list_str *compera_list(struct list_str *pList1, struct list_str *pList2, struct list_str *pList3) { unsigned int i; struct list_str *temp = pList3; struct list_str *pRet = pList3; /* * 组合链表 */ for (i = 0; i < pList1->private_cnt; i++) { pList3->values = pList1->values; pList3++; if (NULL != pList1->next) { pList1++; } temp->next = pList3; temp = temp->next; } for (i = 0; i < pList2->private_cnt; i++) { pList3->values = pList2->values; pList3++; if (NULL != pList2->next) { pList2++; } else { break; } temp->next = pList3; temp = temp->next; } temp->next = NULL; /* * 指针归位操作 */ pList3 = pRet; return pList3; }
main.c:
#include "compera_list.h" /* * 声明要使用的全局变量: * *list_A; * *list_B; * *list_C; */ static struct list_str *list_A; static struct list_str *list_B; static struct list_str *list_C; int main(int argc, char *argv[]) { unsigned int size_A; unsigned int size_B; /* * size_A: 指定list_A长度 * size_B: 指定list_B长度 */ printf("输入A链表与B链表的长度(如2,3):"); scanf("%d,%d", &size_A, &size_B); printf("\n"); /* * 分配指针空间与构建list操作函数成员 */ list_A = register_list(list_A, size_A, "list_A"); list_B = register_list(list_B, size_B, "list_B"); list_C = register_list(list_C, (size_A + size_B), "list_C"); /* * 初始化节点值 */ list_A->l_ops->init_list(list_A, size_A); list_B->l_ops->init_list(list_B, size_B); /* * 按递增形式排序 */ list_A->l_ops->sort_by_inc(list_A, size_A); list_B->l_ops->sort_by_inc(list_B, size_B); /* * 组合list_A与list_B */ list_C = compera_list(list_A, list_B, list_C); /* * 按递增形式排序 */ list_C->l_ops->sort_by_inc(list_C, (size_A + size_B)); /* * 打印节点内容 */ list_A->l_ops->print_list(list_A); list_B->l_ops->print_list(list_B); list_C->l_ops->print_list(list_C); /* * 释放指针空间 */ free(list_A); free(list_B); free(list_C); return 0; }
在娴熟的技术,都源于不断的训练。