A.数据结构练习题――线性表操作

线性表的基本操作

1.在某个位置p插入val,复杂度O(p)

2.在某个位置p删除val,复杂度O(p)

3.查找某个位置p的值,复杂度O(p)

4.清除链表,复杂度O(链表长)

 #include<iostream>
#include<string>
#include<list>
using namespace std;
int main()
{
int n,p,x;
string s;
list<int>li;
list<int>::iterator it;
while(cin>>s)
{
if(s=="exit")break;
else if(s=="clear")li.clear();
else if(s=="insert")
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>p>>x;
it=li.begin();
while(--p)it++;
li.insert(it,x);
}
}
else
{
cin>>p;
it=li.begin();
while(--p)it++;
cout<<*it<<endl;
if(s=="delete")li.erase(it);
}
}
return ;
}

 B.数据结构练习题――合并表

线性表的基本操作

先把第一排每个数放入链表,然后把b中的每个数插入链表,复杂度O(n+m)

 #include<iostream>
#include<list>
using namespace std;
int main()
{
int t,n,m;
list<int>li;
list<int>::iterator it;
cin>>t;
while(t--)
{
li.clear();
cin>>n;
for(int i=,a;i<n;i++)cin>>a,li.push_back(a);
cin>>m;
it=li.begin();
for(int i=,b;i<m;i++)
{
cin>>b;
while(it!=li.end()&&*it<=b)it++;
li.insert(it,b);
}
cout<<n+m;
for(auto X:li)
cout<<" "<<X;
cout<<endl;
}
return ;
}

C.数据结构―线性表插入删

线性表的基本操作

1.在第pos个位置前插入连续m个数,由于是插入在前面,所以找到第pos个位置的地址it,然后不断插入,复杂度O(m)

2.删除第pos个位置开始连续m个数,若到表尾则删除到结尾,这里erase第pos个位置的地址it后,需要讲,复杂度O(m)

 #include<stdio.h>
#include<list>
using namespace std;
int main()
{
int t,n,p,pos,m,x;
char s[];
scanf("%d",&t);
while(t--)
{
list<int>l;
list<int>::iterator it;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&x),l.push_back(x);
scanf("%d",&p);
for(int i=;i<p;i++)
{
scanf("%s",s);
if(s[]=='i')
{
scanf("%d%d",&pos,&m);
it=l.begin();
while(--pos>)it++;//这里>0考虑到pos=0的情况
while(m--)
scanf("%d",&x),l.insert(it,x);
}
if(s[]=='d')
{
scanf("%d%d",&pos,&m);
it=l.begin();
while(--pos)it++;
while(m--&&it!=l.end())l.erase(it++);
}
}
int k=;
for(it=l.begin();it!=l.end();it++)
{
if(k++)printf(" ");
printf("%d",*it);
}
printf("\n");
}
return ;
}

E.数据结构练习题――栈

栈的基本操作

1.把元素val压入栈,复杂度O(1)

2.弹出栈顶元素,若栈为空输出None,否则输出栈顶元素val,复杂度O(1)

3.输出栈顶元素,若栈为空输出None,否则输出栈顶元素val,复杂度O(1)

4.清空栈,复杂度O(栈中元素数)

 #include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
int a;
string s;
stack<int>st;
while(cin>>s)
{
if(s=="exit")break;
if(s=="push")cin>>a,st.push(a);
if(s=="top")
{
if(st.empty())cout<<"None"<<endl;
else cout<<st.top()<<endl;
}
if(s=="pop")
{
if(st.empty())cout<<"None"<<endl;
else cout<<st.top()<<endl,st.pop();
}
if(s=="clear")
while(!st.empty())st.pop();
}
return ;
}

F.数据结构练习题----队列的基本操作

队列基本操作

1.值val入队,复杂度O(1)

2.出队,若队列为空,输出None,否则输出队头元素val,复杂度O(1)

3.输出队列中所有元素,若队列为空,输出None,否则输出所有元素,需要借助一个临时队列p用于存输出前的队列,复杂度O(原队列长)

 #include<iostream>
#include<queue>
#include<string>
using namespace std;
int main()
{
int a;
string s;
queue<int>q,p;
while(cin>>s)
{
if(s=="exit")break;
if(s=="enq")cin>>a,q.push(a);
if(s=="printq")
{
if(q.empty()){cout<<"None"<<endl;continue;}
int k=;
p=q;
while(!q.empty())
{
if(k++)cout<<" ";
cout<<q.front(),q.pop();
}
cout<<endl;
q=p;
}
if(s=="deq")
{
if(q.empty())cout<<"None"<<endl;
else cout<<q.front()<<endl,q.pop();
}
}
return ;
}

 G.简单排序

给你n个数让你排个序,复杂度O(nlogn)

 #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t,n,a[];
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<n;i++)cin>>a[i];
sort(a,a+n);
for(int i=;i<n;i++)cout<<a[i]<<(i==n-?"\n":" ");
}
return ;
}

H.快速排序

给你n个数让你排序,注意用C++提交,复杂度O(nlogn)

 #include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int t,n,a[];
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)scanf("%d",&a[i]);
sort(a,a+n);
for(int i=;i<n;i++)printf("%d%c",a[i],i==n-?'\n':' ');
}
return ;
}

 I.二分查找

二分查找,注意C++提交,复杂度O(logn)

lowber_bound(a,a+n,x);

函数的作用是返回>=x的第一个位置,若没有返回最后一个位置

第一个为二分开始的位置,第二个为二分结束的位置,注意左闭右开,第三个为查找的值val

 #include<stdio.h>
#include<algorithm>
using namespace std;
int a[];
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)scanf("%d",&a[i]);
scanf("%d",&m);
int pos=lower_bound(a,a+n,m)-a;
if(pos==n||a[pos]!=m)printf("None\n");
else printf("%d\n",pos+);
}
return ;
}

J.图的遍历

图的深度优先和图的广度优先,需要有一个基本的认识,复杂度O(2n)

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,vis[],G[][];
void dfs(int u)
{
if(vis[u])return;
printf("%d ",u);
vis[u]=;
for(int i=;i<=n;i++)if(!vis[i]&&G[u][i])dfs(i);
}
void bfs(int u)
{
queue<int>q;
q.push(u);
while(!q.empty())
{
u=q.front();q.pop();
if(vis[u])continue;
printf("%d ",u);
vis[u]=;
for(int i=;i<=n;i++)if(!vis[i]&&G[u][i])q.push(i);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(G,,sizeof(G));
for(int i=,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
G[u][v]=G[v][u]=;
}
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)if(!vis[i])dfs(i);
printf("\n");
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)if(!vis[i])bfs(i);
printf("\n");
}
return ;
}

K.二叉树遍历

这题给你的是层序,而且只有1000个节点,我们知道2^10-1=1024,所以最多10层,我们可以用数组建树

然后你需要知道如何先序中序后序,复杂度O(3*k),k为节点数

 #include<stdio.h>
int a[],k;
void P(int u,int flag)
{
if(u>k||a[u]==)return;
if(flag==)printf(" %d",a[u]);//先序
P(u*,flag);//左儿子
if(flag==)printf(" %d",a[u]);//中序
P(u*+,flag);//右儿子
if(flag==)printf(" %d",a[u]);//后序
}
int main()
{
int t,x;
scanf("%d",&t);
while(t--)
{
k=;
while(scanf("%d",&x),x!=-)a[k++]=x;
for(int i=;i<=;i++)
P(,i),printf("\n");
}
return ;
}

TZOJ 数据结构期末历年题目的更多相关文章

  1. $NOIp$提高组历年题目复习

    写在前面 一个简略的\(NOIp\)题高组历年题目复习记录.大部分都有单独写题解,但懒得放\(link\)了\(QwQ\).对于想的时候兜了圈子的题打上\(*\). \(NOIp2018\ [4/6] ...

  2. PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分)   给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义 ...

  3. PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-42整型关键字的散列映射 (25 分) 7-42 整型关键字的散列映射 (25 分)   给定一系列整型关键字和素数P,用除留余数法定义的散列函数将关键字映射 ...

  4. PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)

    PTA数据结构与算法题目集(中文)  7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分)   计算机程序设计能力考试(Programming Ability Test,简称P ...

  5. PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)

    PTA数据结构与算法题目集(中文)  7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分)   每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...

  6. PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)

    PTA数据结构与算法题目集(中文)  7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分)   在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...

  7. PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)

    PTA数据结构与算法题目集(中文)  7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分)   胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...

  8. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  9. PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分)

    PTA数据结构与算法题目集(中文)  7-36 社交网络图中结点的“重要性”计算 (30 分) 7-36 社交网络图中结点的“重要性”计算 (30 分)   在社交网络中,个人或单位(结点)之间通过某 ...

随机推荐

  1. 我的母校zbvc试做

    一.观察分析页面布局 可以从上至下分为6大部分 logo栏 menu菜单栏 slide幻灯片 news新闻区域 other其他 bottom底部 二.logo 分为三部分 ①左侧logo ②中间log ...

  2. day02-数据库操作

    一.数据库操作 1.1.创建数据库(增) CREATE DATABASE 也可以使用小写,(注意不要漏掉分号 ;) mysql> create database test; 或 mysql> ...

  3. MySQL主从同步机制及同步中的问题处理

    http://www.drupal001.com/2012/03/mysql-master-slave-troubles/ http://www.jb51.net/article/33052.htm

  4. struts2中的constant介绍之struts.objectFactory与spring的整合

    struts2提供给我们更为灵活的设计,他的很多东西都是可以手动配置的,下面介绍下他的一些 常用的constant作用和配置 struts.objectFactory这个属性用于说明Struts2的 ...

  5. Dockerfile构建MySQL

    转自:https://www.cnblogs.com/jsonhc/p/7807931.html 利用Dockerfile自定义构建MySQL服务折腾了几天,一直在启动服务上出现错误,现在终于解决了该 ...

  6. delphi 新版内存表 FDMemTable

    c++builder XE 官方demo最全60多个 http://community.embarcadero.com/blogs?view=entry&id=8761 FireDAC.Com ...

  7. php版本升级导致openssl无法使用

    也就是call to undefined function openssl错误: 把extension前面的注释去掉,甚至把“libeay32.dll和ssleay32.dll文件复制并替换到apac ...

  8. C++复习:纯虚函数和抽象类

    纯虚函数和抽象类 1基本概念 2抽象类案例   3抽象类在多继承中的应用 C++中没有Java中的接口概念,抽象类可以模拟Java中的接口类.(接口和协议) 3.1有关多继承的说明 工程上的多继承 被 ...

  9. openwrt中防暴力破解shell的脚本

    原文:http://www.right.com.cn/forum/thread-124429-1-1.html 原理:1. snort做入侵检测是很好,但是太大太复杂,我们需要轻量化的操作.当对方进行 ...

  10. python抽象方法

    1.抽象方法的概念 之前我们定义一个基类的时候,如果要求子类必须重写父类中的某一个方法,可以这样做: 定义一个名为Pizza的基类,让其get_radius方法必须被子类继承 class Pizza( ...