PAT链表专题

关于PAT甲级的链表问题,主要内容 就是”建立链表“

所以第一步学会模拟链表,pat又不卡时间,这里用vector + 结构体,更简洁

模拟链表的普遍代码

const int maxn = 1e6+10;

struct node{
int address;
int next;
char key;
}nod[maxn]; int head1,n;
vector<node> list1; cin>>head1>>n;
for(int i=1;i<=n;i++) {
int address,next;
char key;
cin>>address>>key>>next;
nod[address].address = address;//重点1
nod[address].key = key;
nod[address].next = next;
}
for(head1; head1 != -1; head1 = nod[head1].next){ //重点2
list1.push_back(nod[head1]);
}

学会模拟链表之后,PAT甲级的链表题就都能做了,万变不离其宗,

基本就是,建立链表、按照题意操作链表(链表合并、去重、反转...)、注意判断链表边界(链表有效长度)、输入输出( %05d 来输出地址)

2019秋季PAT 7-2

#include<bits/stdc++.h>
using namespace std; const int maxn = 100001;
int ad1,ad2,n;
int head1,head2 = 0;
int len1 = 0,len2 = 0; struct node{
int add,next,key;
}nod[maxn]; vector<node> list1;
vector<node> list2;
vector<node> res; int main(){
cin>>head1>>head2>>n;
for(int i=1;i<=n;i++){
int add,next,key;
cin>>add>>key>>next;
nod[add].add = add;
nod[add].next = next;
nod[add].key = key;
}
for (int p = head1; p != -1; p = nod[p].next)
list1.push_back(nod[p]);
for(int p = head2;p!=-1;p = nod[p].next)
list2.push_back(nod[p]); len1 = list1.size()-1;
len2 = list2.size()-1;
if(len1 >= 2*len2){
int p = 0,q = len2;
while(q >= 0){
res.push_back(list1[p++]);
res.push_back(list1[p++]);
res.push_back(list2[len2--]);
}
while(p <= len1){
res.push_back(list1[p++]);
}
for(int i=0;i<res.size();i++){
if(i != res.size() - 1){
res[i].next = res[i+1].add;
}
}
}else{
int p = len1,q = 0;
while(p >= 0){
res.push_back(list2[q++]);
res.push_back(list2[q++]);
res.push_back(list1[p--]);
}
while(q<=len2){
res.push_back(list2[q++]);
}
for(int i=0;i<res.size();i++){
if(i != res.size() - 1){
res[i].next = res[i+1].add;
}
}
}
for(int i=0;i<res.size();i++){
if(i == res.size()-1) printf("%05d %d -1\n",res[i].add,res[i].key);
else printf("%05d %d %05d\n",res[i].add,res[i].key,res[i].next);
} return 0;
}

PTA1032

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e6+10; struct node{
int address;
int next;
char key;
}nod[maxn]; int head1,head2,n;
bool use[maxn];
vector<node> list1,list2; int main(){
cin>>head1>>head2>>n;
for(int i=1;i<=n;i++) {
int address,next;
char key;
cin>>address>>key>>next;
nod[address].address = address;
nod[address].key = key;
nod[address].next = next;
}
for(head1; head1 != -1; head1 = nod[head1].next){
list1.push_back(nod[head1]);
use[head1] = true;
}
for(head2; head2 != -1; head2 = nod[head2].next){
if(use[head2]){
printf("%05d\n",head2);
return 0;
}
}
cout<<"-1"<<endl;
return 0;
}

PTA1052

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e6+100; struct node{
int address;
int key;
int next;
bool flag;
}nod[100010]; vector<node> list1; int n;
int head; bool cmp(node a,node b){
return a.key < b.key;
} int main(){
cin>>n>>head;
for(int i=1;i<=n;i++){
int address,key,next;
cin>>address>>key>>next;
nod[address].address = address;
nod[address].key = key;
nod[address].next = next;
nod[address].flag = true;
}
int m = 0;
for(;head!=-1;head = nod[head].next){
nod[head].flag = true;
list1.push_back(nod[head]);
m++;
}
if(m == 0){ //判边界 否则段错误
printf("0 -1\n");
return 0;
}
sort(list1.begin(),list1.end(),cmp);
for(int i=0;i<m-1;i++){
list1[i].next = list1[i+1].address;
}
cout<<m<<" ";
printf("%05d\n",list1[0].address);
for(int i=0;i<m-1;i++){
printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i].next);
}
printf("%05d %d -1\n",list1[m-1].address,list1[m-1].key);
return 0;
}

PTA1074

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+10; /*
题目意思是 k个元素间换一下
*/ struct node{
int address;
int key;
int next;
}nod[maxn]; vector<node>list1,list2;
int head,n,k; void print(){
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",list2[i].address,list2[i].key,list2[i].next);
}
printf("%05d %d -1\n",list2[n-1].address,list2[n-1].key);
} int main(){
cin>>head>>n>>k;
for(int i=0;i<n;i++){
int add,key,next;
cin>>add>>key>>next;
nod[add].address = add;
nod[add].key = key;
nod[add].next = next;
} for(;head!=-1;head=nod[head].next)
list1.push_back(nod[head]);
n = list1.size(); //原来的n不一定是新链表的长度 可能输入数据有无效结点 int pos = 0;
while(pos + k - 1 < n){ //这里注意 下标 每pos~pos+k-1为一组
for(int i = pos+k-1;i>=pos;i--){ //倒序存进list2
list2.push_back(list1[i]);
}
pos+=k;
}
if(pos < n){ //剩下的没有凑够k个就 直接存入链表list2
for(int i=pos;i<n;i++){//正序存进list2
list2.push_back(list1[i]);
}
}
for(int i=0;i<n-1;i++){ //更新next地址
list2[i].next = list2[i+1].address;
}
list2[n-1].next = -1;
print();
return 0;
} /*
00100 4 2
00000 4 -1
00100 1 12309
33218 3 00000
12309 2 33218
*/

PTA1097

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+10; int head,n;
struct node{
int address;
int key;
int next;
}nod[maxn]; bool vis[maxn];
vector<node> list1,list2; int main(){
cin>>head>>n;
for(int i = 0;i < n;i++){
int add,key,next;
cin>>add>>key>>next;
nod[add].address = add;
nod[add].key = key;
nod[add].next = next;
}
for(;head!=-1;head = nod[head].next){
if(!vis[abs(nod[head].key)]){
vis[abs(nod[head].key)] = true;
list1.push_back(nod[head]);
}else{
list2.push_back(nod[head]);
}
}
n = list1.size();
if(n != 0){
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i+1].address);
}
printf("%05d %d -1\n",list1[n-1].address,list1[n-1].key);
}
int m = list2.size();
if(m != 0){
for(int i=0;i<m-1;i++){
printf("%05d %d %05d\n",list2[i].address,list2[i].key,list2[i+1].address);
}
printf("%05d %d -1\n",list2[m-1].address,list2[m-1].key);
}
return 0;
}

PAT甲级专题|链表的更多相关文章

  1. PAT甲级专题|树的遍历

    PAT甲级专题-树的遍历 涉及知识点:树.建树.深度优先搜索.广度优先搜索.递归 甲级PTA 1004 输出每一层的结点,邻接表vector建树后.用dfs.bfs都可以边搜边存当前层的数据, #in ...

  2. PAT甲级专题|最短路

    PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...

  3. PAT甲级满分攻略|记一次考试经历

    一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...

  4. 2019秋季PAT甲级_备考总结

    2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...

  5. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  6. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  8. PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca

    给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...

  9. PAT甲级题分类汇编——理论

    本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...

随机推荐

  1. Azure上MySQL的离线备份:将备份拷贝到Azure Blob上

    公司在Azure的Iaas虚拟机上部署有好几台MySQL数据库,至于没有选择Azure Database for MySQL,是因为预算有限(钱不够啊!说多了也是泪,坑的还是DBA自己).选择了Iaa ...

  2. mysql设计规范一

    原文地址:http://www.jianshu.com/p/33b7b6e0a396 主键 表中每一行都应该有可以唯一标识自己的一列(或一组列). 一个顾客可以使用顾客编号列,而订单可以使用订单ID, ...

  3. 在线热备份数据库之innobackupex 完整备份InnoDB

    在线热备份数据库innobackupex 完整备份InnoDB XtraBackup xtrabackup C程序,支持InnoDB/XtraDB innobackupex : 以Perl脚本封装xt ...

  4. [考试反思]1015csp-s模拟测试75:混乱

    赶上一套极其傻逼的题(是傻逼,不是简单) T1超级卡精 T2模拟(输出卡"0.0"与"-0.0"不开spj),而且数据诡异乱打就能A(貌似给这道题的时间越长分越 ...

  5. 「考试」$5T$

    啊因为最近题实在是好啊,只能四五篇四五篇写了. T1. 括号序列的确简单. 当我们维护左右$cnt$后. 到一个左括号的地方的话. 答案就是:$$\sum\limits_{i=1}^{min(lc,r ...

  6. ubuntu 12 install redis

    ubuntu 12 install redis 今天开始写数据server部分,大家初步的方案是用redis+mysql 所以要安装,下面记录安装的基本过程,留做后续参考 unbuntu 12 已经支 ...

  7. centos7上以RPM方式安装MySQL5.6

    1. 下载MySQL http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/ MySQL-5.6.36-1.el7.src.rpm MySQL-5.6.36- ...

  8. 最新JetBrains PyCharm 使用教程--安装教程(一)

    安装过程 ​ ​ ​ ​ ​ ​ ​ Pycharm  激活码 license server选项里边输入:http://intellij.mandroid.cn/ ​

  9. TCP--文件上传

    客户端 public class Test2_UpdateClient { public static void main(String[] args) throws UnknownHostExcep ...

  10. 深入理解计算机系统 第二章 信息的表示和处理 part2

      上一周遗留问题的解决 问题:原码.反码.补码是只针对有符号数吗?无符号数有没有这三种编码方式? 得到的答案:对于无符号数,原码.反码和补码是一致的 进一步,由于有符号数是以补码的形式存储在计算机中 ...