飞雪寒露 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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能