简体   繁体   English

从指针链接列表中删除节点

[英]Deleting a node from a pointer linked list

I have a program that adds school courses to a linked list. 我有一个将学校课程添加到链接列表的程序。 I'm able to add the courses to the list and print them out. 我可以将课程添加到列表中并打印出来。

However I cant delete a course from the linked list (remove_course function). 但是,我无法从链接列表中删除课程(remove_course函数)。 For some reason the CourseList I pass into delete_from_list is equal to NULL. 由于某种原因,我传递给delete_from_list的CourseList等于NULL。

main.c main.c

#define _CRT_SECURE_NO_WARNINGS

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "bst.h"
#include "courseList.h"

/* Add course to the given linked list */
void add_course(CourseList *self)
{
    char course_name[100];
    Course *c;

    printf("enter a course name: \n");
    scanf("%s", course_name);

    c = (Course*)malloc(sizeof(struct course));

    c->name = (char*)malloc(strlen(course_name) + 1);
    strcpy(c->name, &course_name);
    c->students = NULL;

    insert_at_front(self, *c);
}

/* Remove a given course from the given linked list */
void remove_course(CourseList *self)
{
    char course_name[100];
    printf("What course would you like to remove?\n");
    scanf("%s", course_name);
    delete_from_list(self, course_name);
}

/* Print the courses in the given linked list */
void print_courses(CourseList *self)
{
    CourseList current;
    current = *self;
    printf("Your list is: \n");
    while (current != NULL) 
    {   
        printf("%s\n", current->data.name); 
        current = current->next; 
    }
    printf("\n");    
}      

int main(void)
{       
    CourseList master;    
    int choice;

    printf("Welcome to student enrolment\n");

    while (true)
    { 
        printf("Pick your option\n");
        scanf("%d", &choice);
        switch (choice)
        {
        case 0:
            exit(1);
            break;
        case 1:
            add_course(&master);
            printf("%s was add to the list!\n", master->data.name);
            break;
        case 2:
            print_courses(&master);
            break;
        case 3:
            remove_course(&master);
            break;
        }    
    }    
}

CourseList.c CourseList.c

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "bst.h"
#include "courseList.h"


void insert_at_front(CourseList *cl, Course c)
{
    CourseList newNode;

    newNode = (CourseList)malloc(sizeof(struct courseNode));
    newNode->data = c;
    newNode->next = *cl;

    *cl = newNode;    
}    

void delete_from_list(CourseList *self, char *course_name)
{
    CourseList current = *self;
    CourseList prev = NULL;
    CourseList to_free = NULL;

    while (current != NULL)
    {
        if (strcmp(current->data.name, course_name) == 0) //Error here current has nothing in it
        {
            if (prev == NULL)
            {
                *self = current->next;
                free(current);
                current = *self;
            }
            else
            {
                prev->next = current->next;
                free(current);
                current = prev->next;
            }
        }
        else
        {
            prev = current;
            current = current->next;
        }
    }
}

CourseList.h CourseList.h

#include "bst.h"

#ifndef COURSELIST_H
#define COURSELIST_H

struct course;
typedef struct course Course;

struct courseNode;
typedef struct courseNode *CourseList;

typedef struct course
{
    char *name;
    BST students;

} Course;

typedef struct courseNode
{
    Course data;
    struct courseNode *next;

} *CourseList;

void insert_at_front(CourseList *cl, Course c);
void delete_from_list(CourseList *self, char *course_name);

#endif

bst.h bst.h

#ifndef BST_H
#define BST_H

typedef struct bstNode
{
    long student_id;
    struct bstNode*left;
    struct bstNode*right;

} *BST;

#endif

You never initialize the variable master in the main function. 您永远不会在main函数中初始化变量master That means it will have an indeterminate value, and using it uninitialized will lead to undefined behavior . 这意味着它将具有不确定的值,并且未初始化使用它将导致未定义的行为

You need to initialize it to a NULL pointer: 您需要将其初始化为NULL指针:

CourseList master = NULL;

What happens now when you haven't initialize the variable, is that in your insert_at_front function have 现在,当您尚未初始化变量时会发生什么,就是在您的insert_at_front函数中有

newNode->next = *cl;

Since *cl is master from main , and it's uninitialized it will have a seemingly random value, one that is most likely not NULL , and therefore it will seem like the new node will have a next node in the list, when in fact there isn't any. 由于*clmastermain ,这是未初始化它会有一个看似随机值,一个是最有可能不是NULL的,因此它会看起来像新的节点将在列表中的下一个节点,而事实上这ISN没有。

This causes the loop in delete_from_list to continue past the actual last node in the list, and you will dereference data that isn't belong to any node or data you have allocated. 这将导致delete_from_list的循环继续经过列表中实际的最后一个节点,并且您将取消引用不属于任何节点的数据或已分配的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM