输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然按照递增排序
来源:互联网 发布:淘宝助手菜鸟模板 编辑:程序博客网 时间:2024/05/21 15:46
算法描述:
输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然按照递增排序
算法实现:
/*************************************************************************> File Name: main.c> Author: cyf> Mail: XXX@qq.com> Created Time: 2016年05月18日 星期三 14时01分15秒 ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "mergeList.h"#include "List.h"struct ListNode* Test(char* testName, struct ListNode* pHead1, struct ListNode* pHead2){ if(testName != NULL) printf("%s begins:\n", testName); printf("The first list is:\n"); PrintList(pHead1); printf("\nThe second list is:\n"); PrintList(pHead2); printf("\nThe merged list is:\n"); struct ListNode* pMergedHead = mergeList(pHead1, pHead2); PrintList(pMergedHead); printf("\n\n"); return pMergedHead;}// list1: 1->3->5// list2: 2->4->6void Test1(){ struct ListNode* pNode1 = CreateListNode(1); struct ListNode* pNode3 = CreateListNode(3); struct ListNode* pNode5 = CreateListNode(5); ConnectListNodes(pNode1, pNode3); ConnectListNodes(pNode3, pNode5); struct ListNode* pNode2 = CreateListNode(2); struct ListNode* pNode4 = CreateListNode(4); struct ListNode* pNode6 = CreateListNode(6); ConnectListNodes(pNode2, pNode4); ConnectListNodes(pNode4, pNode6); struct ListNode* pMergedHead = Test("Test1", pNode1, pNode2); DestroyList(pMergedHead);}int main(){Test1();return 0;}
/*************************************************************************> File Name: mergeList.h> Author: cyf> Mail: XXX@qq.com> Created Time: 2016年05月18日 星期三 13时45分25秒 ************************************************************************/#ifndef _MERGELIST_H#define _MERGELIST_H#include "List.h"/* * 输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然按照递增排序 * */struct ListNode *mergeList(struct ListNode * pHead1, struct ListNode *pHead2);#endif
/*************************************************************************> File Name: mergeList.c> Author: cyf> Mail: XXX@qq.com> Created Time: 2016年05月18日 星期三 13时55分40秒 ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "mergeList.h"struct ListNode *mergeList(struct ListNode *pHead1, struct ListNode *pHead2){if (pHead1 == NULL)return pHead2;else if (pHead2 == NULL)return pHead1;struct ListNode *pRet = NULL;if (pHead1->data < pHead2->data){pRet = pHead1;pRet->next = mergeList(pHead1->next, pHead2);}else if (pHead1->data >= pHead2->data){pRet = pHead2;pRet->next = mergeList(pHead1, pHead2->next);}return pRet;}
/************************************************************************> File Name: List.h> Author: cyf> Mail: 1097189275@qq.com > Created Time: 2016年03月23日 星期三 17时27分15秒 ************************************************************************/#ifndef ALGRITHMN_LIST_H#define ALGRITHMN_LIST_Hstruct ListNode{int data;struct ListNode *next;};struct ListNode *CreateListNode(int value);void ConnectListNodes(struct ListNode *pCurrent, struct ListNode *pNext);void PrintListNode(struct ListNode *pNode);void PrintList(struct ListNode *pHead);void AddToTail(struct ListNode **pHead, int value);void RemoveNode(struct ListNode **pHead,int value);unsigned int GetListLength(struct ListNode *pHead);void DestroyNode(struct ListNode *pNode);void DestroyList(struct ListNode *pHead);#endif
</pre><pre name="code" class="cpp">/*************************************************************************> File Name: List.c> Author: cyf> Mail: 1097189275@qq.com > Created Time: 2016年03月23日 星期三 17时32分11秒 ************************************************************************/#include "List.h"#include <stdio.h>#include <stdlib.h>/* * 创建一个链表的结点 * */struct ListNode *CreateListNode(int value){struct ListNode *pNode =(struct ListNode *)malloc(sizeof(struct ListNode));pNode->data = value;pNode->next = NULL;return pNode;}/* * 将两个结点连接起来 * */void ConnectListNodes(struct ListNode *pCurrent, struct ListNode *pNext){if(!pCurrent){printf("ERROR!connect two ListNode.\n");exit(1);}pCurrent->next = pNext;}/* * 打印结点 * */void PrintListNode(struct ListNode *pNode){if(!pNode){printf("the node is null!\n");exit(1);}printf("The value of the node is %d\n",pNode->data);}/* * 打印链表 * */void PrintList(struct ListNode *pHead){printf("print the list:\n");struct ListNode *pNode = pHead;if(!pNode){printf("the list is null\n");exit(1);}while(pNode != NULL){printf("%d ",pNode->data);pNode = pNode->next;}}/* * 在链表结尾添加结点 * */void AddToTail(struct ListNode **pHead, int value){struct ListNode *pNew = (struct ListNode*)malloc(sizeof(struct ListNode));pNew->data = value;pNew->next = NULL;if(!pHead)*pHead = pNew;else{struct ListNode *pNode = *pHead;while(pNode != NULL){pNode = pNode->next;}pNode->next = pNew;}}/* * 删除链表中结点值为value的结点 * */void RemoveNode(struct ListNode **pHead, int value){if(pHead == NULL||*pHead == NULL){printf("the list is null");return;}struct ListNode *ToBeDel = NULL;if((*pHead)->data == value){ToBeDel = *pHead;*pHead = (*pHead)->next;}else{struct ListNode *pNode = *pHead;while(pNode->next != NULL && pNode->next->data != value)pNode = pNode->next;if(pNode->next != NULL && pNode->next->data == value){ToBeDel = pNode->next;pNode->next = pNode->next->next;}}if(ToBeDel != NULL){free(ToBeDel);ToBeDel = NULL;}}/* * 计算链表的长度 * */unsigned int GetListLength(struct ListNode *pHead){unsigned int length = 0;struct ListNode *pNode = pHead;while(pNode != NULL){++length;pNode = pNode->next;}return length;}/* * 销毁一个结点 * */void DestroyNode(struct ListNode *pNode){free(pNode);pNode = NULL;}void DestroyList(struct ListNode* pHead){struct ListNode *pNode = pHead;while (pNode != NULL){pHead = pHead->next;free(pNode);pNode = pHead;}}
.SUFFIXEX:.c.oCC = gccCFLAGS = -g -O2 -WallSRCS = main.c\ List.c\ mergeList.cOBJS = $(SRCS:.c=.o)EXE = mainall:$(OBJS)$(CC) $(OBJS) -o $(EXE) $(CFLAGS).c.o:$(CC) -o $@ -c $(CFLAGS) $<clean:rm -rf $(OBJS) $(EXE)
0 0
- 输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然按照递增排序
- 合并两个递增排序的链表
- 合并两个递增排序链表
- C语言——合并两个递增排序链表
- 合并两个递增链表
- 链表合并 求两个递增链表的并
- 合并两个递增的链表
- 合并两个递增的链表
- 用C语言合并两个按递增排序的链表
- 合并两个递增链表为一个递增链表
- 剑指offer--合并两个递增链表
- [C练习]合并两个递增链表
- 将两个递增的链表合并成一个递减的链表(相同的元素只留下一个),并要求利用原表结点
- 将两个递增的有序链表合并为一个递增的有序链表
- 【面试题】剑指offer17--合并两个递增的链表,结果还是递增
- 【C++】合并两个排序的链表,要求合并后仍然是有序的
- 合并两个已经按递增排序的文件
- 合并两个链表并排序
- 灰帽子Python 学习记录 8 内存断点
- 第十七章 Autorotation, Popover Controllers, and Modal View Controllers翻译
- java的String和StringBuilder类的替换方法的运用。
- 代码覆盖率工具---Emma
- maven项目打jar,war包方法
- 输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然按照递增排序
- 为什么百度地图只显示网格不显示内容
- Apple pay
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- linux源码安装iostat,dstat,systat的方法分享
- TCP协议中的三次握手和四次挥手(图解)
- linux驱动之--输入子系统
- UITableViewController与UIViewController的区别
- intellij + gradle + jetty + spring mvc step by step - 1