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

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

2016-07-27 11:15 519 查看
删除时
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

typedef struct node
{
int data;
node *next;
}Linklist;

Linklist *head, *p, *tail, *q;

void nxcreat(int n)
{
head=new Linklist;
head->next=NULL;
while(n--)
{
p=new Linklist;
cin>>p->data;
p->next=head->next;
head->next=p;
}
}

void display()
{
p=head->next;
while(p->next)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<p->data<<endl;
}

int del(int n)
{
Linklist *t;
p=head->next;
while(p)
{
t=p;
q=p->next;
while(q)
{
if(p->data==q->data)
{
n--;
t->next=q->next;
free(q);
q=t->next;
}
else
{
q=q->next;
t=t->next;
}
}
p=p->next;
}
return n;
}

int main()
{
int n;
cin>>n;
nxcreat(n);
cout<<n<<endl;
display();
cout<<del(n)<<endl;
display();
return 0;
}


用到三个指针
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: