链表所有重复元素的删除
2017-12-24 15:26
357 查看
实现链表所有重复元素的删除
#include<stdio.h>
#include<stdlib.h>
typedef struct _Node
{
int data;
struct _Node *next;
}Node;
typedef struct _list{
Node *head;
}List;
void add(List *pList,int number)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data=number;
p->next=NULL;
Node *last=pList->head;
if(last)
{
while(last->next)
{
last=last->next;
}
last->next=p;
}
else
pList->head=p;
}
void add2(List *pList,int number)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data=number;
p->next=pList->head;
pList->head=p;
}
void deletel(List *pList)
{
Node *p;
Node *q;
Node *r;
for(p=pList->head;p;p=p->next)
{
q=p;
while(q->next)
{
if(p->data==q->next->data)
{
r=q->next;
q->next=r->next;
free(r);
}
else
{
q=q->next;
}
}
}
}
void showL(List *pList)
{
Node *p;
for(p=pList->head;p;p=p->next)
{
if(p->next)
printf("%d ",p->data);
else
printf("%d\n",p->data);
}
}
int count(List *pList)
{
Node *p;
int m=0;
for(p=pList->head;p;p=p->next)
{
m++;
}
return m;
}
int main()
{
List L;
L.head=NULL;
int n,temp,m;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&temp);
add2(&L,temp);
}
m=count(&L);
printf("%d\n",m);
showL(&L);
deletel(&L);
m=count(&L);
printf("%d\n",m);
showL(&L);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct _Node
{
int data;
struct _Node *next;
}Node;
typedef struct _list{
Node *head;
}List;
void add(List *pList,int number)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data=number;
p->next=NULL;
Node *last=pList->head;
if(last)
{
while(last->next)
{
last=last->next;
}
last->next=p;
}
else
pList->head=p;
}
void add2(List *pList,int number)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data=number;
p->next=pList->head;
pList->head=p;
}
void deletel(List *pList)
{
Node *p;
Node *q;
Node *r;
for(p=pList->head;p;p=p->next)
{
q=p;
while(q->next)
{
if(p->data==q->next->data)
{
r=q->next;
q->next=r->next;
free(r);
}
else
{
q=q->next;
}
}
}
}
void showL(List *pList)
{
Node *p;
for(p=pList->head;p;p=p->next)
{
if(p->next)
printf("%d ",p->data);
else
printf("%d\n",p->data);
}
}
int count(List *pList)
{
Node *p;
int m=0;
for(p=pList->head;p;p=p->next)
{
m++;
}
return m;
}
int main()
{
List L;
L.head=NULL;
int n,temp,m;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&temp);
add2(&L,temp);
}
m=count(&L);
printf("%d\n",m);
showL(&L);
deletel(&L);
m=count(&L);
printf("%d\n",m);
showL(&L);
}
相关文章推荐
- 链表逆转以及删除所有重复元素
- 给定一个排序链表,删除所有重复的元素每个元素只留下一个。
- 删除链表中重复元素
- 数据结构实验之链表七:单链表中重复元素的删除
- 2122数据结构实验之链表七:单链表中重复元素的删除
- 删除排序链表中的重复元素
- 数据结构实验之链表七:单链表中重复元素的删除
- 数据结构实验之链表七:单链表中重复元素的删除
- 删除排序链表中的重复元素
- 数据结构实验之链表七:单链表中重复元素的删除 (SDUT 2122)
- Lintcode删除排序链表中的重复元素
- 3597 Problem F 算法2-25 有序单链表删除重复元素
- 数据结构实验之链表七:单链表中重复元素的删除
- [2178]链表的有序集合 (链表的插排法+重复元素的删除) SDUT
- 数据结构实验之链表七:单链表中重复元素的删除
- 数据结构实验之链表七:单链表中重复元素的删除
- 数据结构实验之链表七:单链表中重复元素的删除
- 数据结构实验之链表七:单链表中重复元素的删除
- 牛客网剑指Offer——删除链表中重复元素
- 单链表删除有序重复元素