PAT甲级1074 Reversing Linked List (25分)

【程序思路】
先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点
注意:输入的节点中有可能存在无用节点需要过滤,这里我使用map映射来实现过滤
【代码实现】
#include<bits/stdc++.h>
using namespace std;
struct node{
int addrest,data,next;
}t;
int stk[100005];
int main(){
map<int,node> a;
vector<int> c;
int head,n,i,k,top = -1;
cin>>head>>n>>k;
for(i=0;i<n;i++) {
cin>>t.addrest>>t.data>>t.next;
a[t.addrest] = t;
}
while(head!=-1) {
stk[++top] = head;
if(top + 1 == k)
while(top != -1)
c.push_back(stk[top--]);
head = a[head].next;
}
for(i = 0; i <= top; i++)
c.push_back(stk[i]);
for (i = 0; i < c.size() - 1; i++)
printf("%05d %d %05d\n",c[i],a[c[i]].data,c[i+1]);
printf("%05d %d -1\n",c[i],a[c[i]].data);
return 0;
}
PAT甲级1074 Reversing Linked List (25分)的更多相关文章
- 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 ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- 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 ...
- 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 ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1062 Talent and Virtue (25 分)(简单,结构体排序)
1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a histor ...
随机推荐
- selenium下拉选择框处理
HTML: (一)通过xpath层级标签定位 driver.find_element_by_xpath(".//*[@id='Resolution']/option[2]").cl ...
- 鸿蒙内核源码分析(索引节点篇) | 谁是文件系统最重要的概念 | 百篇博客分析OpenHarmony源码 | v64.01
百篇博客系列篇.本篇为: v64.xx 鸿蒙内核源码分析(索引节点篇) | 谁是文件系统最重要的概念 | 51.c.h.o 文件系统相关篇为: v62.xx 鸿蒙内核源码分析(文件概念篇) | 为什么 ...
- 牛客练习赛89E-牛牛小数点【数论】
正题 题目链接:https://ac.nowcoder.com/acm/contest/11179/E 题目大意 定义\(f(x)\)表示\(\frac{1}{x}\)的混循环节长度(如果没有循环节就 ...
- CF622F-The Sum of the k-th Powers【拉格朗日插值】
正题 题目链接:https://www.luogu.com.cn/problem/CF622F 题目大意 给出\(n,k\),求 \[\sum_{i=1}^ni^k \] 解题思路 很经典的拉格朗日差 ...
- 深入浅出WPF-02.WPF系列目录
WPF系列目录 2. XAML认识 3. XAML语法 4. x名称空间详解 5. 控件与布局 6. 绑定Binding-01 6. 绑定Binding-02 6. 绑定Binding-03 7. 属 ...
- Kettle学习笔记(二)— 基本操作
目录 Kettle学习笔记(一)- 环境部署及运行 Kettle学习笔记(二)- 基本操作 kettle学习笔记(三)- 定时任务的脚本执行 Kettle学习笔记(四)- 总结 打开Kettle 打开 ...
- linux中文件查找、whereis、which、输出命令
1.文件查找(find):find是最常⻅和最强⼤的查找命令 格式:find / -name 文件名,比如:find / -name mysql. (1).模糊查找:*是代表所有的,?是代表⼀个字 ...
- 第一次Alpha Scrum Meeting
本次会议为Alpha阶段第一次Scrum Meeting会议 会议概要 会议时间:2021年4月22日 会议地点:北航Inspiration Space咖啡厅 会议时长:1小时 会议内容简介:本次会议 ...
- BUAA软件工程个人作业-软件案例分析
BUAA个人博客作业-软件案例分析 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 软件案例分析 我在这个课程的目标是 学习开发软件的能力 这个作 ...
- BUAA_2020_软件工程_个人项目作业
作业抬头(1') 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 个人项目作业 我在这个课程的目标是 了解软件工程的技术,掌握工程化开发的能力 这 ...