c++ 链表删除重复的数据
//List.h
#include <iostream>
typedef int dataType; struct Node{
Node():data(),pNextNode(NULL){} //结点构造函数
dataType data;
Node* pNextNode;
};
class List{
private:
Node *head; //作为链表唯一的头指针
int size; //链表长度
public:
List(){head=new Node;size=;}
bool isEmpty(); //判断是否空表
bool InsertList(int i,dataType elem); //i的指标从1开始,而不是从0开始
void PushList(dataType elem); //在链表尾部添加元素
bool DeleteList(int i); //删除指定位置的元素
void ClearList(); //清除整条链表
void DeleteRepetitiveData();//删除重复元素
void PrintList(); //按顺序输出链表
int GetSize();
Node* Fine(int i); //找到第i个结点并返回该结点的指针
Node* FinePre(int i); //找到第i个结点前的结点,返回指针
};
//List.cpp
#include "List.h"
#include <iostream>
#include <vector>
using namespace std;
//判断空表
bool List::isEmpty(){
if(head==NULL)
return false;
else
return true;
}
//在第i位插入数据
bool List::InsertList(int i,dataType elem){
if (i<)
return false;
else if(head==NULL||i==)//如果是空表
{
head->data=elem;
size++;
return true;
}
else if (i>size) //位标大于链表长度时,在尾部添加
{
PushList(elem);
return true;
}
else
{
Node *pre=Fine(i-);
Node *follow=Fine(i);
Node *s=new Node; //为新结点申请内存
pre->pNextNode=s; //连接前结点
s->pNextNode=follow; //连接后结点
s->data=elem;
size++;
return true;
}
}
//在尾部添加元素
void List::PushList(dataType elem){
if(head==NULL)
{
head=new Node;
head->data=elem;
size++;
}
else
{
Node *cur=head;
while(cur->pNextNode)
cur=cur->pNextNode;
Node *s=new Node;
cur->pNextNode=s;
s->data=elem;
size++;
}
}
//打印链表
void List::PrintList(){
Node *cur=head;
while (cur!=NULL)
{
cout<<cur->data<<" ";
cur=cur->pNextNode;
}
cout<<endl;
}
//size
int List::GetSize(){
return size;
}
//找到第i个结点
Node* List::Fine(int i){
if(i==)
return head;
else
{
Node *cur=head;
for (int pos=;pos<i;pos++)
cur=cur->pNextNode;
return cur;
}
}
//找到i的前一个个结点
Node* List::FinePre(int i){
if(i<)
{
cout<<"参数必须大于等于2!"<<endl;
return NULL;
}
else if(i==)
return head;
else
return Fine(i-);
}
//删除第i个元素
bool List::DeleteList(int i){
if (i<||i>size) //限制i的范围
return false;
else if(i==)
{
Node *temp=head;
head=head->pNextNode;
delete temp;
size--;
return true;
}
else
{
Node *cur=this->Fine(i);
Node *pre=this->FinePre(i);
pre->pNextNode=cur->pNextNode; //重新连接结点
delete cur;
size--;
return true;
}
}
//清空整个链表
void List::ClearList(){
Node *temp=head;
while (head!=NULL)
{
temp=head;
head=head->pNextNode;
delete temp;
}
size=;
}
//删除重复元素
void List::DeleteRepetitiveData(){
int flag=; //代码运行标志,0为未运行
if(size==) //只有一个元素时跳出
return;
Node *stand=head;//内循环结束后或者找到相同数据后才会指向下个结点
Node *cursor; //游标
vector<int>storge;
for (int i=;i<size;i++)
{ //用for循环是因为要得到被删除元素的位置
//采用握手式比较,算法的时间复杂度为O(n^2)
cursor=stand->pNextNode;
flag=;
while(cursor!=NULL)
{
if(stand->data==cursor->data)
{
stand=stand->pNextNode;
flag=;
//将重复的数据的下标降序保存起来
storge.insert(storge.begin(),i+);
break;
}
else
cursor=cursor->pNextNode;
}
if(!flag)
stand=stand->pNextNode;
}
int itemp=storge.size();
while (itemp--)
{
this->DeleteList(storge.at());//提取下标,释放多余结点
storge.erase(storge.begin());
}
storge.clear(); //释放vector内存
}
//main.cpp
#include "List.h"
#include <iostream>
using namespace std; void main(){
//List类测试代码
List list;
list.InsertList(,);
list.InsertList(,);
list.InsertList(,);
list.InsertList(,);
list.PrintList(); //打印这4个元素
cout<<"链表的长度为:"<<list.GetSize()<<endl;
cout<<"现在删除第2个元素"<<endl;
list.DeleteList();
list.PrintList();
cout<<"现在清空链表"<<endl;
list.ClearList(); //清空链表
cout<<"输出空表:"<<endl;
list.PrintList(); list.PushList();
list.PushList();
list.PushList();
list.PushList();
list.PushList();
list.PushList();
list.PushList(); //压入4 1 7 1 1 2 2
cout<<"输出链表新加入的元素:"<<endl;
list.PrintList();
cout<<"删除相同元素后的链表为:"<<endl;
list.DeleteRepetitiveData();
list.PrintList();
} /*
总结:
1.链表类应该先写Fine()和FindPre(),再写插入和删除
2.链表的结构应该给每一个结点加上一个下标。因为下标在
操作时再加上的话,元素跟下标是不统一的,
如果有元素被删除,下标将要重新分配。
这就导致void List::DeleteRepetitiveData()不能用
相对直接的方法在检测到重复数据时就删除数据,而要借助vector
间接删除
*/
c++ 链表删除重复的数据的更多相关文章
- SQL:一句话删除重复的数据
--构造原始数据 )) --插入数据 INSERT INTO #T (N)VALUES ('A') --方式一:一句话删除重复数据(无主键) --方式二:采用CTQ,with的写法删除 ;
- ORACLE 删除重复的数据
内容转自:https://www.cnblogs.com/zfox2017/p/7676237.html 查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据 ...
- sqlserver删除重复的数据
分享链接: http://blog.csdn.net/s630730701/article/details/52033018 http://blog.csdn.net/anya/article/det ...
- 082 Remove Duplicates from Sorted List II 有序的链表删除重复的结点 II
给定一个有序的链表,删除所有有重复数字的节点,只保留原始列表中唯一的数字.例如:给定 1->2->3->3->4->4->5 ,则返回 1->2->5给 ...
- SQLServer一次性删除重复的数据
delete from [GCPCore].[GCP.Product].[CityMall] where AreaID in(select AreaID from [GCPCore].[GCP.Pr ...
- ROWID-Oracle中删除重复行数据
DELETE FROM DEPT_BAK WHERE ROWID NOT IN (SELECT MIN(ROWID) RID FROM DEPT_BAK GROUP BY DEPTNO,DNAME,L ...
- openquery链表删除时报错 “数据提供程序或其他服务返回 E_FAIL 状态”
DELETE OPENQUERY (VERYEAST_COMPANY_MYSQL_CONN, 'SELECT * FROM company ') WHERE c_userid in(select c_ ...
- 链表有环判断,快慢指针两种方法/合并链表/删除重复元素/二分递归和while
public static boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return fals ...
- sql server删除重复的数据保留一条
DELETE FROM [TCX_1710_SHZJ].[dbo].[PR_BindingTray] WHERE 1=1 AND SNum in (SELECT * FROM ( (SELECT SN ...
随机推荐
- JS滚动条下拉事件
<script type="text/javascript"> window.onscroll = function(){ var t = document.docum ...
- C#中的委托和事件2-2(转)
引言 如果你看过了 C#中的委托和事件2-1 一文,我想你对委托和事件已经有了一个基本的认识.但那些远不是委托和事件的全部内容,还有很多的地方没有涉及.本文将讨论委托和事件一些更为细节的问题,包括一些 ...
- Java内部类和外部类的通信探索
1.内部类访问外部类的成员和方法 在内部类中,可以无障碍地访问外部类的所有成员和方法. 在下面的实验代码中,可以看到,内部类sl可以访问外部类的私有成员:sz 和 cur. 同时可以访问私有方法:pr ...
- 【学习笔记】【Foundation】数组
数组:可重复,有序 不可变数组 创建数组 //array开头的方法是类方法,init开头的是实例方法 //NSArray* arr=[[NSArray alloc]initWithObjects:@& ...
- iOS开发——C篇&动态内存分配
再C语言中关于内存是一个很重要的知识点,所以今天我就从c语言的内存分配开始为大家解析一下C语言再iOS开发中非常重要的一些知识. 1:malloc函数的介绍 C语言中开辟内存空间:malloc函数 再 ...
- 将Android 工程从Windows导入到Ubuntu 下java类中的中文在ADT中乱码
解决方法: 右键工程-->Properties->Resource->Text file encoding->Other 手动输入GBK ->点击OK (Other 中是 ...
- CRegKey 注册表操作
CRegKey 注册表操作 标签: accessnulluserpathbyteie 2011-11-03 13:55 3477人阅读 评论(0) 收藏 举报 分类: win32(7) 1.简介 ...
- 【HDOJ】4515 小Q系列故事——世界上最遥远的距离
简单题目,先把时间都归到整年,然后再计算.同时为了防止减法出现xx月00日的情况,需要将d先多增加1,再恢复回来. #include <cstdio> #include <cstri ...
- 【HDOJ】1104 Remainder
bfs. #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> ...
- BZOJ3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: 27[Sub ...