C++ 已知单链表L=(a1,a2,a3,…an).设计一个算法实现单链表的重新排列,即得到L‘=(a1,an,a2,an-1,a3,an-2…..)

实验思路:

  1. 寻找链表的中点,把链表分为两部分。
  2. 后半部分进行逆置。
  3. 前半部分链表和后半部分链表结合起来。

核心代码:

1.查找中点

    Node* fast = head->next;

    Node* slow = head->next;

    Node* slowpre = slow;

    while (fast != nullptr && slow != nullptr && fast->next!= nullptr)

    {

        slowpre = slow;

        slow = slow->next;

        fast = fast->next->next;

    }

    slowpre->next = nullptr;

2.倒序   

 Node* head2=new Node;

    head2->next = nullptr;

    Node* p, * s;

    s= slow;

    p = slow->next;

    while (p)

    {

        s->next = head2->next;

        head2->next = s;

        s = p;

        p = p->next;

        if(p==nullptr)

        {

            s->next = head2->next;

            head2->next = s;

        }

    }

    Node* second = head2->next;

    delete head2;

  1. 合并链表

   

Node *first=head->next;

   Node *head1=first;

   Node *tail=head1;

   first=first->next;

   while(first!=nullptr && second!=nullptr)

   {

           tail->next=second;

           second=second->next;

             tail=tail->next;



      tail->next=first;

        first=first->next;

       tail=tail->next;

   }

   tail->next=(first==nullptr)?second:first;

全部代码:

#include<iostream>
using namespace std;
struct Node{
    int date;
    Node* next = nullptr;
};
int main()
{
    Node *head=new Node;
    Node *re=head;
      int a[11];
        for (int i = 0; i < 11; ++i)
        a[i] = i + 1;
     for (int i = 0; i < 11; ++i)
     {
         Node *s=new Node;
         s->date=a[i];
         s->next=re->next;
         re->next=s;
         re=s;
     }

     //表达数据
      Node* x1 = head->next;
    if (x1 != nullptr)
    {
        cout << "初始数据为:" << endl;
        while (x1 != nullptr)
        {
            cout << "   " << x1->date;
            x1 = x1->next;
        }
        cout << endl;
    }
    else
    cout << "表中无数据" << endl;
//查找中点
    Node* fast = head->next;
    Node* slow = head->next;
    Node* slowpre = slow;
    while (fast != nullptr && slow != nullptr && fast->next!= nullptr)
    {
        slowpre = slow;
        slow = slow->next;
        fast = fast->next->next;
    }
    slowpre->next = nullptr;

//倒序
    Node* head2=new Node;
    head2->next = nullptr;
    Node* p, * s;
    s= slow;
    p = slow->next;
    while (p)
    {
        s->next = head2->next;
        head2->next = s;
        s = p;
        p = p->next;
        if(p==nullptr)
        {
            s->next = head2->next;
            head2->next = s;
        }
    }
    Node* second = head2->next;
    delete head2;


//合并链表
   Node *first=head->next;

   Node *head1=first;
   Node *tail=head1;
   first=first->next;
   while(first!=nullptr && second!=nullptr)
   {
           tail->next=second;
           second=second->next;
             tail=tail->next;

      tail->next=first;
        first=first->next;
       tail=tail->next;
   }
   tail->next=(first==nullptr)?second:first;

//表达数据
      Node* p1 = head1;
    if (p1 != nullptr)
    {
        cout << "变化后数据为:" << endl;
        while (p1 != nullptr)
        {
            cout << "   " << p1->date;
            p1 = p1->next;

        }
        cout<<endl;
    }
    else
    cout << "表中无数据" << endl;


        Node* p2 = head;
    while (p2)
    {
        Node* q = p2;
        p2 = p2->next;
        delete q;//从头开始删除
    }

    return 0;
}


 

结果截图:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辣鲨椒鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值