Cattle off network PAT B Zhenti reverse a linked list (25) (containing pit)

Cattle off network PAT B Zhenti reverse a linked list (25) (containing pit)

2000 ms time limit memory limit 150400 KB limit a code length determination program 100 KB Standard (from small)
Title Description
Given a constant K and a singly-linked list L, the program will write a L K nodes each inversion. For example: Given L of 1 → 2 → 3 → 4 → 5 → 6, K 3, then 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 Description:
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 (<= 105), and a positive integer K (<= N), which requires an inverted
sub hinge point number. Address of the node 5 are non-negative integers, NULL address is represented by -1.

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

Address Data Next

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

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

Input Examples:
00100. 4. 6
00000. 4 99999
00100 12309. 1
68 237 -1. 6
33 218 00000. 3
99999. 5 68 237
12309 2 33 218

Examples Output:
00000 33218. 4
33218. 3 12309
12309 2 00100
00100 99999. 1
99999 68 237. 5
68 237 -1. 6

Problem-solving ideas

At first glance, a simple question. But to enter the title, people really difficult. After reading, I suddenly remembered that my grandmother Yan static book list is a good thing, this is one idea.
This problem may be that, these three variables into the structure inside, arranged in order in accordance with good link
assignment inside into an array, then the array against reverse position, then, when the next value is output to the next node of the current node the address value

Pit

Hang this question is not careful, some node in the list, it needs its own statistics about the
AC code is as follows

#include<stdio.h>
struct node
{
    int data;
    int next;
}sq[200001];        //多1是为了加个链表表头
int main()
{
    for(int i=0;i<200001;i++)sq[i].next=i;      //把各个结点规整好
    int ini,N,K;
    scanf("%d%d%d",&ini,&N,&K);
    for(int i=0,a,b,c;i<N;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        sq[a].data=b;
        sq[a].next=c;
    }               //都安排上了,开整。其他的操作和链式存储差不多啊!
    N=0;
    for(int i=ini;i!=-1;i=sq[i].next)N++;       //这个很重要的!!!因为题中有些结点不在链表内,这是最大的坑了!!!
    int p,pre,coun=1,q;      //cou第几个结点了
    sq[200000].next=ini;
    pre=200000;             //我们的大表头
    while(coun<=N)        //即是还没到链表尾部
    {           //防止有无效的结点存在,影响判断
        if(N-coun+1>=K)     //如果大于K个,逆置。头插法
        {
            p=sq[pre].next;
            q=sq[p].next;
            for(int i=1;i<K;i++)
            {
                sq[p].next=sq[q].next;
                sq[q].next=sq[pre].next;            //头插呀,对吧
                sq[pre].next=q;              //把前面的头接好
                q=sq[p].next;
            }
            pre=p;
        }
        else
            break;
        coun=coun+K;
    }
    int i;     //大功告成,输出biubiu,biubiu(づ ̄3 ̄)づ╭❤~
    for(i=sq[200000].next;sq[i].next!=-1;i=sq[i].next)
        printf("%05d %d %05d\n",i,sq[i].data,sq[i].next);       //严格5位输出
    printf("%05d %d %d\n",i,sq[i].data,-1);      //最后的-1不是5位输出啊!
    return 0;
}
Published 18 original articles · won praise 2 · Views 227

Guess you like

Origin blog.csdn.net/zhou_hao_ran/article/details/103759522
Recommended