【程序思路】
先根据地址按顺序读入节点,入栈,当栈里的元素个数等于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分)的更多相关文章

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

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

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

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

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

  5. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  6. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  7. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  8. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  9. PAT 甲级 1062 Talent and Virtue (25 分)(简单,结构体排序)

    1062 Talent and Virtue (25 分)   About 900 years ago, a Chinese philosopher Sima Guang wrote a histor ...

随机推荐

  1. 封装excel导出方法

    封装读取excel内容方法 /** * 获取Excel内容 * @param type $filename * @return type */ public function getExcelCont ...

  2. HTML 网页开发、CSS 基础语法——六. HTML基本结构

    1.基本骨架 HTML文件最基本的四个标签,组成了网页的基本骨架,包括:<html>. <head>.<title>.<body>四组标签. ① < ...

  3. 如何把springboot项目部署到tomcat上

    前言: 开始以为打包springboot项目为war包丢到tomcat上的webapps下面就可以访问controller层的路径了,可是调用接口却报404的错误,而打开8080的主页,不加路径却可以 ...

  4. 华为云计算IE面试笔记-其它知识点

    * 虚拟化与云计算的区别?云计算和虚拟化关系?华为云计算怎么做的? 虚拟化是一种具体的技术,实现的是对硬件资源的虚拟化,提升资源利用率,降低能耗,云计算是一种概念各种技术组件的集合,针对的是对各种资源 ...

  5. 【C++ Primer Plus】编程练习答案——第8章

    1 void ch8_1_print(const std::string & str, int n = 0 ) { 2 using namespace std; 3 static int fl ...

  6. NOIP 模拟 十一

    T1 math 分析性质,对于 $$ ax+by=c$$ 有 $$ gcd(x,y)|c$$ 所以 $$ gcd(a_1,a_2 .....,a_n)|num$$ 换句话说就是最后得到的数一定是 GC ...

  7. Http Only Cookie保护AccessToken

    前言 JWT认证方式目前已被广泛使用,一直以来我们将token放在请求头中的Authorization中,若通过此种方式,一旦token被恶意窃取,攻击者可肆意对用户可访问资源进行任意索取,我们大多都 ...

  8. 前端必会的Javascript经典面试题

    这是前端最基础的问题,也是在面试中难倒无数同学的经典问题 01. Javascript 数据类型 Javascript 数据类型 = 基本类型 + 引用类型 ES6 之前 5 + 1 = 6 种 ES ...

  9. Python爬取 | 唯美女生图片

    这里只是代码展示,且复制后不能直接运行,需要配置一些设置才行,具体请查看下方链接介绍: Python爬取 | 唯美女生图片 from selenium import webdriver from fa ...

  10. 关于django配置好静态文件后打开相关图片页显示404的解决方法

    在url里设置以上代码即可,即可解决图片显示异常(出现此问题的根本原因是django版本)django3后需要加以上代码)