重复元素的删除
2016-03-30 08:51
288 查看
数据结构实验之链表七:单链表中重复元素的删除
Time Limit: 1000MS Memory limit: 65536K
题目描述
按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。
输入
第一行输入元素个数n;第二行输入n个整数。
输出
第一行输出初始链表元素个数;第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; struct node { int data; node *next; }; node *head, *p; node *creat(int n); node *del(node *head, int n); int main() { int n, m; scanf("%d", &n); head = creat(n); printf("%d\n", n); p = head -> next; while(p) { if(p -> next) { printf("%d ", p -> data); } else { printf("%d\n", p -> data); } p = p -> next; } head = del(head, n); p = head -> next; while(p) { if(p -> next) { printf("%d ", p -> data); } else { printf("%d\n", p -> data); } p = p -> next; } return 0; } node *creat(int n) { node *head, *p; head = new node; head -> next = NULL; for(int i = 0; i < n; i ++) { p = new node; scanf("%d", &p -> data); p -> next = head -> next; head -> next = p; } return head; } node *del(node *head, int n) { node *p, *q, *tail; p = head -> next; while(p) { tail = p; q = p -> next; while(q) { if(p -> data == q -> data) { tail -> next = q -> next; q = tail -> next; n--; } else { tail = tail -> next; q = q -> next; } } p = p -> next; } printf("%d\n", n); return head; }
相关文章推荐
- UnsatisfiedLinkError nativeLibraryDirectories=[/vendor/lib64, /system/lib64
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
- Socket连接与HTTP连接
- ie 7/8不支持trim方法
- Android源码编译环境搭建问题及解决方案小结
- 标准Web系统的架构分层
- 设计的目标
- android基础学习之通过Edittext为spinner添加内容
- 欢迎使用CSDN-markdown编辑器
- BaseServlet 继承 httpServlet
- 2016年3月26日--4月2日(共16小时,剩3036小时)
- algrothm_面向对象_封装
- mongodb_Mongo3.2.4框架及角色介绍
- 目标检测程序开发(一)——OpenCV2.4.11环境配置
- 分布式系统架构的基本原则和实践(转)
- 安全编程
- 基于jQuery实现Ajax验证用户名是否存在实例
- 深度优先搜索DFS(迷宫问题)
- 【BZOJ-1588】营业额统计 Splay
- CSS Hack解析