pat-basic-1025-Reverse Linked List

Source: Internet
Author: User

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, andnext 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 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


A reversal of every k element

/*
DATA:
1 6 3
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 6-1
*/

#include <bits/stdc++.h>#defineMAXN 100000+50using namespacestd; structdata{intaddress; intdata; intNext;  };  DATA ARR[MAXN]; Vector<DATA>ans; Stack<DATA>s; intsrcaddress, N, K; intMain () {scanf ("%d%d%d", &srcaddress, &n, &k);  for(inti =0; I < n; ++i) {          intPOS; scanf ("%d", &POS); Arr[pos].address=POS; scanf ("%d%d", &arr[pos].data, &arr[pos].next); }       Do{ans.push_back (arr[srcaddress]); Srcaddress=Arr[srcaddress].next; } while(Srcaddress! =-1); //printf ("%d\n", Ans.size ());     intSZ =ans.size (); //k = min (k, SZ);     intpos =0; /*for (int i = 0; i < K/2; ++i) {DATA tmp = ans[i];         Ans[i] = ans[(k-1)-i];     ans[(k-1)-i] = tmp; }*/       while(Pos+k <=SZ) {           while(!S.empty ())          {S.pop (); }           for(inti = pos; i < K+pos; ++i)          {S.push (ans[i]); }           while(!S.empty ()) {Ans[pos++] =S.top ();          S.pop (); }         //pos + = k;     }       for(inti =0; I < sz-1; ++i) {printf ("%05d%d%05d\n", Ans[i].address, Ans[i].data, ans[i+1].address); } printf ("%05d%d%d\n", ans[sz-1].address, ans[sz-1].data,-1); return 0; }  
Capouis ' CODE



pat-basic-1025-Reverse Linked List

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: info-contact@alibabacloud.com 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.