将新数据插入链表之后,无法从新链表中删除任何数据

时间:2019-09-10 17:55:19

标签: linked-list

我正在创建linked-list并将数据列表显示为已排序格式。一切正常。当我不向其中插入新数据时,删除部分也可以工作。我的意思是,当我跳过insert部分时。但是,当我还要使用insert部分时,那就说-(data) Not Found to delete!。我不明白为什么会这样?在哪里出问题了,有人会帮忙找出来吗?

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

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

node *createLinkedList(int n);
void displayList(node *head);
void sortedList(node *head);
void search(node *head, int data);
void insert(node *head, int data);
void deleteNode(node *head, int data);
int main(){
    int n = 0;
    int sr = 0;
    node *head = NULL;
    printf("\nHow many nodes: ");
    scanf("%d", &n);
    head = createLinkedList(n);
    sortedList(head);
    displayList(head);

    printf("\nEnter the value you want to delete: ");
    scanf("%d", &n);
    deleteNode(head, n);
    displayList(head);

    printf("\nEnter the new value to insert: ");
    scanf("%d", &n);
    insert(head, n);
    sortedList(head);
    displayList(head);

    printf("\nEnter the value to search: ");
    scanf("%d", &n);
    search(head, n);

    return 0;
}

node *createLinkedList(int n){
    int i = 0;
    node *head = NULL;
    node *temp = NULL;
    node *p = NULL;

    for(i = 0; i < n; i++){
        temp = (node*)malloc(sizeof(node));
        printf("\nEnter the data for node number %d: ", i+1);
        scanf("%d", &(temp->data));

        //if list is currently empty
        if(head == NULL){
            head = temp;
        }else{
            p = head;
            while(p->next != NULL){
                p = p->next;
            }
            p->next = temp;
        }
    }

    return head;
}

void displayList(node *head){
    node *p = head;

    while(p != NULL){
         printf("\t%d", p->data);
         p = p->next;
    }
}

void sortedList(node *head){
    int temp;
    node *i, *j;
    printf("\nThe sorted data list is: \n");
    for(i = head; i->next != NULL; i = i->next){
        for(j = i->next; j != NULL; j = j->next){
            if(i->data > j->data){
                temp = i->data;
                i->data = j->data;
                j->data = temp;
            }
         }
     }
}

void search(node *head, int data){
     int count = 0;
     while(head->next != NULL){
         if(head->next->data == data){
             count++;
         }
         head = head->next;
    }
    printf("\nTotal %d results found for %d\n", count, data);
}

void insert(node *head, int data){
    while(head->next != NULL){
        head = head->next;
    }
    head->next = (node *)malloc(sizeof(node));
    head->next->data = data;
    head->next->next = NULL;

    printf("\nAfter inserting %d", data);
}
void deleteNode(node *head, int data){
    while(head->next != NULL){
        if(head->next->data == data){
            head->next = head->next->next;
            return 0;
        }
        if(head->next->data != data){
            printf("\n%d Not Found to delete! \n", data);
            return 0;
        }
        head = head->next;
    }
    printf("\nAfter delete %d", data);
}

2 个答案:

答案 0 :(得分:2)

在deleteNode函数中,您正在检查第二个元素,如果不是需要删除的数据,则打印出“找不到要删除的”并退出该函数。因此,您要删除的数据不会被删除。

答案 1 :(得分:1)

在* createLinkedList();中函数,您无需为temp的下一个指针(这是一个野生指针)分配值,而只是将数据读入temp-> data

相关问题