1074 Reversing Linked List (25)(25 分)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 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 N (<= 10^5^) which is the total number of nodes, and a positive K (<=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 N 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

题目大意:给出一个链表,按照数k对其进行反转,并输出反转后的链表。

代码来自:https://www.liuchuo.net/archives/1910

#include <iostream>
#include<stdio.h>
using namespace std;
int main() {
int first, k, n, sum = ;
cin >> first >> n >> k;
int temp, data[], next[], list[], result[];
for (int i = ; i < n; i++) {
cin >> temp;
cin >> data[temp] >> next[temp];
}
while (first != -) {//还需要再次统计,并不是所有的节点都是有效的。
list[sum++] = first;
first = next[first];
}
for (int i = ; i < sum; i++) result[i] = list[i];//现在result里都已经是顺序排列了
for (int i = ; i < (sum - sum % k); i++){
result[i] = list[i / k * k + k - - i % k];
//printf("%d %d\n",i,i / k * k + k - 1 - i % k);
}
for (int i = ; i < sum - ; i++)
printf("%05d %d %05d\n", result[i], data[result[i]], result[i + ]);
printf("%05d %d -1", result[sum - ], data[result[sum - ]]);
return ;
}

//并没有真正的使用结构体来构造链表。data也是使用地址下标来存储,十分简洁。

1.由于输入的节点可能存在异常的,所以又重新统计了一下。

2.但是那个result被反转赋值为list的下标公式可真难。

这个代码来自:

#include<iostream>
#include<algorithm>
using namespace std;
int list[];
int node[][];
int main()
{
int st,num,r;
cin>>st>>num>>r;
int address,data,next,i;
for(i=;i<num;i++)
{
cin>>address>>data>>next;
node[address][]=data;
node[address][]=next;
}
int m=,n=st;
while(n!=-)
{
list[m++]=n;
n=node[n][];
}
i=;
while(i+r<=m)
{
reverse(list+i,list+i+r);
i=i+r;
}
for (i = ; i < m-; i++)
{
printf("%05d %d %05d\n", list[i], node[list[i]][], list[i+]);
}
printf("%05d %d -1\n", list[i], node[list[i]][]);
}

//这个和上边的思想类似,都是使用数组来存储。

1.data是通过二维数组中的第一维来表示,(其实都一样),均是通过地址下标找到的。

2.使用了reverse函数,没想到可以用reverse函数,队医一维数组来说。

while(i+r<=m)
{
reverse(list+i,list+i+r);
i=i+r;
}

//厉害。

PAT 1074 Reversing Linked List[链表][一般]的更多相关文章

  1. PAT 1074. 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 ...

  2. PAT 1074. Reversing Linked List

    #include <cstdio> #include <cstdlib> #include <iostream> #include <unordered_ma ...

  3. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  4. PAT Advanced 1074 Reversing Linked List (25) [链表]

    题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...

  5. PAT甲题题解-1074. Reversing Linked List (25)-求反向链表

    题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...

  6. PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  8. PAT (Advanced Level) 1074. Reversing Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

随机推荐

  1. 【转载】如何从win8/8.1中文版(核心版)升级到win8/8.1专业版

    最近帮助很多同学从win8/8.1的基础版本 - 中文版(核心版)升级到了专业版,经过咨询,升级系统的最主要原因是中文版(核心版)的功能限制,因为基础版本阉割掉了很多常用的功能,比如组策略,计算机管理 ...

  2. 【cs229-Lecture3】Logistic回归

    参考: http://www.itongji.cn/article/12112cH013.html http://blog.csdn.net/zouxy09/article/details/20319 ...

  3. 【cs229-Lecture2】Linear Regression with One Variable (Week 1)(含测试数据和源码)

    从Ⅱ到Ⅳ都在讲的是线性回归,其中第Ⅱ章讲得是简单线性回归(simple linear regression, SLR)(单变量),第Ⅲ章讲的是线代基础,第Ⅳ章讲的是多元回归(大于一个自变量). 本文的 ...

  4. 【ORACLE 】 ORA-00031 标记要删去的会话(解决)

    在使用Oracle的过程中,会有使用了锁(for update)但又忘记释放锁的情况.这是就需要用到KILL语句了.(如果不知道KILL语句怎么用,可参考: http://www.cnblogs.co ...

  5. JSON类库 Flexjson学习

    官方地址(需FQ):http://flexjson.sourceforge.net/ Flexjson 是一个将 Java 对象转成 JSON 的 类库,是一个深度转换的过程. 下面是我写的一个例子: ...

  6. Linux 帐户管理

    一 用户相关操作 1. 添加帐户 useradd 选项 用户名 -c comment 指定一段注释性描述. -d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主目录. -g 用 ...

  7. VC++ 多线程编程,win32,MFC 例子(转)

    一.问题的提出 编写一个耗时的单线程程序: 新建一个基于对话框的应用程序SingleThread,在主对话框IDD_SINGLETHREAD_DIALOG添加一个按钮,ID为IDC_SLEEP_SIX ...

  8. 【CF819C】Mister B and Beacons on Field 数学

    [CF819C]Mister B and Beacons on Field 题意:外星人盯上了Farmer Jack的农场!我们假设FJ的农场是一个二维直角坐标系,FJ的家在原点.外星人向FJ的农场上 ...

  9. 美团店铺评价语言处理以及文本分类(logistic regression)

    美团店铺评价语言处理以及分类(LogisticRegression) 第一篇 数据清洗与分析部分 第二篇 可视化部分, 第三篇 朴素贝叶斯文本分类 本文是该系列的第四篇 主要讨论逻辑回归分类算法的参数 ...

  10. 超链接 a的小手

    cursor:hand   仅仅ie only,FIREFOX底下就不可以正常渲染 cursor:pointer; <span style="cursor:pointer;" ...