2016-04-05 3 views
0

私はリンクされたリストに学校のコースを追加するプログラムを持っています。コースにリストを追加して印刷することができます。ポインタリンクリストからのノードの削除

ただし、リンクされたリスト(remove_course関数)からコースを削除することはできません。何らかの理由で、私がdelete_from_listに渡すCourseListはNULLに等しいです。

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

#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

#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

#ifndef BST_H 
#define BST_H 

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

} *BST; 

#endif 
+0

「BST」とは何ですか? –

+0

1つの問題: 'strcpy(c-> name、&course_name);' - > 'strcpy(c-> name、&course_name);'。 –

+0

@MichaelWalzバイナリ検索ツリー..私はまだ追加されていません。しかし、ヘッダファイルの中にあります:) – BriannaXD

答えて

2

あなたはmain関数内で変数masterを初期化することはありません。つまり、不定値の値を持ち、未初期化を使用すると、の未定義の動作になります。あなたがNULLポインタに、それを初期化する必要があり

CourseList master = NULL; 

あなたは変数を初期化していない時に、今何が起こるには、あなたのinsert_at_front機能に

newNode->next = *cl; 

を持っているということです*clmasterからmainまでです。初期化されていないので、一見ランダムな値を持ちますそれはおそらくNULLではない可能性が高いため、実際には存在しないときに新しいノードがリスト内に次のノードを持つように見えます。

これにより、delete_from_listのループがリストの実際の最後のノードを越えて続き、割り当てられたノードまたはデータに属していないデータが間接参照されます。