02-线性结构2 Reversing Linked List (25分)

02-线性结构2 Reversing Linked List   (25分)

Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL being 1→2→3→4→5→6, if K3K=3, then you must output 3→2→1→6→5→4; if K4K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NN (105105) which is the total number of nodes, and a positive KK (NN) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then NN lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
主要思路:
1、逆转链表时同时存在三个记录连续位置的变量
2、每逆转一组链表后,须把当前链表的最后一个结点链接到下一组链表的第一个结点
3、每逆转一组链表后,须把上一组链表的最后一个结点链接到当前链表的第一个结点
4、使用C++时setfill和setw两个函数的使用
//
//  main.cpp
//  02线性结构2
//
//  Created by 佘一夫 on 16/3/24.
//  Copyright © 2016年 佘一夫. All rights reserved.
//

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

#define Max_Node_Num 100002

typedef struct node
{
    int data;
    int next;
}Node;

int Reverse(int head,int K,int N,vector<Node>& vec)//分区域逆转链表的核心函数
{
    int num=N/K;//需要逆转的大小为K的子链表的个数
    int New_Begin=0;//逆转链表后新的开始结点的地址
    int flag=0;//最早的K个结点逆转后存储新的开始地址的标志
    while (num--)
    {
        int cnt=1,tmp;
        int New=vec[head].next;//head为上一组链表逆转后的最后一个结点
        int Record=New;
        int Old=vec[New].next;
        while (cnt<K)//依次逆转本组链表中的每个结点
        {
            tmp=vec[Old].next;
            vec[Old].next=New;
            New=Old;
            Old=tmp;
            cnt++;
        }
        vec[vec[head].next].next=Old;//处理完一组后把本组当前最后一个结点(逆转前为本组第一个结点)指向下一组第一个结点
        vec[head].next=New;//上一组逆转后的链表最后一个结点指向本组逆转后的第一个结点
        head=Record;//本组逆转后的最后一个结点作为下一组链表未逆转时的头结点
        if (flag==0)//记录整个链表的新的开始地址(即为第一组链表逆转后的第一个结点的地址)
        {
            flag=1;
            New_Begin=New;
        }
    }
    return New_Begin;
}

int main(int argc, const char * argv[])
{
    //Input
    vector<Node> List(Max_Node_Num);
    int begin=0,N=0,K=0;
    cin>>begin>>N>>K;
    int front,value,rear;
    for (int i=0; i<N; ++i)
    {
        cin>>front>>value>>rear;
        List[front].data=value;
        List[front].next=rear;
    }
   
    
    //Process
    int Real_N=0;
    int Begin_temp=begin;
    while (Begin_temp!=-1)
    {
        Begin_temp=List[Begin_temp].next;
        Real_N++;//Real_N代表给出的结点中在链表之中的有效结点的个数
    }
    
    List[100001].next=begin;
    int head=100001;
    int new_begin=Reverse(head, K,Real_N, List);
    
    //Output
    while (new_begin!=-1)
    {
        if (List[new_begin].next==-1)//输出-1不填充0,分开处理
        {
            cout<<setfill('0')<<setw(5)<<new_begin<<' '<<List[new_begin].data<<' '<<List[new_begin].next;
            //setfill函数在setw函数规定的大小未满时填充‘0’
        }else
        {
            cout<<setfill('0')<<setw(5)<<new_begin<<' '<<List[new_begin].data<<' '<<setfill('0')<<setw(5)<<List[new_begin].next;
        }
        new_begin=List[new_begin].next;
        cout<<endl;
    }
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值