C语言 删除单链表中的节点时出现问题

eeq64g8w  于 2023-05-28  发布在  其他
关注(0)|答案(1)|浏览(140)

单链表的问题
所以我正在做删除节点的单链表,我做了3个函数,但不知何故,它创建了无限的数据,虽然它删除了我需要的数据。下面是我的代码:

Node delHead(Node head){
    if(head==NULL){
        printf("There is nothing to delete!");
    }
    else{
        head=head->next;
    }
    return head;
}

Node delTail(Node head){
    if(head==NULL||head->next==NULL){
        return delHead(head);
    }else{
        Node p = head;
        Node prev = NULL;
        while(p->next !=NULL){
            prev=p;
            p=p->next;
        }
        prev->next= NULL;
        free(p);
        return head;
    }
}
Node delAt(Node head, int position){
    if(position == 0 || head == NULL || head->next == NULL){
        head = delHead(head); 
    }else{
        int k = 0;
        Node p = head;
        Node prev = NULL;
        while(p != NULL && k != position){
            prev = p;
            p=p->next;
            k++;
        }
        if(k != position){
            head = delTail(head);
        }else{
            prev->next=p->next;
            free(p);
        }
    }
    return head;
}

我正在使用visual studio代码,所以我希望有人能用这个来修复我。我试着p->next->next,但没有成功,所以非常感谢你们。谢谢你!

rxztt3cl

rxztt3cl1#

正如在另一条评论中提到的,您应该提供一个示例,说明您如何编码以及它给您的输出。我只注意到一个问题,你使用了一个节点而不是一个节点*作为参数,返回值和局部变量。我更改了代码并验证了您的原始函数是正确的。

技巧一:只有当引用指针时才能使用箭头表示法StructPtr->variable,否则必须使用点号法Struct.variable
*技巧2:指针只能设置为NULL。也就是说,可以将Node node设置为NULL,但不能将“Node node”**设置为NULL

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

typedef struct Node{
    int data;
    struct Node* next;
}Node;

Node* delHead(Node* head);
Node* delTail(Node* head);
Node* delAt(Node* head, int position);
void displayNode(Node* head, char* type);

int main()
{
    Node* head = malloc(sizeof(Node));
    Node* tmpNext = head;
    
    for(int i = 9; i >= 0; i--){
        tmpNext->data = i;
        tmpNext->next = malloc(sizeof(Node));
        tmpNext = tmpNext->next;
    }
    tmpNext->next = NULL;
    
    displayNode(head, "Original");
    
    tmpNext = head;
    tmpNext = delHead(tmpNext);
    displayNode(tmpNext, "Delete Head");
    
    tmpNext = head;
    tmpNext = delTail(tmpNext);
    displayNode(tmpNext, "Delete Tail");
    
    tmpNext = head;
    tmpNext = delAt(tmpNext, 3);
    displayNode(tmpNext, "Delete at 3");

    return 0;
}

Node* delHead(Node* head){
    if(head==NULL){
        printf("There is nothing to delete!");
    }
    else{
        head=head->next;
    }
    return head;
}

Node* delTail(Node* head){
    if(head==NULL||head->next==NULL){
        return delHead(head);
    }else{
        Node* p = head;
        Node* prev = NULL;
        while(p->next !=NULL){
            prev=p;
            p=p->next;
        }
        prev->next= NULL;
        free(p);
        return head;
    }
}

Node* delAt(Node* head, int position){
    if(position == 0 || head == NULL || head->next == NULL){
        head = delHead(head); 
    }else{
        int k = 0;
        Node* p = head;
        Node* prev = NULL;
        while(p != NULL && k != position){
            prev = p;
            p=p->next;
            k++;
        }
        if(k != position){
            head = delTail(head);
        }else{
            prev->next=p->next;
            free(p);
        }
    }
    return head;
}

void displayNode(Node* head, char* type){
    printf("\n%s:\n", type);
    while(head->next != NULL){
        printf("[%d]", head->data);
        head = head->next;
        if(head->next != NULL)
            printf(" -> ");
    }
    printf("\n");
}

输出:

Original:
[9] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0]

Delete Head:
[8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0]

Delete Tail:
[9] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1]

Delete at 3:
[9] -> [8] -> [7] -> [5] -> [4] -> [3] -> [2] -> [1]

相关问题