单链表中重复元素的删除

# include <stdio.h>
# include <malloc.h>
# include <string.h>
typedef struct node
{
    struct node *next;
    int data;
    int num;
}LINK;
int main()
{
    int n,i,j,k=0;
    LINK *p,*head,*q;
    head=(LINK *)malloc(sizeof(LINK));
    head->next=NULL;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        p=(LINK *)malloc(sizeof(LINK));
        scanf("%d",&p->data);
        p->num=1;
        p->next=head->next;
        head->next=p;
    }
    printf("%d\n",n);
    p=head->next;
    for(i=0;i<n;i++)
    {
        if(i)printf(" ");
        printf("%d",p->data);
        p=p->next;
    }
    p=head->next;
    while(p)
    {
        q=p->next;
        while(q)
        {
            
            if(q->data==p->data)
            {
                q->num=0;
            }
            q=q->next;
        }
        p=p->next;
    }
    p=head->next;
    while(p)
    {
        if(p->num)
            k++;
        p=p->next;
    }
    printf("\n%d\n",k);
    p=head->next;
    for(i=0;i<n;i++)
    {
        if(p->num)
        {
            if(i)
                printf(" ");
            printf("%d",p->data);
        }
        p=p->next;
    }
    printf("\n");
    return 0;
} 

 

posted on 2013-09-25 18:15  随风浪子的博客  阅读(132)  评论(0编辑  收藏  举报

导航