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

来源:互联网 发布:免费发送短信软件 编辑:程序博客网 时间:2024/05/21 07:49
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 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218

Sample Output:

00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 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 100002typedef 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 0
原创粉丝点击