PAT B1075 Linked List Element Classification (25 points)

Insert picture description here
For this problem, you can open an array to save the address, and then traverse the original linked list three times, the first time to find the addresses of all negative nodes, the second time to find all the nodes with the interval value of [0,k], and the third time to find All nodes greater than k can also ensure that all saved addresses are also arranged in the original internal order.

#include <cstdio>

struct Node{
    
    
	int ad;
	int data;
	int next;
}node[100010];

int main(){
    
    
	int head, n, k;
	scanf("%d %d %d", &head, &n, &k);
	
	for(int i=0; i<n; i++){
    
    
		int ad;
		scanf("%d", &ad);
		node[ad].ad = ad;
		scanf("%d %d", &node[ad].data, &node[ad].next);
	}
	
	int arr[n];
	int num = 0;
	int h = head;
	while(h != -1){
    
    
		if(node[h].data < 0){
    
    
			arr[num++] = h;
		}
		h = node[h].next;
	}
	h = head;
	while(h != -1){
    
    
		if(node[h].data>=0 && node[h].data<=k){
    
    
			arr[num++] = h;
		}
		h = node[h].next;
	}
	h = head;
	while(h != -1){
    
    
		if(node[h].data > k){
    
    
			arr[num++] = h;
		}
		h = node[h].next;
	}
	for(int i=0; i<num-1; i++){
    
    
		printf("%05d %d %05d\n", arr[i], node[arr[i]].data, arr[i+1]);
	}
	printf("%05d %d -1", arr[num-1], node[arr[num-1]].data);
	
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45964844/article/details/113894610