chenlifeng 发表于 2022-6-12 22:09:31

合并两个单链表出问题了

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

//单链表
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

struct node {
        int data;
        node * next;
        node(){
                next = NULL;
        }
};

//--------------------------------------------------
class list {
        private:
                node * head, * rear;
        public:
                list(){head = rear = NULL;}
                void create();                //构建链表,从键盘输入数据
                void create_2();        //构建链表,自动生成数据,有序
                void print();                //输出链表到屏幕
                ~list();                        //释放内存
                void sort(list b);                //排序
                void merge(node * f1, node * e1, node * f2, node * e2);
                node * get_head();
};
//--------------------------------------------------
node * list::get_head() {
        return head;
}
//--------------------------------------------------
list::~list() {
        node * t;
        while(head) {
                t = head;
                head = head->next;
                delete t;
        }
}

//--------------------------------------------------
void list::print() {
        node * t = head->next;
        while(t != NULL) {
                cout << t->data << " ";
                t = t->next;
        }
        cout << endl;
}

//--------------------------------------------------
void list::create() {
        head = new node;
        rear = head;
       
        node * t;
        int n(0), t_data(0);
        cin >> n;        //数据的数量
        for(int i(0); i < n; ++i) {
                t = new node;        //建立新结点
                cin >> t_data;
                t->data = t_data;
               
                rear->next = t;
                rear = rear->next;
        }
}
//--------------------------------------------------
void list::create_2() {
        srand(time(0));
        int data(0);
       
        head = new node;
        rear = head;
       
        node * t;
        int n(0), t_data(0);
        cin >> n;        //数据的数量
        for(int i(0); i < n; ++i) {
                t = new node;        //建立新结点
                data += rand() % 6;
                t_data = data;
                t->data = t_data;
               
                rear->next = t;
                rear = rear->next;
        }
}

void list::sort(list b) {
        node * a1(head), * b1(b.get_head());
        node * t(NULL);
        b1 = b1->next;
        while(b1 != NULL && a1->next != NULL) {
                if(b1->data <= a1->next->data) {
                        t = b1->next;
                        b1->next = a1->next;
                        a1->next = b1;
                        b1 = t;
                }
                else {
                        a1 = a1->next;
                }
                print();
        }
        if(b1 != NULL) {
                a1->next = b1;
        }
        cout << "last:" << endl;
        print();
}

int main() {
        list a, b;
        a.create_2();
        b.create_2();
        a.print();
        b.print();
        a.sort(b);
        cout << "b:" << endl;
        cout << "a:" << endl;
        a.print();
        return 0;
}

jhq999 发表于 2022-6-13 06:54:10

本帖最后由 jhq999 于 2022-6-13 07:02 编辑

看看
运行一下没错啊?
3 3
3 4 5
4 7 11
3 4 5
3 4 4 5
3 4 4 5
3 4 4 5
3 4 4 5
last:
3 4 4 5 7 11
b:
a:
3 4 4 5 7 11

jhq999 发表于 2022-6-13 08:49:42

本帖最后由 jhq999 于 2022-6-13 09:41 编辑

void list::sort(list *b) {///////////////注意类实例的析构
      node * a1(head), * b1(b->get_head());
      node * t(NULL);
      b1 = b1->next;
      while(b1 != NULL && a1->next != NULL) {
                if(b1->data <= a1->next->data) {
                        t = b1->next;
                        b1->next = a1->next;
                        a1->next = b1;
                        b1 = t;
                }
                else {
                        a1 = a1->next;
                }
                print();
      }
      if(b1 != NULL) {
                a1->next = b1;
      }
        b->get_head()->next=NULL;//////////////////////如果不设为null,主程序结束时实例b会析构,会释放已经在a析构时已经释放的数据
      cout << "last:" << endl;
      print();
}

chenlifeng 发表于 2022-6-13 23:23:23

jhq999 发表于 2022-6-13 08:49


得知答案的我心情是复杂的,但我忍不住想要高呼“jhq999 nb!”{:9_232:}
页: [1]
查看完整版本: 合并两个单链表出问题了