Deduplication on a Linked List(两个测试点)

#include <stdio.h>

#include <stdlib.h>

typedef int ElementType;

typedef struct LinkedNode *LinkedList;

struct LinkedNode {

    ElementType Data;

    LinkedList link;

    int Address,Next;

};

LinkedList Delete(LinkedList Head, LinkedList *DeleteRear);

void PrintLinkedList(LinkedList Head);

int main()

{

    int N,Head,Address,Key,Next;

    scanf("%d %d",&Head, &N);

    LinkedList Array[100000];

    int *AddressArray = malloc(sizeof(int)*N);

    int i = 0;

    while (N--) {

        scanf("%d %d %d",&Address,&Key,&Next);

        Array[Address] = malloc(sizeof(struct LinkedNode));

        Array[Address]->Data = Key;

        AddressArray[i++] = Address;

        Array[Address]->Address = Address;

        Array[Address]->Next = Next;

    }

    for (i = i-1; i>=0; i--) {

        if (Array[AddressArray[i]]->Next == -1) {

            Array[AddressArray[i]]->link = NULL;

        }else

        {

            Array[AddressArray[i]]->link = Array[Array[AddressArray[i]]->Next];

        }

    }

    LinkedList head = Array[Head];

    LinkedList DeleteHead = malloc(sizeof(struct LinkedNode));

    DeleteHead->link = NULL;

    LinkedList Rear = DeleteHead;

    head = Delete(head, &Rear);

    PrintLinkedList(head);

    PrintLinkedList(DeleteHead->link);

    return 0;

}

LinkedList Delete(LinkedList Head, LinkedList *DeleteRear)

{

    LinkedList Result = Head;

    while (Head) {

        LinkedList Temp = Head->link;

        LinkedList Last = Head;

        while (Temp) {

            if (Temp->Data == Head->Data || -(Temp->Data) == Head->Data) {

                (*DeleteRear)->link = Temp;

                *DeleteRear = Temp;

                Last->link = Temp->link;

            }

            Last = Temp;

            Temp = Temp->link;

        }

        Head = Head->link;

    }

    (*DeleteRear)->link = NULL;

    return Result;

}

void PrintLinkedList(LinkedList Head)

{

    while (Head) {

        printf("%05d %d ",Head->Address,Head->Data);

        if (Head->link == NULL) {

            printf("-1\n");

        }else

        {

            printf("%05d\n",Head->link->Address);

        }

        Head = Head->link;

    }

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值