飞雪寒露 2021-10-21 17:25 采纳率: 50%
浏览 70
已结题

单链表问题,涉及排序去重等操作。

img

  • 写回答

2条回答 默认 最新

  • 关注

    效果图及代码如下,如有帮助,请帮忙采纳一下,谢谢。

    img

    代码:

    #include <iostream>
    using namespace std;
    
    struct StNode 
    {
        int data;
        struct StNode* next;
    };
    
    //创建节点
    StNode* CreateNode(int d)
    {
        StNode* node = new StNode;
        node->data = d;
        node->next = 0;
        return node;
    }
    //创建链表
    StNode* CreateList()
    {
        StNode* head,*p,*t;
        head = 0;
        p = head;
        t = head;
        int data;
        cout << "请输入链表中各节点的值(在一行中输入多个数字,以空格分隔,以回车结束):" <<endl;
        while(1)
        {
            cin >> data;
            t = CreateNode(data);
            if(head ==0)
            {
                head = t;
                p = head;
            }
            else
            {
                p->next = t;
                p = t;
            }
    
            if (cin.get() == '\n') 
                break;
    
        }
        return head;
    }
    //打印链表
    void Display(StNode* head)
    {
        cout << "打印链表:" << endl;
        while(head)
        {
            cout << head->data << " ";
            head = head->next;
        }
        cout << endl;
    }
    
    //判断是否重复
    int isRepeat(StNode* head,StNode* p)
    {
        while(head)
        {
            if(head == p) return 0; //只判断p之前的
            if(head->data == p->data) return 1;
            else
                head = head->next;
        }
        return 0;
    }
    
    
    //去重
    StNode* Quchong(StNode* head)
    {
        StNode *p,*t,*k,*pre;
        p = head->next;
        pre = head;
        while(p)
        {
            if(isRepeat(head,p))
            {
                t = p->next;
                pre->next = t;
                delete p;
                p = t;
            }else
            {
                pre = p;
                p = p->next;
            }
        }
        return head;
    }
    
    //插入递增链表
    StNode* InsertDz(StNode* head, StNode* p)
    {
        StNode* t,*pre;
        if(head == 0) return p;
        if(p->data < head->data)
        {
            p->next = head;
            head = p;
            return head;
        }
        
        pre = head;
        t = head->next;
        while(t && t->data < p->data)
        {
            pre = t;
            t = t->next;
        }
        pre->next = p;
        p->next = t;
        return head;
    }
    
    
    //拆分列表
    void SplitList(StNode* head,StNode* &L1,StNode* &L2)
    {
        StNode* p;
        L1 = 0;
        L2 = 0;
        p = head;
        while (head)
        {
            p = head->next;
            head->next = 0;
            if(head->data%2==0)
            {
                L1 = InsertDz(L1,head);
            }else
            {
                L2 = InsertDz(L2,head);
            }
            head = p;
        }
        
    }
    
    void Free(StNode* head)
    {
        StNode* p = head;
        while(head)
        {
            p = head->next;
            delete head; head = p;
        }
    }
    
    
    int main()
    {
        StNode* A,*B,*C;
        A = CreateList();
        Display(A);
    
        cout << "去重,";
        A = Quchong(A);
        Display(A);
        
        SplitList(A,C,B); //B是奇数 C是偶数
        cout << "分裂后的链表:"<< endl;
        Display(B);
        Display(C);
        //释放空间
        Free(B);
        Free(C);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月10日
  • 已采纳回答 11月2日
  • 请采纳用户回复 10月31日
  • 创建了问题 10月21日

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄