【无标题】单链表的应用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

本文是对单链表的操作与应用,熟练掌握单链表的用法。


提示:以下是本篇文章正文内容,下面案例可供参考

一、要达到的目的?

1:从键盘中输入5个无序的整数,插入到单链表a中,并保证插入后单链表a中的数据要从大到小进行排序。

2:从键盘中输入5个无序的整数,插入到单链表b中,并保证插入后单链表b中的数据要从小到大进行排序。

3:输出这两个单链表中的交集,并集。

4:将这两个有序单链表合并成一个有序单链表(从大到小排序),将生成的有序单链表输出显示。对合并后的链表倒序排列(从小到大排序)。

5:复制倒序后的链表:例如合并链表为: 1 2 3 4 5, 复制后的结果是 1 1 2 2 3 3 4 4 5 5。

二、代码

/*
#include<iostream>
using namespace std;
struct Node
{
    char data;
    struct Node* next;
};

//定义集合类Gather的声明 
class Gather
{
public:
    Gather();//初始化集合 
    int Length(); //求单链表的长度
    void PrintList();//遍历
    bool Locate(char ch);//按值查找 
    bool Insert(char ch);//插入操作
    char Get(int i);//按位查找 
    void Paixu(Gather A);
    void Pxu(Gather B);
    void FZhi(Gather C);
    //Node*  HeBing(Node* A,Node* B);
    void HeBing2(Gather A);
    void JiaoJi(Gather B);//求交集
    void BingJi(Gather B);//求并集 
    void ChaJi(Gather B);//求差集 
    void Equal(Gather B);//判断是否相等
    
private:
    Node* first;
};
Gather::Gather()
{
    first = new Node;
    first = NULL;
}

//求单链表的长度
int Gather::Length()
{
    struct Node* p = first;
    int count = 0;
    while (p != NULL)
    {
        p = p->next;
        count++;
    }
    return count;
}
//遍历
void Gather::PrintList()
{
    struct Node* p = first;
    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}

//按值查找 
bool Gather::Locate(char ch)
{
    struct Node* p = first;
    while (p != NULL)
    {
        if (p->data == ch) return true;
        p = p->next;
    }
    return false;
}

//插入操作
bool Gather::Insert(char ch)
{
    Node* p = first, * s = NULL;
    if (first == NULL)
    {
        first = new Node;
        first->data = ch;
        first->next = NULL;
        return true;
    }
    else {
        while (p->next != NULL)
        {
            p = p->next;
        }
        s = new Node; s->data = ch;
        s->next = p->next; p->next = s;
        return true;
    }
}

//按位查找
char Gather::Get(int i)
{
    Node* p = this->first;
    int count = 0;
    while (p != NULL && count < i)
    {
        p = p->next;
        count++;
    }
    if (p == NULL) throw"查找位置错误";
    else return p->data;
}
void Gather::Paixu(Gather A)//从小到大排序
{
    Node* p , * q;
    p = this->first;
    int t;
    while (p != nullptr)
    {
        q = p->next;
        while (q != nullptr)
        {
            if (p->data > q->data)
            {
                t = p->data;
                p->data = q->data;
                q->data = t;
            }
            q = q->next;
        }
        p = p->next;
    }
}
void Gather::Pxu(Gather B)
{
    Node* p, * q;
    p = this->first;
    int temp;
    while (p != nullptr)
    {
        q = p->next;
        while (q != nullptr)
        {
            if (p->data < q->data)
            {
                temp = p->data;
                p->data = q->data;
                q->data = temp;
            }
            q = q->next;
        }
        p = p->next;
    }
}
void Gather:: HeBing2(Gather A)
{
    Node* p;
    Gather C;
    p = this->first;
    while (p != nullptr)
    {
        char sh = p->data;
        C.Insert(sh);
    }

}
void Gather::FZhi(Gather C)
{
    Node* p;
    Gather D;
    p = this->first;
    while (p != nullptr)
    {
        char ch = p->data;
        D.Insert(ch);
        D.Insert(ch);
        p = p->next;
    }
    D.PrintList();
    cout << endl;
}*/
/*Node* Gather::HeBing(Node* A, Node* B)
{
    if (A == nullptr)
        return B;
    if (B == nullptr)
        return A;
    Node* s = new Node();
    Node* p = s;
    while (A && B)
    {
        if (A->data < B->data)
        {
            p->next = A;
            A = A->next;
        }
        else
        {
            p->next = B;
            B = B->next;
        }
        p = p->next;
        if (A == nullptr)
            p->next = B;
        else if (B == nullptr)
            p->next = A;
        s = s->next;
        return s;
    }


}*/

/*void Gather::JiaoJi(Gather B)
{
    Gather C;
    Node* p = this->first;
    while (p != NULL)
    {
        char ch = p->data;
        if (B.Locate(ch) && !C.Locate(ch)) C.Insert(ch);
        p = p->next;
    }
    C.PrintList();
    cout << endl;
}
void Gather::BingJi(Gather B)
{
    Gather C;
    Node* p = this->first;
    while (p != NULL)
    {
        char ch = p->data;
        C.Insert(ch);
        p = p->next;
    }
    Node* q = B.first;
    while (q != NULL)
    {
        char ch = q->data;
        C.Insert(ch);
        q = q->next;
    }
    C.PrintList();
    C.Paixu(C);
    cout << "排序后:" << endl;
    C.PrintList();
    cout << "复制后:" << endl;
    C.FZhi(C);
    cout << endl;
}
void Gather::ChaJi(Gather B)
{
    Gather C;
    Node* p = this->first;
    while (p != NULL)
    {
        char ch = p->data;
        if (!B.Locate(ch) && !C.Locate(ch)) C.Insert(ch);
        p = p->next;
    }
    C.PrintList();
    cout << endl;
}
void Gather::Equal(Gather B)
{
    Node* p = this->first;
    if (this->Length() == B.Length()) {
        while (p != NULL)
        {
            char ch = p->data;
            if (B.Locate(ch)) p = p->next;
            else {
                cout << "集合A与集合B不相等!" << endl;
                return;
            }
        }
        cout << "集合A与集合B相等!" << endl;
    }
    else {
        cout << "集合A与集合B不相等!" << endl;
    }
}
int main()
{
    Gather A;
    Gather B;
    Gather C;
    char nameA[5];
    char nameB[5];
    cout << "输入A:" << endl;
    for (int i = 0; i < 5; i++)
    {
        cin >>nameA[i];
        A.Insert(nameA[i]);
    }
    cout << "成功:" << endl;
    cout << "排序:" << endl;
    A.Paixu(A);
    A.PrintList();
    cout << "输入B:" << endl;
    for (int j = 0; j < 5; j++)
    {
        cin >> nameB[j];
        B.Insert(nameB[j]);
    }
    cout << "ok" << endl;
    cout << "排序后:" << endl;
    B.Pxu(B);
    cout << "排序后的单链表:" << endl;
    B.PrintList();
    cout << "交集:" << endl;
    A.JiaoJi(B);
    cout << "并集:" << endl;
    A.BingJi(B);
    cout << "差集:" << endl;
    A.ChaJi(B);
    //C.HeBing();
    return  0;
}*/

结果图


 

总结

通过该代码可以对单链表的排序,交集,并集,差集,复制进行解决。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值