出現問題 "current = head;", "error: cannot convert 'course*' to 'main()::node*' in assignment"
示例運行應如下所示:您想學習哪些課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 3 您目前沒有參加任何課程。
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 1 您想添加什么課程名稱?Intro_to_C 您想添加什么課程編號?COP3223C 已添加!
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 1 您想添加什么課程名稱?Computer_Science_1 您想添加什么課程編號?COP3502C 已添加!
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 3 課程安排:
- COP3223C - Intro_to_C
- COP3502C - 計算機科學_1
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 1 您想添加什么課程名稱?Concepts_in_Computer_Science 您想添加什么課程編號?COP2500C 已添加!
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 3 課程安排:
- COP2500C - Concepts_in_Computer_Science
- COP3223C - Intro_to_C
- COP3502C - 計算機科學_1
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 2 你想放棄什么課程代碼?COP2100C 本課程不在您的日程中。
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 2 你想放棄什么課程代碼?COP3223C 課程已被洗掉。
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 出口 3 課程安排:
- COP2500C - Concepts_in_Computer_Science
- COP3502C - 計算機科學_1
你想修什么課程?
- 添加課程
- 下降課程
- 列印時間表
- 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 - %s\n", 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 Course\n2. Drop Course\n3. Print Schedule\n4. Exit\n");
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;
}
uj5u.com熱心網友回復:
變數head是型別struct course *,而變數current是型別struct node *。由于它們屬于不同型別,因此您不能將一個分配給另一個。
您可能希望改為使用current型別指標struct course *。所以你應該換行
struct node *current = NULL;
到:
struct course *current = NULL;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465483.html
上一篇:C語言中的斐波那契演算法
