Source:

PAT A1074 Reversing Linked List (25 分)

Description:

Given a constant K and a singly linked list L, you are supposed to reverse the links of every Kelements 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 (≤) which is the total number of nodes, and a positive K (≤) 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

Keys:

  • 栈和队列

Attention:

  • vector<int> ans.insert(ans.begin()+pos,x);

Code:

 #include<cstdio>
#include<stack>
#include<vector>
using namespace std;
const int M=1e5+;
struct node
{
int data;
int addr,next;
}link[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int first,n,k,address;
scanf("%d%d%d", &first,&n,&k);
for(int i=; i<n; i++)
{
scanf("%d", &address);
link[address].addr=address;
scanf("%d%d", &link[address].data,&link[address].next);
}
int pos=;
stack<int> re;
vector<int> ans;
while(first != -)
{
re.push(first);
if(++pos == k)
{
pos=;
while(!re.empty())
{
ans.push_back(re.top());
re.pop();
}
}
first = link[first].next;
}
int len=ans.size();
while(!re.empty())
{
ans.insert(ans.begin()+len,re.top());
re.pop();
}
for(int i=; i<ans.size(); i++)
{
if(i!=)
printf("%05d\n", ans[i]);
printf("%05d %d ", ans[i],link[ans[i]].data);
}
printf("-1"); return ;
}

PAT_A1074#Reversing Linked List的更多相关文章

  1. PAT1074 Reversing Linked List (25)详细题解

    02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. PAT 1074 Reversing Linked List[链表][一般]

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

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

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  4. 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...

  5. PTA 02-线性结构3 Reversing Linked List (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List   (25分) Given a ...

  6. 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 ...

  7. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  8. 想了很久,一道Microsoft的笔试题目 —— Reversing Linked List

    Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the ...

  9. 02-线性结构2 Reversing Linked List

    由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...

随机推荐

  1. 2019 Multi-University Training Contest 1 - 1012 - NTT

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=6589 题解连接: https://www.cnblogs.com/xusirui/p/1122945 ...

  2. Codeforces - 1189B - Number Circle - 贪心

    https://codeforc.es/contest/1189/problem/B 优先考虑最大的元素怎么构造.拿两个次大的围着他就很好,但是其他的怎么安排呢?就直接降序排列就可以了. a数组还开错 ...

  3. Activiti6.0 java项目框架 spring5 SSM 工作流引擎 审批流程

    工作流模块----------------------------------------------------------------------------------------------- ...

  4. vue,一路走来(15)--简单投票系统

    今天记录一下简单的投票系统,主要实现选中至少五张作品,并提交投票. 思路:选中作品,将作品id存入到数组里. 取消投票,则从数组中移除该作品id. 如图效果: <li v-for="( ...

  5. vue,一路走来(9)--聊天窗口

    闲暇时间,介绍一下我做一个聊天窗口的心得.如图: 首先要考虑的是得判断出是自己的信息还是对方发来的信息,给出如图的布局,切换不同的类. <li class="clearfix" ...

  6. Sass:RGB颜色函数-Mix()函数

    Mix 函数是将两种颜色根据一定的比例混合在一起,生成另一种颜色.其使用语法如下: mix($color-1,$color-2,$weight); $color-1 和 $color-2 指的是你需要 ...

  7. Sass-字符串

    JavaScript支持css的两种字符串类型: 有引号字符串 (quoted strings),如 "Lucida Grande" .'http://sass-lang.com' ...

  8. axis2获取request方法

    修改axis2的请求url-pattern 找到axis2-kernel jar包中axis2.xml配置文件的servicePath配置项,修改成与url-pattern一样的值,这样就改变了请求的 ...

  9. gps位置坐标转百度坐标

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. html5 带声音的导航

    代码实例: <!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3. ...