编程算法之合并有序链表

Smarth
Smarth 这家伙很懒,还没有设置简介...

0 人点赞了该文章 · 24 浏览

编程算法之合并有序链表

  一些应用软件中有用到升序链表,这个就需要程序员在应用软件开发的时候写好升序链表的代码,下面就是一组升序链表的代码,想要升序链表代码的小伙伴都拿去吧。


  节点结构:

  struct Node{

  int val;

  Node *next;

  };

  要求实现的函数:

  Node* mergeList

  代码:

  /*

  * test.cpp

  *

  * Created on: 2014.04.24

  * Author: Spike

  */

  /*eclipse cdt, gcc 4.8.1*/

  #include

  #include

  using namespace std;

  struct Node{

  int val;

  Node *next;

  };

  Node* mergeList {

  if //递归的终止条件

  return list_b;

  else if

  return list_a;

  Node* pMergedHead = NULL; //合并后的链表

  if {

  pMergedHead = list_a; //指向头结点

  pMergedHead->next = mergeList; //递归

  } else {

  pMergedHead = list_b;

  pMergedHead->next = mergeList;

  }

  return pMergedHead;

  }

  Node* initList {

  Node* pHead = new Node;

  Node* pTemp = pHead;

  for ;>

  pTemp->val = vi[i];

  if -1) { //非尾结点

  Node* pNode = new Node;

  pTemp->next = pNode;

  pTemp = pTemp->next;

  }

  }

  pTemp->next = NULL;

  return pHead;

  }

  void printList {

  Node* pTemp = L;

  while {

  std::cout << ptemp-="">val <<>

  pTemp = pTemp->next;

  }

  std::cout << ptemp-="">val <<>

  std::cout << std::endl="">

  }

  int main {

  std::vector via = {1, 2, 3, 4, 5, 13};

  std::vector vib = {2, 4, 5, 7, 9, 11};

  Node* list_a = initList;

  Node* list_b = initList;

  std::cout << printlist="">

  std::cout << printlist="">

  Node* list_merge = mergeList;

  std::cout << printlist="">

  return 0;

  }

  输出:

  如果你想找升序链表服务,或者想要了解更多升序链表资讯,就上网。网入驻上百家专业应用程序开发公司,汇集全国千万优秀专业人才,提供最全面、最专业的升序链表服务。


发布于 2022-09-30 23:14

免责声明:

本文由 Smarth 原创或收集发布于 火鲤鱼 ,著作权归作者所有,如有侵权可联系本站删除。

火鲤鱼 © 2026 专注小微企业服务 冀ICP备09002609号-8