合并有序单链表并排序(遍历一次)
来源:互联网 发布:软件结项文档 编辑:程序博客网 时间:2024/05/20 11:24
LinkList MergeLinkList(LinkList L1, LinkList L2){ LinkList PnewHead = NULL; LinkList PL1 = L1; LinkList PL2 = L2; LinkList PtailNode = NULL; if (L1 == NULL) return L2; if (L2 == NULL) return L1; if (PL1->data<= PL2->data) { PnewHead = PL1; PtailNode = PL1; PL1 = PL1->next; } else { PnewHead = PL2; PtailNode = PL2; PL2 = PL2->next; } while (PL1&&PL2){ if (PL1->data < PL2->data) { PtailNode->next = PL1; PL1 = PL1->next; } else { PtailNode->next= PL2; PL2 = PL2->next; } PtailNode = PtailNode->next; } if (PL1 == NULL) PtailNode->next = PL2; if (PL2 == NULL) PtailNode->next = PL1; return PnewHead;}//测试函数test,c#define _CRT_SECURE_NO_WARNINGS 1typedef int DataType;#define NULL 0#include<stdio.h>#include<windows.h>#include<assert.h>typedef struct Node{ DataType data; struct Node*next;}Node, *LinkList;void InitList(LinkList *L){ *L = (Node*)malloc(sizeof(Node)); if (*L == NULL){ printf("申请内存空间失败"); } (*L)->next = NULL; (*L)->data = NULL;}LinkList BuyNode(DataType data){ LinkList NewNode = NULL; NewNode = (LinkList)malloc(sizeof(Node)); if (NewNode == NULL){ printf("为节点创建空间失败"); } NewNode->data = data; NewNode->next = NULL; return NewNode;}void PrintList(LinkList L){ LinkList Cur = L; if (L == NULL){ printf("NULL"); } while (Cur) { printf("%d--->", Cur->data); Cur = Cur->next; } printf("NULL\n");}void PushBack(LinkList* L, DataType data){ assert(L); LinkList Cur = *L; if (Cur == NULL){ Cur = BuyNode(data); } while (Cur->next) { Cur = Cur->next; } Cur->next = BuyNode(data);}LinkList MergeLinkList(LinkList L1, LinkList L2){ LinkList PnewHead = NULL; LinkList PL1 = L1; LinkList PL2 = L2; LinkList PtailNode = NULL; if (L1 == NULL) return L2; if (L2 == NULL) return L1; if (PL1->data<= PL2->data) { PnewHead = PL1; PtailNode = PL1; PL1 = PL1->next; } else { PnewHead = PL2; PtailNode = PL2; PL2 = PL2->next; } while (PL1&&PL2){ if (PL1->data < PL2->data) { PtailNode->next = PL1; PL1 = PL1->next; } else { PtailNode->next= PL2; PL2 = PL2->next; } PtailNode = PtailNode->next; } if (PL1 == NULL) PtailNode->next = PL2; if (PL2 == NULL) PtailNode->next = PL1; return PnewHead;}void test2(){ LinkList M = NULL; LinkList L = NULL; LinkList S = NULL; InitList(&M); InitList(&L); PushBack(&M, 1); PushBack(&M, 3); PushBack(&M, 5); PushBack(&L, 2); PushBack(&L, 4); PushBack(&L, 6); ///*PopBack(&M); //PopFront(&M); //Erase(&M, Find(&M,3)); //Insert(Find(&M,0), 3); //printf("%d\n",Size(M)); //BubbleSortLinkList(M); S = MergeLinkList(L, M); PrintList(S);}int main(){ test2(); system("pause"); return 0;}
阅读全文
0 0
- 合并有序单链表并排序(遍历一次)
- 合并两个有序数组并排序
- 排序单链表、 并两个有序链表, 合并后依然有序
- 合并两个有序链表/链表反转(逆置)/找链表倒数第k点(遍历一次)
- 合并两个有序数组(归并排序)
- 排序13:有序数组合并
- 单链表的操作(添加,遍历,反序,排序,合并)
- 两个有序数组的合并排序,Java代码实现,并去重复,考虑空间利用率问题
- 合并有序单链表(递归实现)
- 有序单链表合并(递归法)
- 合并两个有序单链表
- 有序单链表的合并
- 有序单链表的合并
- 合并两个有序单链表
- 合并两个有序单链表
- 两个有序单链表合并
- 合并有序单链表
- leetcode有序单链表合并
- css的overflow属性
- Util-BaseApplication
- 【第五周项目4】数制转换
- PPT制作流程 --- 早看早超生
- 黑客工具软件大全100套
- 合并有序单链表并排序(遍历一次)
- Studio像Eclipse一样设置switch/case那样的代码块
- 搜狗2016研发工程师编程题
- iOS-Xcode 上传App Store遇到 Unexpected CFBundleExecutable Key问题
- sql文件反向生成物理概念模型
- 黑客工具软件大全100套
- leetcode
- 网络安全之相关攻击方式总结
- Kotlin从入门到放弃之基础篇(五)——修饰词、扩展、数据类