关闭

02-线性结构2 Reversing Linked List

标签: 数据结构-PAT
538人阅读 评论(0) 收藏 举报
分类:

Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL being 1→2→3→4→5→6, if K = 3K=3, then you must output 3→2→1→6→5→4; if K = 4K=4, you must output 4→3→2→1→5→6.
Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NN (\le 10^5≤10
​5
​​ ) which is the total number of nodes, and a positive KK (\le N≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then NN lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

思路:建立双向链表,依次访问直到数目为倒序的次数,若能访问,则用双向链表倒序输出,否则就正序输出。

#include <iostream>
#include <vector>
using namespace std;

typedef struct link
{
    int rank;
    int base_add;
    int next_add;
    link* parent;
    link* child;
}link;

int main()
{
    int begin_add, N, listnum;

    cin >> begin_add;
    cin >> N;
    cin >> listnum;

    vector<pair<int, int>> address(N);//first is base address,second is next address
    int base_add, next_add, rank;
    for (int i = 0; i < N; i++)
    {
        cin >> base_add;
        cin >> rank;
        cin >> next_add;


        address[rank - 1].first = base_add;
        address[rank - 1].second = next_add;
    }

    link* top;
    link* current;
    link* child;
    top = (link*)malloc(sizeof(link));
    current = top;
    top->parent = NULL;
    for (int i = 0; i < N; i++)
    {
        current->base_add = address[i].first;
        current->next_add = address[i].second;
        current->rank = i+1;
        if (i != N - 1)//not the last node
        {
            child = (link*)malloc(sizeof(link));
            current->child = child;
            child->parent = current;
            current = child;
        }
        else
        {
            current->child = NULL;
        }
    }
    int count;
    current = top;
    bool flag = false;
    while (current != NULL)
    {
        count = 1;
        child = current;
        if (flag)
            printf("%05d\n", child->base_add);
        while (current != NULL&&count!=listnum)
        {
            current = current->child;
            count++;
        }
        if (count == listnum)
        {
            child = current;

            for (int j = 0; j < listnum-1; j++)
            {
                //if (child->parent != NULL)
                printf("%05d %d %05d\n", child->base_add, child->rank, child->parent->base_add);
                child = child->parent;
            }
            printf("%05d %d ", child->base_add, child->rank);
            current = current->child;
            flag = true;
        }
        else
        {
            for (int j = 0; j < count-2; j++)
            {
                printf("%05d %d %05d\n", child->base_add, child->rank, child->child->base_add);
                child = child->child;
            }
            printf("%05d %d %d\n", child->base_add, child->rank, -1);
            break;
        }

    }

    return 0;
}
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

数据结构:链表(linked-list)

链表分为单向链表(Singly linked lis)、双向链表(Doubly linked list)、循环链表(Circular Linked list)。
  • juanqinyang
  • juanqinyang
  • 2016-05-09 11:09
  • 2863

[笔记]python数据结构之线性表:linkedlist链表,stack栈,queue队列

python数据结构之线性表 python内置了很多高级数据结构,list,dict,tuple,string,set等,在使用的时候十分舒心。但是,如果从一个初学者的角度利用python学习数据结...
  • u013805817
  • u013805817
  • 2015-06-06 17:47
  • 7480

C++数据结构 之 链表_Linked List

C++数据结构 之 链表_Linked List源代码只包含头文件注:需要C++11的支持.
  • liu798675179
  • liu798675179
  • 2016-08-10 11:57
  • 2746

02-线性结构2 Reversing Linked List

#include #define MAX_SIZE 100001 typedef struct node{ int address; int data; int next_addres...
  • asunamashiro
  • asunamashiro
  • 2015-09-22 17:13
  • 760

02-线性结构2. Reversing Linked List (25)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem...
  • qq_19672579
  • qq_19672579
  • 2015-04-07 23:25
  • 499

PAT 02-线性结构3 Reversing Linked List 【JAVA实现】

单向链表的逆转JAVA实现,附陈越老师的讲解。
  • u014800380
  • u014800380
  • 2016-10-25 12:03
  • 335

02-线性结构1. Reversing Linked List (25)

原题链接:http://www.patest.cn/contests/mooc-ds/02-%E7%BA%BF%E6%80%A7%E7%BB%93%E6%9E%841 02-线性结构...
  • ice_camel
  • ice_camel
  • 2015-04-20 21:50
  • 753

逆转链表-pta02-线性结构3 Reversing Linked List

题目链接 题目 难点分析 1.链表结点如何逆转 用三个指针new_head、old_head,tmp。之所以要用三个,是因为除了逆转的两个结点外,还需要保存下一个待逆转的结点,不保存的话逆转之后...
  • bobo1356
  • bobo1356
  • 2017-05-05 18:53
  • 201

[数据结构]02-线性结构3 Reversing Linked List

Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every ...
  • sinat_37273780
  • sinat_37273780
  • 2017-03-22 21:03
  • 99

[MOOC-陈越何钦铭数据结构]02-1. Reversing Linked List (25)

02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...
  • aldslvda
  • aldslvda
  • 2015-02-14 16:32
  • 391
    个人资料
    • 访问:14725次
    • 积分:411
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论