定义静态链表
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 (<105
) 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 [−105 ,105 ], 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);
}
}
}