PAT 1025 B et al. (Inverse Chain List) c++.

1025. Inverse Chain List (25)

Time limit
300 ms
Memory Limit
65536 kB
Code length limit
8000 B
Decision Procedure
Standard
author
CHEN, Yue

Given a constant K and a single-chain list L, write a program to invert each K node in L.For example, given L 1_2_3_4_5_6 and K 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, that is, no more than K elements will be inverted.

Input format:

Each input contains one test case.Line 1 of each test case gives the address of the first node, a positive integer N (<= 105) for the total number of nodes, and a positive integer K (<=N), the number of subchain nodes requiring inversion.The address of the node is a 5-bit non-negative integer, and the NULL address is represented by -1.

Next, there are N lines, each in the format:

Address Data Next

Address is the address of the node, Data is the integer data stored by the node, and Next is the address of the next node.

Output format:

For each test case, the inverted chain table is output sequentially, with each node on a row in 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
Output sample:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1


Ideas: This topic can use a structure to define node Node, and c++ template class vectors to encapsulate it. It can traverse node vectors of type vector <Node> like an array. It is necessary to have a certain understanding of the basic properties of the chain table, and I believe you should have no problem (reference data structure).The first time you solve a problem, you omitted a situation where the input node may not be in the list. This is caused by fixed-minded thinking, which is fatal and a mental loophole.


Algorithms:

1. Enter all nodes and store them with Node1[address]

2. Starting from the first address, find successors of nodes in Node 1 in turn until node.next=-1, automatically skip nodes that are not in the list of chains, and store nodes in Node 2 in turn

3. Reverse Node2 nodes, group them as required, store them in Node 3. Reverse implementation depends on the code.


4. Traversing through the output Node3, the following implementation saves some time by not changing the node's successor address in Node3, but outputting it by accident.


This is not recommended because in practice, new operations are usually performed on the inverted list.


#include <iostream> 
#include <cstdio>
#include <vector>
using namespace std;

struct Node{
	int address;  //address
	int data;   //Node Data
       int next;    //Node Successor Address
};

vector<Node> Node1(100000);   //Address stores the corresponding node at the subscript location
vector<Node>  Node2;   //Chain list node to store before inversion 
vector<Node> Node3;    //Used to store inverted list nodes 

//Swap a,b's positions 
void swap(Node &a, Node &b){
	Node temp = a;
	a = b;
	b = temp;
}


/*
test case
00100 6 2
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

There are nodes in the middle that are not on the list 
00001 6 2
00001 1 00002
00002 2 00003 
00006 3 00007
00003 4 00004
00004 5 00005
00005 6 -1 
*/
int main(){
	
	//Receive Input 
	int firstAdd,n,k;
	scanf("%d%d%d", &firstAdd, &n,&k);
	
	Node temp;
	for(int i=0;i<n;i++){
		scanf("%d%d%d", &temp.address, &temp.data, &temp.next);
		Node1[temp.address] = temp;
	} 
	
	//From the first node, find all the nodes in the list based on the next address 
	int next = firstAdd;
	bool go_on = true;    //Does Recording Continue 
	for(int i=0; go_on ;i++) {
		Node2.push_back(Node1[next]);    //Store successor nodes in Node2 vector starting from the first node 
		next = Node1[next].next;
		if(next == -1) go_on = false;
	}
	
	
	int  n1= Node2.size();  //Number of nodes that can form a complete list of chains 
	
	int num = (n1 / k);     //Number of node groups requiring inversion
	vector<Node>::iterator it;  //Traversal Pointer
	for(int i=0;i<num;i++){    
		int lo=i*k, hi=(i+1)*k;    //Each inversion of a node within the [lo, hi] interval does not take into account the change in the node's next address 
		while(lo<hi){
			swap(Node2[lo++],Node2[--hi]);
		}
		
	} 
	
	//Output results, with "-1" output directly at the end of the last node and other nodes output in the format (this node's first address, this node's data, next node's first address) 
	for(it = Node2.begin(); it!=Node2.end(); it++){
		if(it!= Node2.end() -1 ){
		vector<Node>::iterator temp=it;
		printf("%05d %d %05d\n", (*it).address,(*it).data, (*(++temp)).address);
		}
		else
		{
		
		printf("%05d %d %d\n", (*it).address,(*it).data, -1);
	
	}
	} 
	return 0;
}

Posted by jamess on Sun, 30 Jun 2019 09:26:13 -0700