删除链表中的重复元素(链表为随机排列,请使用C语言)

删除链表中的重复元素(链表为随机排列,请使用C语言)

问题描述:

请编写程序删除链表中的多余节点,即:若链表中有多个节点具有相同的值,则只保留其中的一个节点即可,使得处理后的链表中的值各不相同。
【输入】第一行整数n(1<=n)
第二行为单链表中的n个整数
【输出】删除链表中的重复元素后的单链表信息
例如:
【输入】
9
3 6 9 10 6 1 3 10 6
【输出】
3 6 9 10 1

我想创建一个双向链表,然后包含两个循环,第一个循环把n-1个元素取一遍,第二个循环把第一个循环取出的元素与他之后的所有元素进行比较,如果所指的值相等就删除节点,但是运行后我的删除操作好像没有起作用?最好帮我看看我的程序哪里出了问题我直接改就可以,当然我代码不太规范如果看不懂的话另附上代码也可以(大一,刚看到链表,请使用链表及之前的知识)。

#include <stdio.h>
#include <stdlib.h>
typedef struct List{
    int value;
    struct List* next;
    struct List* pre;
}list;
list* add(list* head,int n);
list* delete(list* head,int n);
int main()
{
    int n;
    scanf("%d",&n);
    list* head=NULL;
    head=add(head, n);
    head=delete(head, n);
    list *p3=head;
    while (p3->next!=NULL) {
        printf("%d ",p3->value);
        p3=p3->next;
    }
    printf("%d",p3->value);
}
list* add(list* head,int n)
{
    for (int i=0; i<n; i++) {
        list* p=(list*)malloc(sizeof(list));
        scanf("%d",&p->value);
        p->next=NULL;
        p->pre=NULL;
        if (head==NULL) {
            head=p;
        }
        else{
            list* last=head;
            while (last->next!=NULL) {
                last=last->next;
            }
            last->next=p;
            p->pre=last;
        }
    }
    return head;
}
list* delete(list* head,int n)
{
    list* last=head->next;
    list* p=head;
    while(p->next!=NULL) {
        while(last->next!=NULL){
        if (last->value==p->value) {
            list* p1=last->pre;
            p1->next=last->next;
            last->next->pre=p1;
            free(last);
        }
            last=last->next;
        }
        p=p->next;
    }
    return head;
}

修改如下,供参考:

#include <stdio.h>
#include <stdlib.h>
typedef struct List {
    int value;
    struct List* next;
    struct List* pre;
}list;
list* add(list* head, int n);
list* Delete(list* head);  //, int n);
int main()
{
    int n;
    scanf("%d", &n);
    list* head = NULL;
    head = add(head, n);
    head = Delete(head);//head = Delete(head, n);
    list* p3 = head;
    while (p3->next != NULL) {
        printf("%d ", p3->value);
        p3 = p3->next;
    }
    printf("%d", p3->value);
}
list* add(list* head, int n)
{
    for (int i = 0; i < n; i++) {
        list* p = (list*)malloc(sizeof(list));
        scanf("%d", &p->value);
        p->next = NULL;
        p->pre = NULL;
        if (head == NULL) {
            head = p;
        }
        else {
            list* last = head;
            while (last->next != NULL) {
                last = last->next;
            }
            last->next = p;
            p->pre = last;
        }
    }
    return head;
}
list* Delete(list* head)   //, int n)
{
    list* last; //= head->next;
    list* p = head;
    while (p) {  //(p->next != NULL) {
        last = p->next;
        while (last){  //last->next != NULL) {
            if (last->value == p->value) {
                list* p1 = last;  //last->pre;
                last = p1->pre;
                last->next = p1->next;//p1->next = last->next;
                if (p1->next != NULL)
                    p1->next->pre = last;//last->next->pre = p1;
                free(p1);//free(last);
            }
            last = last->next;
        }
        p = p->next;
    }
    return head;
}