静态链表通用解题步骤

本文讲解了如何定义和操作静态链表,包括结构体的定义、初始化、遍历,以及如何通过特定属性区分有效结点并进行有效结点的左聚集排序。最后给出了一个具体的例题1052 LinkedList Sorting的解答,展示了如何按照节点的键值对链表进行升序排序并输出。
摘要由CSDN通过智能技术生成

定义静态链表

struct Node {
	int address;//节点地址 
	typedata data;//结点数据 
	int next;//下一个结点 
	XXX  //结点的某一个性质 
} nodes[maxn];

XXX可以根据题目的要求来使用,比如定义该结点是否加入了链表。

链表初始化

	for(int i = 0; i<maxn; i++) {
		nodes[i].XXX 
	}

将其定义为一般情况下达不到的数字。防止影响以后的使用

静态链表的遍历

	int p ;
	for(p = startaddress; p!=-1;) {
		nodes[p].XXX;
		p = nodes[p].next;
	}

在遍历的同时,设置修改XXX的属性值

有效结点的左聚集

题目中给出一系列的点,这些点并非都属于静态链表,那就需要排序,将属于静态链表的结点放在左边。
我们可以使用上面的XXX属性。
比如,我们默认XXX属性为负值,当我们在第三步遍历的时候就将属性值设置为正值,这样出现在链表的结点肯定会比没出现在链表的结点的XXX属性值大,现在重写cmp函数即可。

bool cmp(Node a,Node b){
	if(a.XXX == -1||b.XXX == -1){
		return a.XXX > b.XXX
	}
} 

一个例题

1052 Linked List Sorting (25 分)
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (<10​5
​​ ) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next
where Address is the address of the node in memory, Key is an integer in [−10​5​​ ,10​5 ], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
作者
CHEN, Yue
单位
浙江大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
C++ (g++)

题目意思

给出一个结点的首地址,给出节点数,
接着给出每个结点的地址,值,还有下一个结点的地址
利用上面的模板,创建结点结构体,初始化,遍历,排序,输出

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<stack>
#include<string.h>
#include<queue>
#include<map>
#include<ctype.h>
#include<algorithm>
//静态链表答题
using namespace std;
const int maxn = 100010;
struct Node {
	int address;//节点地址
	int data;
	int next;//下一个结点 
	bool flag;
} nodes[maxn];
bool cmp(Node a,Node b){
	if(a.flag == false||b.flag == false){
		return a.flag > b.flag;
	}else
		return a.data < b.data;
}
int main() {
	for(int i = 0; i<maxn; i++) {
		nodes[i].flag = false;
	}
	int add1,N;
	scanf("%d%d",&N,&add1);
	int sadd,nadd;
	int da;
	for(int i =0; i<N; i++) {
		scanf("%d %d %d",&sadd,&da,&nadd);
		nodes[sadd].address = sadd;
		nodes[sadd].data = da;
		nodes[sadd].next = nadd;
	}
	int p ;
	int count =0;
	for(p = add1; p!=-1;) {
		nodes[p].flag =  true;
		p = nodes[p].next;
		count ++;
	}
	if(count == 0){
		printf("0 -1");
	}else{
		sort(nodes,nodes+ maxn,cmp);
		printf("%d %05d\n",count,nodes[0].address);
		for(int i = 0;i<count;i++){
			if(i != count - 1)
				printf("%05d %d %05d\n",nodes[i].address,nodes[i].data,nodes[i + 1].address);
			else
				printf("%05d %d -1\n",nodes[i].address,nodes[i].data);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值