1074. Reversing Linked List (25)

简介: #include #include #include using namespace std;struct node { int address, data, next;};const int NUM...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct node {
    int address, data, next;
};

const int NUM = 100001;
node nodes[NUM];
vector<node> list;

int main(){
    int fnAdress, n, k;
    cin >> fnAdress >> n >> k;

    for (int i = 0; i < n; i++) {
        node nodet;
        cin >> nodet.address >> nodet.data >> nodet.next;
        nodes[nodet.address] = nodet;//将每一个node放入正确的位置
    }

    int address = fnAdress;
    while (address != -1) {//去噪
        list.push_back(nodes[address]);
        address = nodes[address].next;
    }

    int size = (int)list.size();
    int round = size / k;
    for (int i = 0; i < round; i++) {
        int start = i * k;
        int end = (i + 1) * k;
        reverse(list.begin() + start, list.begin() + end);
    }
    for(int i = 0; i < size - 1; i++){
        printf("%05d %d %05d\n", list[i].address, list[i].data, list[i+1].address);
    }

    printf("%05d %d %d\n", list[size - 1].address, list[size - 1].data, -1);

    return 0;
}
目录
相关文章
|
6月前
|
存储 Python
链表(Linked List)详解
链表(Linked List)详解
50 0
|
C++
【PAT甲级 - C++题解】1133 Splitting A Linked List
【PAT甲级 - C++题解】1133 Splitting A Linked List
79 0
|
C++
【PAT甲级 - C++题解】1074 Reversing Linked List
【PAT甲级 - C++题解】1074 Reversing Linked List
72 0
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1052 Linked List Sorting
【PAT甲级 - C++题解】1052 Linked List Sorting
83 0
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
|
存储 C++
线性表的链式存储结构 单链表(Singly Linked List) C++
线性表的链式存储结构 单链表(Singly Linked List) C++
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List

热门文章

最新文章