单链表操作B 分类: 链表 2015-06-07 12:42 15人阅读 评论(0) 收藏
2015-06-07 12:42
363 查看
数据结构上机测试2-2:单链表操作B
TimeLimit: 1000ms Memory limit: 65536K
题目描述
按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。
输入
第一行输入元素个数n;
第二行输入n个整数。
输出
第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
示例输入
10
2130 14 55 32 63 11 30 55 30
示例输出
10
3055 30 11 63 32 55 14 30 21
7
3055 11 63 32 14 21
TimeLimit: 1000ms Memory limit: 65536K
题目描述
按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。
输入
第一行输入元素个数n;
第二行输入n个整数。
输出
第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
示例输入
10
2130 14 55 32 63 11 30 55 30
示例输出
10
3055 30 11 63 32 55 14 30 21
7
3055 11 63 32 14 21
#include <bits/stdc++.h> #define WW freopen("input.txt","r",stdin) #define RR freopen("ouput.txt","w",stdout) using namespace std; struct node { int data; node *next; }*head; int n; void Creat() { node *q; for(int i=1; i<=n; i++) { q=new node; q->next=head->next; cin>>q->data; head->next=q; } } void Ouput() { node *p; p=head->next; cout<<n<<endl; while(p) { if(p!=head->next) cout<<" "; cout<<p->data; p=p->next; } cout<<endl; } void Delete() { node *q,*p,*r; p=head->next; while(p) { q=p->next; r=p; while(q) { if(p->data==q->data) { r->next=q->next; free(q); q=r->next; n--; } else { q=q->next; r=r->next; } } p=p->next; } } int main() { head=new node; head->next=NULL; cin>>n; Creat(); Ouput(); Delete(); Ouput(); return 0; }
相关文章推荐
- Validate Binary Search Tree
- 设计模式之十三:适配器模式(Adapter)
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )
- 使用JQuery实现全选的功能
- URL重写的几种实现方式的总结
- [c++]String字符串类的运算符重载
- apache+tomcat二级域名+url重写
- 运营开源公司的三个经验教训
- 让vs2008与vs2012同时打开同一个项目文件
- Apaceh配置虚拟主机步骤
- 单链表操作
- QLabel实现超链接,设置超链接颜色,去掉超链接下划线方法
- 最近学习的总结
- python并行计算的简单实现--pp
- 单链表操作 分类: 链表 2015-06-07 12:38 14人阅读 评论(0) 收藏
- 自定义拖拽框插件
- honeyd蜜罐配置和web监听脚本
- 用vs2008打开vs2012项目
- 深入理解C++中public、protected及private用法
- 仿flash运动框架