/*在一个递增有序的线性表中,有数值相同的元素存在。若存储方式为单链表,设计算法去掉数值相同的元素,
使表中不再有重复的元素,例如(7,10,10,21,30,42,42,42,51,70)将变为(7,10,21,30,42,51,70)。*/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList createHeadList(LinkList &L)
{
LNode *r;
L = (LinkList)malloc(sizeof(LNode));
r = L;
ElemType x;
scanf("%d",&x);
while(x != 9999)
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d",&x);
}
r->next = NULL;
return L;
}
LinkList deleSameVal(LinkList &L)
{
LNode *p = L->next;
LNode *q;
if(p == NULL) return NULL;
while(p->next)
{
q = p->next;
if(p->data == q->data)
{
p->next = q->next;
free(q);
}else
p = p->next;
}
return L;
}
bool printList(LinkList L)
{
LNode *p = L->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return true;
}
void main()
{
LinkList L;
createHeadList(L);
printList(L);
deleSameVal(L);
printList(L);
}
04-09
364
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)