简体   繁体   中英

I cannot print delete_first result in C (circular linkedlist)

I want to print delete_first result and delete_last result. But it can only print delete_last code. How can i solve this matter?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

void print_list(ListNode* head)
{
    ListNode* p;

    if (head == NULL) return;
    else {
        p = head->link;
        do {
            printf("%d ", p->data);
            p = p->link;
        } while (p != head->link);
    }printf("\n");
}




ListNode* insert_first(ListNode* head, element data)
{
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    node->data = data;
    if (head == NULL) {
        head = node;
        node->link = head;
    }
    else {
        node->link = head->link;
        head->link = node;
    }
    return head;
}

ListNode* insert_last(ListNode* head, element data)
{
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    node->data = data;
    if (head == NULL) {
        head = node;
        node->link = head;
    }
    else {
        node->link = head->link;
        head->link = node;
        head = node;
    }
    return head;
    }

  

I want to print delete_first result and delete_last result. But I can only print delete_last result. How can i print delete_first result?

This is my delete_first code.

  ListNode* delete_first(ListNode* head) {
        if (head == NULL)
        {
            return head;
        }
        if (head->link == head) {
            free(head);
            head = NULL;
            return head;
        }
        ListNode* p = head->link;
        head->link = p->link;
        free(head);
        head = NULL;
        return head;
    }



    ListNode* delete_last(ListNode* head) {
        if (head == NULL)
            return head;
        ListNode* p = head->link;
        if (head->link == head) {
            free(head);
            head = NULL;
            return head;
        }
        while (p->link != head) {
            p = p->link;
        }
        p->link = head->link;
        free(head);
        head = p;
        return head;
    }

int main() {
    ListNode* head = NULL;

    head = insert_first(head, 10);
    head = insert_first(head, 20);
    head = insert_first(head, 30);
    head = insert_first(head, 40);
    head = insert_last(head, 50);
    print_list(head);

    
    head=delete_last(head);
    print_list(head);
    
    head = delete_first(head);
    print_list(head);

    return 0;
}

If you want to print the element being removed, then you cannot free() it, or you have to copy it to a local variable and return it by value.

If you want caller to be able to print what remains of the list, then you need to find the last node which has a link that points at head and have it point at head->next instead. Then return that value (instead of NULL):

ListNode* delete_first(ListNode* head) {
    if(!head)
       return NULL;
    if(head == head->link) {
       free(head);
       return NULL;
    }
    ListNode* last;
    for(last = head; last->link != head; last = last->link);
    last->link = head->link;
    free(head);
    return last->link;
}

There is no point of setting head = NULL as the copy of that variable is lost the caller anyways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM