如何删除循环链表中的所有数据?

时间:2018-05-08 11:22:21

标签: c data-structures linked-list

 #include <stdio.h>
 #include <stdlib.h>
 #define TRUE 1
 #define FALSE 0

 typedef struct ListNode
 {
     int data;
     struct ListNode *link;
 } ListNode;

 typedef struct List
 {
     ListNode *head;
     ListNode *tail;
 } List;

void list_init(List *header)
{
    header->head = NULL;
    header->tail = NULL;
}

void insert_node(List *header, int data)
{
    ListNode *new_node = (ListNode *)malloc(sizeof(ListNode));

    if (header->head == NULL)
    {
        header->head = new_node;
        header->tail = new_node;
        new_node->data = data;
        new_node->link = header->tail;

        return;
    }

    if (header->head != NULL)
    {
        new_node->link = header->head;
        new_node->data = data;
        header->head = new_node;
        header->tail->link = header->head;

        return;
    }
}

void delete_node(List *header, int key)
{
    if (header->head == NULL)
        return;

    ListNode *curr = header->head;
    ListNode *prev;

    while (curr->data != key)
    {
        if (curr->link == header->head)
            break;

        prev = curr;
        curr = curr->link;
    }

    if (curr->link == header->head)
    {
        header->head = NULL;
        free(curr);
        return;
    }

    if (curr == header->head)
    {
        prev = header->head;

        while (prev->link != header->head)
            prev = prev->link;

        header->head = curr->link;
        prev->link = header->head;
        free(curr);
        return;
    }

    else if (curr->link == header->head)
    {
        prev->link = header->head;
        free(curr);
        return;
    }

    else
    {
        prev->link = curr->link;
        free(curr);
        return;
    }
}

List *reverse(List *header)
{
    ListNode *p, *q, *r;

    if (header->head->link == header->head || header->head == NULL)
        return header;

    p = header->head->link;
    q = header->head;

    while (p != header->head) 
    {
        r = q;
        q = p;
        p = p->link;
        q->link = r;
    }

    header->head = q;
    header->tail = p;
    p->link = q;

    return header;
}

void display(List *header)
{
    ListNode *cur = header->head;

    printf("< ");

    do
    {
        printf("%d ", cur->data);
        cur = cur->link;
    } while (cur != header->head);

    printf(">\n");

    printf("Head : %d\n", header->head->data);
    printf("Tail : %d\n", header->tail->data);
    printf("Tail Next : %d\n", header->tail->link->data);
}


int main()
{
    List list;
    int data, delete_data = 172;
    int num_of_data = 0, i;

    list_init(&list);

    FILE *fp = fopen("data2.txt", "r");

    if (fp == NULL)
    {
        printf("FILE OPEN ERROR!\n");
        return 0;
    }

    while (!feof(fp))
    {
        fscanf(fp, "%d", &data);
        insert_node(&list, data);
        num_of_data++;
    }

    display(&list);
    printf("\n");

    reverse(&list);
    delete_node(&list, delete_data);

    printf("< Delete %d >\n", delete_data);
    printf("< Reverse >\n\n");
    display(&list);

    return 0;
}

data2.txt 172 473 273 584 182 584 172 734 162 372 574 964 543 372 172

我想删除所有172号数据。

如何更改函数delete_node?

如何使用delete_node制作新功能?

&LT; 172 473 273 584 182 584 172 734 162 372 574 964 543 372 172&gt;

负责人:172

尾巴:172

尾巴下一个:172

&LT;删除172&gt;

&LT;反转&gt;

&LT; 473 273 584 182 584 734 162 372 574 964 543 372&gt;

负责人:473

尾巴:372

尾巴下一个:473

当我使用删除功能时,这是结果。

&LT; 372 543 964 574 372 162 734 584 182 584 273 473&gt;

负责人:372

尾巴:473

尾巴下一个:372

这是使用删除功能后使用反转功能的结果。

enter image description here

这张图片是我想要的结果。

1 个答案:

答案 0 :(得分:0)

如果找到并删除了某个节点,您可以修改delete_node函数返回1,并在其他情况下返回0。

删除所有172个节点将很容易:

while (delete_node(&list, delete_data) == 1);

备注:我没有把整个代码放在这里,因为delete_node函数存在一些问题,问题与此无关。但是你应该首先纠正它们,以便每个人都能工作。例如,如果您在单个节点的列表中删除它,它将删除该数据所在的节点。