Pat B-1025. Reverse chain list (-native)

Source: Internet
Author: User
Tags int size printf

Given a constant k and a single-linked table L, write a program that reverses every K-node in L. For example: Given L is 1→2→3→4→5→6,k 3, the output should be 3→2→1→6→5→4, and if K is 4, the output should be 4→3→2→1→5→6, that is, the last K elements do not invert.

Input format:

Each input consists of 1 test cases. The 1th row of each test case gives the address of the 1th node, the total number of nodes with positive integer N (<= 105), and the positive integer k (<=n), which is the number of sub-chain nodes that require inversion. The address of the node is a 5-bit nonnegative integer, and the null address is represented by-1.

Next there are n rows, each in the following format:

Address Data Next

Where address is a node location, data is the integer that the node holds, and next is the address of the next node.

Output format:

For each test case, sequentially output the inverted list with one row on each node, with the same format as the input.

Input Sample:
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

The problem of course is to set up a list node to save the address and the address of the subsequent node.

1, first of all, think how we find this node through the address of a node, it is easy to think of the method is that the address is his array subscript, so we need an array of nodes, in the input of this list of nodes, the address as subscript, the node is assigned to the corresponding subscript position, But this just establishes the relationship between the address and the data, they are discrete, and we have to save these nodes sequentially.

2, then here with a vector as a linked list, because the memory will explode when declaring an array, so that the vector will be better, here by the address to find the node, they string up.

3, finally we use a vector to save the inverted list, with the loop in addition to the last node of the next to modify the address of their next node.

#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std
    struct node{int address;
    int data; 
int next;
    int main () {int n,first,k;
    Vector<node> Shunxu;
    Vector<node> reverse;
    Node N;      Node addr[100000];
        Linked list array for (int i=0;i<n;i++) {cin>>n.address>>>>;  Addr[n.address]=n;   
    Assign the node to the position of the corresponding subscript} int nextaddress=first; 
        while (nextaddress! =-1) {//through Next as the subscript to find elements, added to the vector, update next continue to look for Shunxu.push_back (addr[nextaddress]);
    nextaddress = Addr[nextaddress].next; } int size=shunxu.size ();
    The input node may not be in the linked list, and the length of the linked list is recorded as int temp=k-1; while (temp<size) {//Invert list, each flip K, insufficient k does not reverse and exit loop for (int i=temp;i>temp-k;i--) {Reverse.push_b
        ACK (Shunxu[i]);
    } temp+=k; } for (int i=temp-k+1;i<size;i++)//will not be reversed at last, copied to the inverseThe linked list Reverse.push_back (Shunxu[i]) after the turn;
        for (int i=0;i<size-1;i++) {//modify their next, change to the next element's address reverse[i].next=reverse[i+1].address;  
    printf ("%05d%d%05d\n", reverse[i].address,reverse[i].data,reverse[i].next);

    } printf ("%05d%d%d\n", reverse[size-1].address,reverse[size-1].data,-1);
return 0; }

Topic Links:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.