C : Deleting only one node from a doubly linked list

标签 c doubly-linked-list

我正在编写一个程序来创建一个双向链表并从中删除某个元素。一切都很好,除了列表仅包含 1 个元素的部分,当我尝试删除它时,程序崩溃了。有什么建议吗?

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

struct Node{
    int data;
    struct Node* next;
    struct Node* prev;
};
struct Node* head;
struct Node* tail;

struct Node* CreateNewNode(int x){
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=x;
    newNode->next=NULL;
    newNode->prev=NULL;
    return newNode;
}

void InsertAtBegin(int x){
    struct Node* newNode=CreateNewNode(x);
    if(head == NULL) {
        head = newNode;
        tail = newNode;
        return;
    }
    head->prev = newNode;
    newNode->next = head;
    head = newNode;
}

void InsertAtEnd(int x){
    struct Node* newNode=CreateNewNode(x);
    if(tail==NULL){
        head=newNode;
        tail=newNode;
        return;
    }
    tail->next=newNode;
    newNode->prev=tail;
    tail=newNode;
}

struct Node* PointTo(int k){
    struct Node* curr=head;
    int i=1;
    while(curr!=NULL && i<k){
        curr=curr->next;
        i++;
    }
    return curr;
}

void DelFromList(int k){
    struct Node* temp;
    temp=PointTo(k);

    if(k==1){
        head=temp->next;
        temp->next->prev=NULL;
        free(temp);
        return;
    }
    if(temp->next==NULL){
        temp->prev->next=NULL;
        tail=temp->prev;
        free(temp);
        return;
    }
    if(temp->next==NULL && temp->prev==NULL){
        head=NULL;
        tail=NULL;
        printf("atpazista\n");
        free(temp);
        return;
    }
    temp->next->prev=temp->prev;
    temp->prev->next=temp->next;
    free(temp);
}

void Print(){
    struct Node* temp = head;
    if(head==NULL){
        printf("List empty\n");
        return;
    }
    printf("List: ");
    while(temp != NULL) {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

void Read(int *x){
    while(scanf("%d", x)==0){
        printf("NUMBERS ONLY\n");
        scanf("%s");
    }
}

void Free(){
    struct Node* temp=head;
    while(temp->next!=NULL){
        temp=temp->next;
        free(temp->prev);
    }
}

int main()
{
    int n=0, x;
    printf("Number of elements?\n");
    while(n<=0){
        Read(&n);
    }
    int i;
    for(i=0; i<n; i++){
        printf("Type a number\n");
        Read(&x);
        InsertAtEnd(x);
    }
    Print();
    printf("Head: %d\n", head->data);
    printf("Tail: %d\n", tail->data);


    printf("Number of element to be deleted?\n");
    n=0;
    while(n<=0){
        Read(&n);
    }
    DelFromList(n);


    Print();
    printf("Head: %d\n", head->data);
    printf("Tail: %d\n", tail->data);

    Free();


    return 0;
}

最佳答案

也许交换顺序:

if(temp->next==NULL){
    temp->prev->next=NULL;
    tail=temp->prev;
    free(temp);
    return;
}
if(temp->next==NULL && temp->prev==NULL){
    head=NULL;
    tail=NULL;
    printf("atpazista\n");
    free(temp);
    return;
}

后面的代码永远不会执行,因为第一个案例已经执行并返回

关于C : Deleting only one node from a doubly linked list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34405531/

相关文章:

cublas 矩阵乘法不符合预期

c++ - 数组中有多少个数字小于给定数字?

c - “prev”函数中的“current”和 “__schedule”有什么区别,为什么不直接使用current呢?

python - Python 列表是单链表还是双向链表?

C:删除第一个元素时双向链表中的段错误

c - 双向链表 - 段错误

C 在 bash 中读取 : stdin and stdout

c - 使用 sscanf 时出现段错误

java - 在链接列表中查找最小值

c++ - 双向链表 - 是什么导致我的代码抛出编译器错误,我该如何解决?