重建链表

来源:互联网 发布:上饶师范学院网络教务 编辑:程序博客网 时间:2024/06/01 07:51

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

给定n个节点,每个节点的包含信息(address,data,next),其中address代表这个节点的地址,data代表这个节点的值,next代表这个节点的下一个节点地址,请根据这些信息重建链表。
注意:-1代表NULL,每个节点的地址都是5位非零整数。

Input

第一行包含两个整数a和n,其中a表示链表的首地址,n表示链表中节点的个数。接下来n行包含每个节点的信息。其中1≤n≤10^5。

Output

将链表按照顺序输出,每行一个节点的信息,信息包括这个的地址、数据以及下一个节点的地址。

Sample Input

00100 600000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218

Sample Output

00100 1 1230912309 2 3321833218 3 0000000000 4 9999999999 5 6823768237 6 -1

解题说明:数据结构题目,需要用到struct

#include <stdio.h>#include <stdlib.h>#include <algorithm>using namespace std;const int maxn = 100000+5;struct Node {    int address;    int Data;    int next;}a[maxn];int pos[maxn];int main() {    int addr, n;    while(scanf("%d%d", &addr, &n) == 2) {        for(int i = 0; i < n; i++) {            scanf("%d%d%d", &a[i].address, &a[i].Data, &a[i].next);            pos[a[i].address] = i;        }        for(int i = 0; i < n; i++) {            int p = pos[addr];            if(i == n-1) {                printf("%05d %d -1\n", a[p].address, a[p].Data);            } else {                printf("%05d %d %05d\n", a[p].address, a[p].Data, a[p].next);            }            addr = a[p].next;        }    }    return 0;}


原创粉丝点击