【问题标题】:Doubly linked list reversing - printing out garbage data双向链表反转——打印出垃圾数据
【发布时间】:2016-12-07 15:28:40
【问题描述】:

这部分代码有问题。我的目标是反转一个双向链表。当我尝试打印反向列表时收到垃圾值。

typedef struct node{
    int val;
    struct node* prev;
    struct node* next;
}Node;

typedef struct list{
    Node* head;
    Node* tail;
}List;

void pushFront(List* l, Node* node){
    if(l->head == NULL){
        l->head = node;
        l->tail = node;
        l->tail->next = NULL;
    }else{
        l->head->prev = node;
        node->next = l->head;
        l->head = node;
    }
}
void printList(List* list){

    Node *ptr = list->head;
    while(ptr != NULL){
        printf("%i ",ptr->val);
        ptr = ptr->next;
    }
    puts("");
    free(ptr);
}

void reverse(List* lista){

    Node* ptr = lista->head;
    Node* temp = NULL;
    while(ptr != NULL){
        temp = ptr->prev;
        ptr->prev = ptr->next;
        ptr->next = temp;
        ptr = ptr->prev;
    }

    if(temp != NULL)
        lista->head = temp->prev;
    free(ptr);
    free(temp);
}

我收到的输出:

原始列表:1 2 3 4 5 6 7

倒排列表:1 8532616 3 4 5 6 7 8528368 2002618240

【问题讨论】:

  • if(temp != NULL) lista->head = temp->prev; 嗯?这是做什么的?列表有一个头指针和一个尾指针,它们应该怎么办?
  • 在您的printList 中,完成后调用free(ptr),这相当于free(NULL) 什么都不做,但出于任何目的都不需要它。

标签: c data-structures struct reverse doubly-linked-list


【解决方案1】:

我猜你在同一个列表上使用了你的函数printList 两次(一次在反转列表之前和一次之后)这会导致未定义的行为,因为你在printList 期间释放你的列表然后尝试访问和使用那些相同的内存位置->当你没有完成时不要释放你的东西:

void printList(List* list){

    Node *ptr = list->head;
    while(ptr != NULL){
        printf("%i ",ptr->val);
        ptr = ptr->next;
    }
    puts("");
    // free(ptr); --> Remove this line
}

【讨论】:

    【解决方案2】:

    为什么要释放 printList() 和 reverse() 中的节点? 在 C 中,您应该只释放先前分配的变量,例如使用 malloc()。 当您在 C 函数中声明变量时,它将自动分配到堆栈或其他内存区域(甚至在 CPU 寄存器中)。它们也将在您的函数结束时自动释放。 如果您要动态分配节点,然后在“反向”函数中释放它们,那么当您读取释放的节点时,我希望看到垃圾。 我试图删除“免费”调用,并且代码运行良好。 https://ideone.com/CN1MaC

    #include <stdio.h>
    
    typedef struct node{
        int val;
        struct node* prev;
        struct node* next;
    }Node;
    
    typedef struct list{
        Node* head;
        Node* tail;
    }List;
    
    void pushFront(List* l, Node* node){
        if(l->head == NULL){
            l->head = node;
            l->tail = node;
            l->tail->next = NULL;
        }else{
            l->head->prev = node;
            node->next = l->head;
            l->head = node;
        }
    }
    void printList(List* list){
    
        Node *ptr = list->head;
        while(ptr != NULL){
            printf("%i ",ptr->val);
            ptr = ptr->next;
        }
        puts("");
    }
    
    void reverse(List* lista){
    
        Node* ptr = lista->head;
        Node* temp = NULL;
        while(ptr != NULL){
            temp = ptr->prev;
            ptr->prev = ptr->next;
            ptr->next = temp;
            ptr = ptr->prev;
        }
    
        if(temp != NULL)
            lista->head = temp->prev;
    }
    
    int main(void) {
        List list = { NULL, NULL };
        Node nodeArr[7];
        int i;
    
        for( i = 0; i < 7; i++ )
        {
            nodeArr[i].val = 7 - i;
            nodeArr[i].prev = NULL;
            nodeArr[i].next = NULL;
            pushFront(&list, &nodeArr[i]);
        }
    
        printList(&list);
        reverse(&list);
        printList(&list);
    
        // your code goes here
        return 0;
    }
    

    输出:

    1 2 3 4 5 6 7 
    7 6 5 4 3 2 1 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 2021-11-29
      相关资源
      最近更新 更多