1025 Reverse list (25 points)

Today is stupid dead, wasted a long time. Hey blame me blind now.

 

Given a constant  K and a singly-linked list  L, the program will write a  L in each of  the K nodes inversion. For example: Given  L is. 1 → 2 → 3 → 4 →. 5 →. 6, K is 3, the output should be 3 → 2 → 1 → 6 → 5 → 4; if  K is 4, the output should be 4 → 3 → 2 → 1 → 5 → 6 , and last less than  K elements is not reversed.

Input formats:

Each input comprises a test. Each test case is given first line of the address of a node, the total number of node positive integer  N ( ≤), and a positive integer  K ( ≤), which requires the number of reverse link sub-point. Address of the node 5 are non-negative integers, NULL addresses  - Fig.

Then there are  N rows, each row of the format:

Address Data Next

Wherein  Address a node address, Data is stored in the node integer data, Next the address of the next node.

Output formats:

Linked list after each test case, the output order is reversed, each node on the same line accounted for, the input format.

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

如题,将一个链表排好再每k段反转,我写这道题目时没看清题目只把前k个反转了,最过分的是6个测试点还过了4个。。。心累。
不多bb,利用栈,每k个进行反转。
#include<bits/stdc++.h>
using namespace std;
struct emmm{
    int a;
    int b;
    int next;
};

int main(){
    emmm x[100001];
    int aa;
    cin>>aa;
    int st=aa;
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++) {
        cin>>aa;
        int m=aa;
        x[m].b=aa;
        cin>>x[m].a;
        cin>>x[m].next;
    } 
    stack<emmm> s;
    int f=1;
    n=0;
    int a=st;
    while(a!=-1){
        a=x[a].next;
        n++;
    }
    while(f){
        for(int i=0;i<k;i++){
            s.push(x[st]);
            st=x[st].next;
            
        }
        if(f==1){
            printf("%05d %d ",s.top().b,s.top().a);
            s.pop();
            f++;
        }
        
        for(int i=0;!s.empty();i++){
            printf("%05d\n%05d %d ",s.top().b,s.top().b,s.top().a);
            s.pop();
        }
        n=n-k;
        if(n<k){
            break;
        }
    }
    
    while(n--){
        if(k==0){
            printf("%05d %d ",x[st].b,x[st].a);
            k++;
        }else{
            printf("%05d\n%05d %d ",x[st].b,x[st].b,x[st].a);
        }
        if(x[st].next==-1){
            break;
        }
        st=x[st].next;
    }
    cout<<-1;
    return 0;
}

This is a big brother code

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct LNode
{
    int add;
    int data;
    int next;
}List[100000];
int main()
{
    int fa, n, k;
    vector<int>lb;
    cin >> fa >> n >> k;
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;
        cin >> List[temp].data;
        cin >> List[temp].next;
        List[temp].add = temp;
    }
    while (fa != -1)
    {
        lb.push_back(fa);
        fa = List[fa].next;
    }
    if (k)
    {
        for (int i = 0; i < lb.size(); i += k)
        {
            int end = i + k;
            if (end > lb.size())
                break;
            for (int j = i; j < i + (end - i) / 2; j++)
                swap(lb[j], lb[end - j + i - 1]);
        }
    }
    for (int i = 0; i < lb.size(); i++)
    {
        if (!i)
            printf("%05d %d ", lb[i], List[lb[i]].data);
        else
            printf("%05d\n%05d %d ", lb[i], lb[i], List[lb[i]].data);
    }
    cout << -1 ;
     return  0 ; 
}
 ---------------------  
Author: ScreaM__ 
Source: CSDN 
Original: HTTPS: // blog.csdn.net/ScreaM__/article/details / 84895022 
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

 

 

Guess you like

Origin www.cnblogs.com/siro/p/11129316.html