C语言创建升序降序链表并交替合并的技术问题
问题描述
需要实现两个链表:
- 第一个链表(FB链表)按id升序排列,每个节点包含
id和age字段 - 第二个链表(GS链表)按id降序排列,每个节点仅包含
id字段
随后将两个链表交替合并,且不改变各自原有顺序(示例:L1:1->3->4->6,L2:9->8->5->2,合并后为1->9->3->8->4->5->6->2)。
已编写相关代码但运行异常,附上完整代码如下:
原主函数代码
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include "function.h" struct nodeFB *startFB = NULL; struct nodeGS *startGS = NULL; struct newNodeFB *startNewFB = NULL; int main() { int id, age; scanf("%d", &id); while(id!=-1) { scanf("%d", &age); insertFB(&startFB, id, age); scanf("%d", &id); } scanf("%d", &id); while(id!=-1) { insertGS(&startGS, id); scanf("%d", &id); } printFB(startFB); printGS(startGS); createFinalList(&startNewFB,startFB,startGS); printAll(startNewFB); return 0; }
原结构体与自定义函数代码
#include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stdio.h> struct nodeFB { int id; int age; struct nodeFB *next; }; struct nodeGS { int id; struct nodeGS *next; }; struct newNodeFB { int id; int age; struct newNodeGS *next; }; struct newNodeGS { int id; struct newNodeFB *next; }; struct nodeFB *startFB; struct nodeGS *startGS; //functions struct nodeFB *insertFB( struct nodeFB **startFB, int id, int age) {//address of the first node in the linked list of FB struct nodeFB *newnode, *ptr; newnode = (struct nodeFB*)malloc(sizeof(struct nodeFB)); newnode->id = id; newnode->age = age; if (startFB == NULL) { newnode->next = NULL; *startFB = newnode; } else { ptr = *startFB; while(ptr->next!=NULL) { ptr=ptr->next; ptr->next= newnode; newnode->next = NULL; } } return *startFB; } void swap(struct nodeFB *a, struct nodeFB *b) {//function to swap two nodes int temp = a->id; a->id = b->id; b->id = temp; } void sortFB(struct nodeFB *startFB) { //function to bubble sort the given linked list int i; int swapped; struct nodeFB *ptr1; struct nodeFB *ptr2= NULL; if (startFB==NULL) { //checking for empty list return; } do { swapped = 0; ptr1=startFB; while (ptr1->next !=ptr2) { if (ptr1->id > ptr1->next->id) { swap (ptr1, ptr1->next); swapped = 1; } ptr1= ptr2->next; } ptr2=ptr1; } while (swapped); } void printFB(struct nodeFB *startFB) { //function to display the sorted list struct nodeFB *ptr; ptr = startFB; sortFB(ptr); while(ptr != NULL) { printf("%d %d/n", ptr->id, ptr->age); ptr=ptr->next; } } struct nodeGS *insertGS(struct nodeGS **startGS, int id ) { struct nodeGS *newnode, *ptr; newnode = (struct nodeGS*)malloc(sizeof(struct nodeGS)); newnode->id = id; if (startGS == NULL) { newnode->next = NULL; *startGS = newnode; } else { ptr = *startGS; while(ptr->next!=NULL) { ptr=ptr->next; ptr->next= newnode; newnode->next = NULL; } } return *startGS; } void swapGS(struct nodeGS *c, struct nodeGS *d) {//function to swap two nodes int temp = c->id; c->id = d->id; d->id = temp; } void sortGS(struct nodeGS *startGS) { //function to bubble sort the given linked list int i; int swapped; struct nodeGS *ptr1; struct nodeGS *ptr2= NULL; if (startGS==NULL) { //checking for empty list return; } do { swapped = 0; ptr1=startGS; while (ptr1->next !=ptr2) { if (ptr1->id < ptr1->next->id) { swapGS (ptr1, ptr1->next); swapped = 1; } ptr1= ptr2->next; } ptr2=ptr1; } while (swapped); } void printGS(struct nodeGS *startGS) { struct nodeGS *ptr; ptr = startGS; sortGS(startGS); while(ptr != NULL) { printf("%d/n", ptr->id); ptr = ptr->next; } } struct newNodeFB *createFinalList(struct newNodeFB **startNewFB, struct nodeFB *startFB, struct nodeGS *startGS ) { struct newNodeFB *temp1, *ptr1; temp1=(struct newNodeFB*) malloc(sizeof(struct newNodeFB)); temp1->id= startFB->id; temp1->age=startFB->age; struct newNodeGS *temp2; temp2->id= startGS->id; struct newNodeFB *temp3 = NULL; struct newNodeGS *temp4 = NULL; while (temp1 != NULL && temp2 != NULL) { ptr1=temp1; while (ptr1->next!=NULL) { ptr1=ptr1->next; ptr1->next= temp2; temp2->next=NULL; } temp3=temp1->next; temp4= temp2->next; temp1->next=temp2; temp2->next=temp3; temp1=temp3; temp2=temp4; } startGS = temp2; return startNewFB; } void printALL(struct newNodeFB *startNewFB){ struct newNodeFB *ptr; ptr= startNewFB; while(ptr != NULL) { printf("%d %d/n%d", startNewFB->id, startNewFB->age, startNewFB->id); ptr=ptr->next; } }
代码核心错误分析
- 插入函数逻辑错误:
insertFB和insertGS在循环内提前挂载新节点,导致链表仅能插入第一个节点,后续节点无法正确添加。正确逻辑应遍历到链表末尾再挂载。 - 排序函数致命错误:
sortFB和sortGS中ptr1 = ptr2->next会直接跳转到链表尾部,导致排序完全失效,应改为ptr1 = ptr1->next。 - 合并函数逻辑混乱:
temp2未分配内存就直接赋值,触发野指针错误- 未实现交替挂载逻辑,嵌套循环完全多余
- 指针赋值和返回值处理错误,无法正确初始化合并后的链表
- 打印函数错误:
- 换行符写成
/n,应为\n printALL始终打印头节点内容,未遍历当前节点,且未处理newNodeGS类型节点
- 换行符写成
- 排序同步问题:
sortFB仅交换id,未同步交换age,导致id和age不匹配。
修正后的完整代码
主文件(main.c)
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include "function.h" struct nodeFB *startFB = NULL; struct nodeGS *startGS = NULL; struct newNodeFB *startNewFB = NULL; int main() { int id, age; scanf("%d", &id); while(id != -1) { scanf("%d", &age); insertFB(&startFB, id, age); scanf("%d", &id); } scanf("%d", &id); while(id != -1) { insertGS(&startGS, id); scanf("%d", &id); } printFB(startFB); printGS(startGS); createFinalList(&startNewFB, startFB, startGS); printAll(startNewFB); return 0; }
函数与结构体定义(function.c)
#include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stdio.h> // 原链表结构体 struct nodeFB { int id; int age; struct nodeFB *next; }; struct nodeGS { int id; struct nodeGS *next; }; // 合并后的链表结构体(交替节点类型) struct newNodeGS; struct newNodeFB { int id; int age; struct newNodeGS *next; }; struct newNodeGS { int id; struct newNodeFB *next; }; // 插入FB链表(尾插法) struct nodeFB *insertFB(struct nodeFB **startFB, int id, int age) { struct nodeFB *newnode = (struct nodeFB*)malloc(sizeof(struct nodeFB)); newnode->id = id; newnode->age = age; newnode->next = NULL; if (*startFB == NULL) { *startFB = newnode; } else { struct nodeFB *ptr = *startFB; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = newnode; } return *startFB; } // 交换FB节点的id和age void swap(struct nodeFB *a, struct nodeFB *b) { int temp = a->id; a->id = b->id; b->id = temp; int tempAge = a->age; a->age = b->age; b->age = tempAge; } // FB链表升序排序(冒泡排序) void sortFB(struct nodeFB *startFB) { if (startFB == NULL) return; int swapped; struct nodeFB *ptr1; struct nodeFB *ptr2 = NULL; do { swapped = 0; ptr1 = startFB; while (ptr1->next != ptr2) { if (ptr1->id > ptr1->next->id) { swap(ptr1, ptr1->next); swapped = 1; } ptr1 = ptr1->next; } ptr2 = ptr1; } while (swapped); } // 打印FB链表 void printFB(struct nodeFB *startFB) { sortFB(startFB); struct nodeFB *ptr = startFB; while (ptr != NULL) { printf("%d %d\n", ptr->id, ptr->age); ptr = ptr->next; } printf("\n"); } // 插入GS链表(尾插法) struct nodeGS *insertGS(struct nodeGS **startGS, int id ) { struct nodeGS *newnode = (struct nodeGS*)malloc(sizeof(struct nodeGS)); newnode->id = id; newnode->next = NULL; if (*startGS == NULL) { *startGS = newnode; } else { struct nodeGS *ptr = *startGS; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = newnode; } return *startGS; } // 交换GS节点的id void swapGS(struct nodeGS *c, struct nodeGS *d) { int temp = c->id; c->id = d->id; d->id = temp; } // GS链表降序排序(冒泡排序) void sortGS(struct nodeGS *startGS) { if (startGS == NULL) return; int swapped; struct nodeGS *ptr1; struct nodeGS *ptr2 = NULL; do { swapped = 0; ptr1 = startGS; while (ptr1->next != ptr2) { if (ptr1->id < ptr1->next->id) { swapGS(ptr1, ptr1->next); swapped = 1; } ptr1 = ptr1->next; } ptr2 = ptr1; } while (swapped); } // 打印GS链表 void printGS(struct nodeGS *startGS) { sortGS(startGS); struct nodeGS *ptr = startGS; while (ptr != NULL) { printf("%d\n", ptr->id); ptr = ptr->next; } printf("\n"); } // 交替合并两个链表 void createFinalList(struct newNodeFB **startNewFB, struct nodeFB *fbPtr, struct nodeGS *gsPtr) { *startNewFB = NULL; struct newNodeFB *currentFB = NULL; struct newNodeGS *currentGS = NULL; while (fbPtr != NULL && gsPtr != NULL) { // 创建FB类型节点 struct newNodeFB *newFB = (struct newNodeFB*)malloc(sizeof(struct newNodeFB)); newFB->id = fbPtr->id; newFB->age = fbPtr->age; newFB->next = NULL; // 创建GS类型节点 struct newNodeGS *newGS = (struct newNodeGS*)malloc(sizeof(struct newNodeGS)); newGS->id = gsPtr->id; newGS->next = NULL; if (*startNewFB == NULL) { *startNewFB = newFB; } else { currentGS->next = newFB; } newFB->next = newGS; // 更新指针 currentFB = newFB; currentGS = newGS; fbPtr = fbPtr->next; gsPtr = gsPtr->next; } // 处理FB链表剩余节点 while (fbPtr != NULL) { struct newNodeFB *newFB = (struct newNodeFB*)malloc(sizeof(struct newNodeFB)); newFB->id = fbPtr->id; newFB->age = fbPtr->age; newFB->next = NULL; if (*startNewFB == NULL) { *startNewFB = newFB; } else { currentGS->next = newFB; currentGS = NULL; } currentFB = newFB; fbPtr = fbPtr->next; } // 处理GS链表剩余节点 while (gsPtr != NULL) { struct newNodeGS *newGS = (struct newNodeGS*)malloc(sizeof(struct newNodeGS)); newGS->id = gsPtr->id; newGS->next = NULL; if (*startNewFB == NULL) { struct newNodeFB *dummyFB = (struct newNodeFB*)malloc(sizeof(struct newNodeFB)); dummyFB->id = -1; dummyFB->age = -1; dummyFB->next = newGS; *startNewFB = dummyFB; } else { currentFB->next = newGS; } currentGS = newGS; gsPtr = gsPtr->next; } } // 打印合并后的链表 void printAll(struct newNodeFB *startNewFB) { struct newNodeFB *fbPtr = startNewFB; struct newNodeGS *gsPtr = NULL; while (fbPtr != NULL) { printf("%d %d\n", fbPtr->id, fbPtr->age); gsPtr = fbPtr->next; if (gsPtr != NULL) { printf("%d\n", gsPtr->id);
相关产品推荐
相关产品推荐

