您的位置:首页 > 理论基础 > 数据结构算法

sdut2122数据结构实验之链表七:单链表中重复元素的删除

2016-11-06 15:11 260 查看
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
void show(LinkList L)
{
LinkList p;
p=L->next;
while(p)
{
if(p->next==NULL)
cout<<p->data<<endl;
else
cout<<p->data<<" ";
p=p->next;
}
}
void CreateList_L(LinkList &L,int n)
{
LinkList p;
p=new LNode;
L=new LNode;
L->next=NULL;
LinkList q;
q=new LNode;
cin>>q->data;
q->next=NULL;
L->next=p;
for(int i=2;i<=n;i++)
{
p=new LNode;
cin>>p->data;
p->next=q;
L->next=p;
q=p;
}
cout<<n<<endl;
show(L);
}
void deletenum(LinkList &L,int n)
{
LinkList p,p1,p2;
p=L;
while(p->next!=NULL)
{
p1=p;
p2=p->next;
while(p2!=NULL)
{
if(p->data==p2->data)
{
p1->next=p2->next;
p2=p2->next;
n--;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
p=p->next;
}
cout<<n<<endl;
show(L);
}
int main()
{
LinkList L;
int n;
cin>>n;
CreateList_L(L,n);
deletenum(L,n);
return 0;
}

/***************************************************
User name: TJRAC6015203228魏杰
Result: Accepted
Take time: 0ms
Take Memory: 156KB
Submit time: 2016-11-01 13:59:38
****************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: