【课程设计|C++】实现两个链表的合并

举报
海轰Pro 发表于 2022/06/16 23:02:06 2022/06/16
1.3k+ 0 0
【摘要】 目录 前言1. 实现两个链表的合并基本功能与要求测试数据 代码实验截图结语 前言 Hello! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~   自...

前言

Hello!
非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
 
自我介绍 ଘ(੭ˊᵕˋ)੭
昵称:海轰
标签:程序猿|C++选手|学生
简介:因C语言结识编程,随后转入计算机专业,获得过国家奖学金,有幸在竞赛中拿过一些国奖、省奖…已保研。
学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
 
唯有努力💪
 

知其然 知其所以然!

1. 实现两个链表的合并

基本功能与要求

(1)建立两个链表A和B,链表元素个数分别为m和n个。

(2)假设元素分别为(x1,x2,…,xm),和(y1,y2,…,yn)。把它们合并成一个线性表C:
当m>=n时,C=x1,y1,x2,y2,…,xn,yn,…,xm
当n>m时, C=y1,x1,y2,x2,…,ym,xm,…,yn;

(3)输出线性表C。

(4)用直接插入排序法对C进行升序排序,生成链表D,并输出链表D。

测试数据

(1) A表(30,41,15,12,56,80)
B表(23,56,78,23,12,33,79,90,55)

(2) A表(30,41,15,12,56,80,23,12,34)
B表(23,56,78,23,12)

代码

#include <iostream>
using namespace std;

template <class t>
class node
{
public:
    t data;
    node<t> *next;
    node(node<t> *ptr = NULL) { next = ptr; }
    node(t a, node<t> *ptr = NULL)
    {
        data = a;
        next = ptr;
    }
};

template <class t>
class linklist
{
public:
    void merge(linklist<t> &l1, linklist<t> &l2)
    {
        int m = l1.length();
        int n = l2.length();
        node<t> *r;
        node<t> *s;
        r = first;
        node<t> *p = l1.first->next;
        cout << p->data << endl;
        node<t> *q = l2.first->next;
        if (m >= n)
        {
            while (p != NULL && q != NULL)
            {
                s = new node<t>(p->data);
                r->next = s;
                r = s;
                s = new node<t>(q->data);
                r->next = s;
                r = s;
                p = p->next;
                q = q->next;
            }
            r->next = p;
        }
        else
        {
            while (p != NULL && q != NULL)
            {
                s = new node<t>(q->data);
                r->next = s;
                r = s;
                s = new node<t>(p->data);
                r->next = s;
                r = s;
                p = p->next;
                q = q->next;
            }
            r->next = q;
        }
    }
    void sort()
    {
        node<t> *L = first->next;
        node<t> *p, *q, *pre;
        p = L->next->next;
        L->next->next = NULL;
        while (p)
        {
            q = p->next;
            pre = L;
            while (pre->next != NULL && pre->next->data < p->data)
                pre = pre->next;
            p->next = pre->next;
            pre->next = p;
            p = q;
        }
        // first = L
        // node<t> *r;
        // node<t> *p = l.first->next;
        // r = first;
        // node<t> *newNode = new node<t>(p->data);
        // r->next = newNode;
        // r = newNode;
        // p = p->next;
        // while(p != NULL) {
        //     t num = r->data;
        //     newNode = new node<t>(p->data);
        //     if(p->data > num) {
        //         // newNode = new node<t>(p->data);
        //         r->next = newNode;
        //         r = newNode;
        //     } else {

        //     }
        //     p = p->next;
        // }
    }
    void rel()
    {
        node<t> *p;
        node<t> *s;
        p = first->next;
        s = p->next;
        first->next = NULL;
        while (p != NULL)

        {
            p->next = first->next;
            first->next = p;
            p = s;
            // cout<<"1"<<endl;
            if (p != NULL)

            {
                s = p->next;
            }
        }
    }
    linklist() { first = new node<t>; }
    linklist(t x) { first = new node<t>(x); }
    void creat(t end)
    {
        node<t> *newnode;
        t val;
        cin >> val;
        while (val != end)
        {
            newnode = new node<t>(val);
            newnode->next = first->next;
            first->next = newnode;
            cin >> val;
        }
    }

    void creat1(t end)
    {
        node<t> *r;
        node<t> *s;
        r = first;
        t val;
        cin >> val;
        while (val != end)
        {

            s = new node<t>(val);
            r->next = s;
            r = s;
            cin >> val;
        }
        // first=new node<t>
        r->next = NULL;
    }
    void print()
    {

        node<t> *p = first->next;
        while (p != NULL)
        {
            cout << p->data << "--->>";
            p = p->next;
        }
        cout << "NULL" << endl;
    }

    int length()
    {
        node<t> *p;
        p = first->next;
        int count = 0;
        while (p != NULL)
        {
            p = p->next;
            count++;
        }
        return count;
    }
    t get(int i)
    {

        node<t> *p;
        int count = 1;
        p = first->next;

        while (p != NULL && count < i)
        {
            p = p->next;
            count++;
        }
        if (p == NULL)
            throw "位置";
        else
            return p->data;
    }

    void insert(t x, int i)
    {
        node<t> *p;
        node<t> *s;
        p = first;
        int count = 0;
        while (p != NULL && count < i - 1)
        {
            p = p->next;
            count++;
        }
        if (p == NULL)
            throw "位置";
        else
        {
            s = new node<t>(x);
            s->next = p->next;
            p->next = s;
        }
    }

    int locate(t x)
    {
        node<t> *p;
        p = first->next;
        int count = 1;
        while (p != NULL)
        {
            if (p->data == x)
                return count;
            p = p->next;
            count++;
        }
        return 0;
    }

    void del(int i)
    {
        node<t> *p;
        node<t> *q;
        p = first;
        int count = 0;
        while (p != NULL && count < i - 1)
        {
            p = p->next;
            count++;
        }
        if (p == NULL || p->next == NULL)
            throw "位置";
        else
        {
            q = p->next;
            p->next = q->next;
            delete q;
        }
    }

    ~linklist()
    {
        node<t> *p;
        while (first != NULL)
        {

            p = first;
            first = first->next;
            delete p;
        }
    }

    // private:
    node<t> *first;
};

linklist<int> merge(linklist<int> &l1, linklist<int> &l2)
{
    int m = l1.length();
    int n = l2.length();
    cout << "m =" << m << endl;
    linklist<int> ans;
    // linklist<int> ans;
    // cout << "((" << endl;
    node<int> *cur = ans.first;
    cout << "((" << endl;
    // cout << ans.first->data;
    node<int> *p = l1.first;
    node<int> *q = l2.first;
    if (m >= n)
    {
        while (p != NULL && q != NULL)
        {
            cur->next = p;
            cur = cur->next;
            cur->next = q;
            cur = cur->next;
            p = p->next;
            q = q->next;
        }
        cur->next = p;
    }
    else
    {
        while (p != NULL && q != NULL)
        {
            cur->next = q;
            cur = cur->next;
            cur->next = p;
            cur = cur->next;
            p = p->next;
            q = q->next;
        }
        cur->next = q;
    }
    return ans;
}
int main()
{

    linklist<int> L;
    linklist<int> A;
    linklist<int> B;
    linklist<int> C;
    linklist<int> D;
    int key = 1;
    int choice;
    int choice1;
    int choice2;
    while (key == 1)
    {
        cout << "+++++++++++++++++++++++++++++" << endl;
        cout << "+       单链表              +" << endl;
        cout << "+  1.创建单链表(头部插入) +" << endl;
        cout << "+  2. 创建单链表(尾部插入)+" << endl;
        cout << "+  3.查找第i个结点的值      +" << endl;
        cout << "+  4.查找元素x的结点位置    +" << endl;
        cout << "+  5.在第i个位置插入元素x   +" << endl;
        cout << "+  6.删除第i个位置的值      +" << endl;
        cout << "+  7.输出单链表各元素       +" << endl;
        cout << "+  8.旋转链表               +" << endl;
        cout << "+  9.新建两个链表               +" << endl;
        cout << "+  10.直接插入排序              +" << endl;
        cout << "输入你的选择:(1---10)" << endl;
        cin >> choice;
        switch (choice)
        {
        case 1:
        {
            cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
            L.creat(0);
            cout << "单链表创建完成!" << endl;
        }
        break;
        case 2:
        {
            cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
            L.creat1(0);
            cout << "单链表创建完成!" << endl;
        }
        break;
        case 3:
        {

            cout << "输入查找元素的位置i(第i个结点):";
            cin >> choice1;
            cout << "第" << choice1 << "个结点的值是:";
            cout << L.get(choice1) << endl;
        }
        break;
        case 4:
        {
            cout << "输入查找元素的值:";
            cin >> choice1;

            cout << "该元素在第" << L.locate(choice1) << "个位置!" << endl;
        }
        break;
        case 5:
        {
            cout << "输入插入元素的位置(第i个) :";
            cin >> choice1;
            cout << "输入插入元素的值:";
            cin >> choice2;
            L.insert(choice2, choice1);
            cout << "插入完成!" << endl;
        }
        break;
        case 6:
        {
            cout << "输入删除元素的位置(第i个):" << endl;
            cin >> choice1;
            L.del(choice1);
            cout << "删除完成!" << endl;
        }
        break;
        case 7:
        {
            cout << "该链表元素的值是:" << endl;
            L.print();
        }
        break;
        case 8:
        {

            L.rel();
            // cout<<"1"<<endl;
        }
        case 9:
        {
            cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
            A.creat1(0);
            cout << "单链表创建完成!" << endl;

            cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
            B.creat1(0);
            cout << "单链表创建完成!" << endl;

            C.merge(A, B);

            C.print();
            // break;
        }
        break;
                case 10:
        {
            C.sort();
            C.print();
        }
        break;
        }
        cout << "是否继续?(1:继续 0:退出)" << endl;
        cin >> key;
        system("cls");
    }
    return 0;
}

  
 

实验截图

菜单
在这里插入图片描述

合并两个链表

在这里插入图片描述
直接插入排序

在这里插入图片描述

结语

文章仅作为个人学习笔记记录,记录从0到1的一个过程

希望对您有一点点帮助,如有错误欢迎小伙伴指正

在这里插入图片描述

文章来源: haihong.blog.csdn.net,作者:海轰Pro,版权归原作者所有,如需转载,请联系作者。

原文链接:haihong.blog.csdn.net/article/details/125306492

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。