去除单链表中的重复元素

来源:互联网 发布:笔记本预装linux 编辑:程序博客网 时间:2024/06/05 17:23
#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;}
0 0