删除链表中的节点时出错

时间:2019-01-20 14:09:45

标签: c linked-list

#include<stdio.h>
#include<stdlib.h>
    struct Node {
        int data;
        struct Node *next;

    };
void printList(struct Node *n)
{
    while (n!=NULL) {

    printf(" %d|\t", n->data);
    n = n->next;
    }
}


int main () {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;
    struct Node* fourth = NULL;
    struct Node* fifth = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
    fourth = (struct Node*)malloc(sizeof(struct Node));
    fifth = (struct Node*)malloc(sizeof(struct Node));

    head-> data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = fourth; 

    fourth->data = 4;
    fourth->next = fifth;

    fifth->data = 5;
    fifth->next = NULL;

    printList(head);
    delete();
    printList(head);
}
void delete(){
    struct Node* temp;
    int loc;
        printf("Enter the location what you want to delete : ");
        scanf("%d",&loc);
        if(loc>length()){
            printf("Invailid Location");
        }
            else if (loc==1) {

            temp = root;
            root= temp -> next;
            temp -> next = NULL;
            free (temp);
        }
}
  

根变量和删除功能存在错误。它说根变量没有减速。之前的“删除”隐式声明在这里 ?是否需要先声明根变量?如果是,想去哪里?请让我知道该程序有什么错误。

0 个答案:

没有答案