所以我试图使一个c程序的菜单,从链表中添加/减去类.为什么这行不通呢?



出现问题"current = head;", "错误:无法将'course*'转换为'main()::node*';

示例运行应该是这样的:你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出3.你目前没有参加任何课程。

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出1您想添加什么课程名称?Intro_to_C你想加哪门课?COP3223C添加!

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出1您想添加什么课程名称?Computer_Science_1你想加哪门课?COP3502C添加!

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出3.课程安排:
  5. COP3223C - Intro_to_C
  6. COP3502C -计算机科学1

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出1您想添加什么课程名称?Concepts_in_Computer_Science你想加哪门课?COP2500C添加!

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出3.课程安排:
  5. COP2500C - Concepts_in_Computer_Science
  6. COP3223C - Intro_to_C
  7. COP3502C -计算机科学1

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出2你想放弃哪个课程代码?COP2100C这门课不在你的课程表上。

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出2你想放弃哪个课程代码?COP3223C课程已被删除。

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出3.课程安排:
  5. COP2500C - Concepts_in_Computer_Science
  6. COP3502C -计算机科学1

你想学什么课程?

  1. 添加课程
  2. 删除课程
  3. 打印时间表
  4. 退出4

完成了!

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//linked list of structs 

struct course {
char name[31], number[11];
struct course *next;
};
//prints the list

void printList(struct course *h) {
struct course *temp = h;
int num = 1;
while (temp != NULL) {
printf("%d. %s - %sn", num, temp->number, temp->name);
temp = temp->next;
}
}
int contains(struct course *h, char code[11]) {
struct course *temp = h;
while (temp != NULL) {
if (strcmp(temp->number, code)) {
return 1;
}
temp = temp->next;
}
return 0;
}
int Menu() {
printf("What courses would you like to do?n1. Add Coursen2. Drop Coursen3. Print Schedulen4. Exitn");
int value;
scanf("%d", &value);
return value;
}
int main() {
struct course *head = NULL;
struct node *current = NULL;
int option = -1;
while (option != 4) {
option = Menu();
if (option == 1) {
//Add new course
struct course *new_course = (struct course *) malloc(sizeof(struct course));
printf("What course name would you like to add?n");
scanf("%s", new_course->name);
printf("What course number would ypou like to add?n");
scanf("%s", new_course->number);
new_course->next = NULL;
if (head == NULL) {
head = new_course;
} else if (contains(head, new_course->number) == 1) {
printf("Course has already been added.n");
} else {
int flag = 0;
if (strcmp(head->number, new_course->number) > 0) {
new_course->next = head;
head = new_course;
flag = 1;
}
current = head;
while (current->next != NULL && flag == 0) {
if (strcmp(current->next->number, new_course->number) > 0) {
new_course->next = current->next;
current->next = new_course;
flag = 1;
break;
}
current = current->next;
}
// inserts at the end;
if (flag == 0) {
current->next = new_course;
}
}
} else if (option == 2) {
//Remove course
char remove[11];
int flag = 0;
printf("What element would you like to delete?n");
scanf("%s", remove);
while (head != NULL && strcmp(head->number, remove) == 0) {
head = head->next;
flag = 1;
}
current = head;
while (current != NULL && current->next != NULL) {
if (strcmp(current->next->number, remove) == 0) {
current->next = current->next->next;
flag = 1;
} else {
current = current->next;
}
}
if (flag == 0) {
printf("This course is not on the schedule.n");
} else {
printf("The course has been deleted.");
}
} else if (option == 3) {
//Print out schedule
printList(head);
}
return 0;
}

变量headstruct course *类型,变量currentstruct node *类型。因为它们是不同的类型,所以不能将其中一个赋值给另一个。

您可能希望将指针current改为struct course *类型。所以你应该改变

这一行
struct node *current = NULL;

:

struct course *current = NULL;

最新更新