您的位置:首页 > 其它

去除单链表中的重复元素

2016-03-29 15:34 260 查看
#include <stdio.h>
#include <stdlib.h>

struct Linklist{
char data;
struct Linklist * next;
};

Linklist * createLinklist(){
Linklist * head,* p,* q;
char x;
head = (Linklist *)malloc(sizeof(Linklist));
head->next = NULL;
q = head;
printf("please input the values!\n");
scanf("%c",&x);
while(x != '\n'){
p = (Linklist *)malloc(sizeof(Linklist));
p->data = x;
p->next = NULL;
q->next = p;
q = p;
scanf("%c",&x);
}
return head;
}

void delRepeat(Linklist * head){
Linklist * r,* p,* q;
p = head->next;
while(p!=NULL){
q = p->next;
r = p;
while(q!=NULL){
if(q->data == p->data){
r->next = q->next;
free(q);
q = r->next;
}else{
q = q->next;
r = r->next;
}
}
p = p->next;
}
}

void print(Linklist * head){
Linklist * p;
p = head->next;
while(p!=NULL){
printf("%c",p->data);
p = p->next;
}
}

int main(){

Linklist * head;
head = createLinklist();
printf("the original elements of the list are:\n");
print(head);
printf("\n\n");

printf("after delete\n");
delRepeat(head);
print(head);
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: