鱼C论坛

 找回密码
 立即注册
查看: 1824|回复: 3

[已解决]合并两个单链表出问题了

[复制链接]
发表于 2022-6-12 22:09:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
救命!

在用来合并两个有序序列的sort()函数那里,我将两个单链表合并了(将b的结点插到a链表里),然后在这个函数里输出合并后的单链表很正常。但是我再到main函数里面输出那个单链表的时候就会输出乱七八糟的东西。

  1. //单链表
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<ctime>
  5. using namespace std;

  6. struct node {
  7.         int data;
  8.         node * next;
  9.         node(){
  10.                 next = NULL;
  11.         }
  12. };

  13. //--------------------------------------------------
  14. class list {
  15.         private:
  16.                 node * head, * rear;
  17.         public:
  18.                 list(){head = rear = NULL;}
  19.                 void create();                //构建链表,从键盘输入数据
  20.                 void create_2();        //构建链表,自动生成数据,有序
  21.                 void print();                //输出链表到屏幕
  22.                 ~list();                        //释放内存
  23.                 void sort(list b);                //排序
  24.                 void merge(node * f1, node * e1, node * f2, node * e2);
  25.                 node * get_head();
  26. };
  27. //--------------------------------------------------
  28. node * list::get_head() {
  29.         return head;
  30. }
  31. //--------------------------------------------------
  32. list::~list() {
  33.         node * t;
  34.         while(head) {
  35.                 t = head;
  36.                 head = head->next;
  37.                 delete t;
  38.         }
  39. }

  40. //--------------------------------------------------
  41. void list::print() {
  42.         node * t = head->next;
  43.         while(t != NULL) {
  44.                 cout << t->data << " ";
  45.                 t = t->next;
  46.         }
  47.         cout << endl;
  48. }

  49. //--------------------------------------------------
  50. void list::create() {
  51.         head = new node;
  52.         rear = head;
  53.        
  54.         node * t;
  55.         int n(0), t_data(0);
  56.         cin >> n;        //数据的数量
  57.         for(int i(0); i < n; ++i) {
  58.                 t = new node;        //建立新结点
  59.                 cin >> t_data;
  60.                 t->data = t_data;
  61.                
  62.                 rear->next = t;
  63.                 rear = rear->next;
  64.         }
  65. }
  66. //--------------------------------------------------
  67. void list::create_2() {
  68.         srand(time(0));
  69.         int data(0);
  70.        
  71.         head = new node;
  72.         rear = head;
  73.        
  74.         node * t;
  75.         int n(0), t_data(0);
  76.         cin >> n;        //数据的数量
  77.         for(int i(0); i < n; ++i) {
  78.                 t = new node;        //建立新结点
  79.                 data += rand() % 6;
  80.                 t_data = data;
  81.                 t->data = t_data;
  82.                
  83.                 rear->next = t;
  84.                 rear = rear->next;
  85.         }
  86. }

  87. void list::sort(list b) {
  88.         node * a1(head), * b1(b.get_head());
  89.         node * t(NULL);
  90.         b1 = b1->next;
  91.         while(b1 != NULL && a1->next != NULL) {
  92.                 if(b1->data <= a1->next->data) {
  93.                         t = b1->next;
  94.                         b1->next = a1->next;
  95.                         a1->next = b1;
  96.                         b1 = t;
  97.                 }
  98.                 else {
  99.                         a1 = a1->next;
  100.                 }
  101.                 print();
  102.         }
  103.         if(b1 != NULL) {
  104.                 a1->next = b1;
  105.         }
  106.         cout << "last:" << endl;
  107.         print();
  108. }

  109. int main() {
  110.         list a, b;
  111.         a.create_2();
  112.         b.create_2();
  113.         a.print();
  114.         b.print();
  115.         a.sort(b);
  116.         cout << "b:" << endl;
  117.         cout << "a:" << endl;
  118.         a.print();
  119.         return 0;
  120. }
复制代码

最佳答案
2022-6-13 08:49:42
本帖最后由 jhq999 于 2022-6-13 09:41 编辑
  1. void list::sort(list *b) {///////////////注意类实例的析构
  2.         node * a1(head), * b1(b->get_head());
  3.         node * t(NULL);
  4.         b1 = b1->next;
  5.         while(b1 != NULL && a1->next != NULL) {
  6.                 if(b1->data <= a1->next->data) {
  7.                         t = b1->next;
  8.                         b1->next = a1->next;
  9.                         a1->next = b1;
  10.                         b1 = t;
  11.                 }
  12.                 else {
  13.                         a1 = a1->next;
  14.                 }
  15.                 print();
  16.         }
  17.         if(b1 != NULL) {
  18.                 a1->next = b1;
  19.         }
  20.         b->get_head()->next=NULL;//////////////////////如果不设为null,主程序结束时实例b会析构,会释放已经在a析构时已经释放的数据
  21.         cout << "last:" << endl;
  22.         print();
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-13 06:54:10 | 显示全部楼层
本帖最后由 jhq999 于 2022-6-13 07:02 编辑

看看
运行一下没错啊?
  1. 3 3
  2. 3 4 5
  3. 4 7 11
  4. 3 4 5
  5. 3 4 4 5
  6. 3 4 4 5
  7. 3 4 4 5
  8. 3 4 4 5
  9. last:
  10. 3 4 4 5 7 11
  11. b:
  12. a:
  13. 3 4 4 5 7 11
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-13 08:49:42 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-6-13 09:41 编辑
  1. void list::sort(list *b) {///////////////注意类实例的析构
  2.         node * a1(head), * b1(b->get_head());
  3.         node * t(NULL);
  4.         b1 = b1->next;
  5.         while(b1 != NULL && a1->next != NULL) {
  6.                 if(b1->data <= a1->next->data) {
  7.                         t = b1->next;
  8.                         b1->next = a1->next;
  9.                         a1->next = b1;
  10.                         b1 = t;
  11.                 }
  12.                 else {
  13.                         a1 = a1->next;
  14.                 }
  15.                 print();
  16.         }
  17.         if(b1 != NULL) {
  18.                 a1->next = b1;
  19.         }
  20.         b->get_head()->next=NULL;//////////////////////如果不设为null,主程序结束时实例b会析构,会释放已经在a析构时已经释放的数据
  21.         cout << "last:" << endl;
  22.         print();
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-6-13 23:23:23 | 显示全部楼层

得知答案的我心情是复杂的,但我忍不住想要高呼“jhq999 nb!”
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 23:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表