鱼C论坛

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

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

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

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

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

x
救命!

在用来合并两个有序序列的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;
}
最佳答案
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();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

得知答案的我心情是复杂的,但我忍不住想要高呼“jhq999 nb!”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 02:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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