我不确定我在这里做错了什么,我只是尝试将一个课程节点添加到具有指定值的链接列表,但是当我尝试打印链表时,它一直说它是空的,我不是确定有什么问题。
我现在一切都在工作,除了当我通过列表的大约1/3时,列表段错误,我不知道为什么。请帮助!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX 50
typedef struct courses{
char *abbr;
char *name;
int credits;
char *prof;
struct courses *next;
}courses;
int isAbbr(char *string);
int isName(char *string);
int isCredit(char *string);
int isProf(char *string);
int isPre(char *string);
courses *readfile(FILE *);
courses *create_course(char *abbr, char *prof, int credits, char *fulldesc);
courses *create_list(courses *, courses *);
void Print(courses *);
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Inadequate amount of arguments.\n");
return 0;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("File cannot be opened.\n");
return 0;
}
courses* head = NULL;
head = readfile(fp);
Print(head);
printf("=== task1: construct a linear linked list from the catalog ===\n\n=== task2: user input and producing output ===\n\n");
int choice = 0;
while (choice != 3)
{
printf("\nSelect your option below:\n1-Register for a Course\n2-See my total\n3-Exit\nChoice: ");
scanf("%d",&choice);
}
printf("\n");
return 0;
}
courses *readfile(FILE *fp)
{
courses *head, *entry;
head = entry = NULL;
char *abbr = malloc(MAX);
char *name = malloc(MAX);
char *prof = malloc(MAX);
char *desc = malloc(MAX);
char *fulldesc = malloc(MAX);
char *fullName = malloc(MAX);
int credit;
int credflag = 0;
int nameFlag = 0;
int profFlag = 0;
int preFlag = 0;
int credits = 0;
int descflag = 0;
char line[MAX];
while (fgets(line, MAX - 1, fp) != NULL)
{
if (line[strlen(line) - 1] == '\n')
{
line[strlen(line) - 1] = '\0';
}
char* token = strtok(line," ,\t");
while (token != NULL)
{
if (isAbbr(token) == 1)
{
abbr = malloc(sizeof(char));;
strcpy(abbr, token);
credflag = 1;
preFlag = 0;
profFlag = 1;
}
if (isName(token) == 1)
{
if (descflag == 1 && isCredit(token) != 1)
{
fulldesc = strcat(desc, token);
}
else
{
desc = malloc(sizeof(char));
strcpy(desc,token);
descflag = 1;
}
}
else
{
descflag = 0;
}
if (isCredit(token) == 1 && credflag == 1)
{
credits = atoi(token);
credflag = 0;
descflag = 0;
}
if (isProf(token)== 1 && profFlag == 1)
{
if(nameFlag == 1 && name[0] != 'T' && token[0] != 'O')
{
prof = strcat(name, token);
entry = create_course(abbr,prof,credits, fulldesc);
head = create_list(head,entry);
profFlag = 0;
nameFlag = 0;
}
else
{
name = malloc(sizeof(char));
strcpy(name, token);
nameFlag = 1;
}
}
else
{
nameFlag = 0;
}
token = strtok(NULL," ,\t");
}
}
}
courses *create_course(char *abbr, char *prof, int credits, char *fulldesc)
{
courses *entry = (courses*)malloc(sizeof(courses));
//printf("%30s %50s %30s %30d\n",abbr,fulldesc,prof,credits);
entry->abbr=(char*)malloc(sizeof(char));
strcpy(entry->abbr, abbr);
entry->prof=(char*)malloc(sizeof(char));
strcpy(entry->prof, prof);
entry->name=(char*)malloc(sizeof(char));
strcpy(entry->name, fulldesc);
entry->credits = credits;
entry->next = NULL;
return entry;
}
courses *create_list(courses *head, courses *entry)
{
if (head == NULL)
{
return entry;
}
courses* curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = entry;
return head;
}
void Print(courses *head)
{
if(head == NULL)
{
printf("Course list is empty\n\n");
return;
}
printf("\n");
while(head)
{
printf("%20s %20d\n",head->abbr,head->credits);
}
printf("\n\n");
}
int isName(char *string)
{
int length = strlen(string);
int i;
if (isupper(string[0]))
{
for (i=1; i<length; i++)
{
if (isupper(string[i]) || string[i] == ':')
{
continue;
}
else
{
return 0;
}
}
return 1;
}
return 0;
}
int isPre(char *string)
{
int length = strlen(string);
int i;
if (length == 14)
{
printf("\t%s\n",string);
return 1;
}
return 0;
}
int isProf(char *string)
{
int length = strlen(string);
int i;
if (isupper(string[0]))
{
for (i=1; i<length; i++)
{
if (islower(string[i]))
{
continue;
}
else
{
return 0;
}
}
return 1;
}
return 0;
}
int isCredit(char *string)
{
int n;
int nfields = sscanf(string, "%d", &n);
if (nfields == 1)
{
return 1;
}
return 0;
}
int isAbbr(char *string)
{
int length = strlen(string);
if (length == 8 && string[4] == '-')
{
//printf(" %s\n",string);
return 1;
}
return 0;
}
答案 0 :(得分:0)
学习使用像GDB这样的调试器。找到这样的bug变得微不足道。由于这是一个链表,我认为这是你课程的第一个作业。来到StackOverflow并等待您遇到的每一个错误的答案都不起作用。
每个程序员都需要知道如何使用调试器。没有人,你将无法通过学校。