Я изучаю связанный список в C, и ниже мой код.
struct movie {
char movie_name[30];
float score;
struct movie* next;
};
typedef struct movie* p_movie;
void print_all(struct movie* head);
void print_index(struct movie* head);
void search_and_index(struct movie* head);
void insert_index(struct movie* head);
void add_index(struct movie* head);
int main()
{
int input;
p_movie head = NULL;
p_movie new_node = malloc(sizeof(struct movie));
if (new_node == NULL)
{
exit(1);
}
strcpy(new_node->movie_name, "Avatar");
new_node->score = 9.5f;
new_node->next = NULL;
if (head == NULL)
head = new_node;
new_node = malloc(sizeof(struct movie));
if (new_node == NULL)
{
exit(1);
}
strcpy(new_node->movie_name, "Aladdin");
new_node->score = 8.0f;
new_node->next = NULL;
p_movie temp = head;
head = new_node;
new_node->next = temp;
new_node = malloc(sizeof(struct movie));
strcpy(new_node->movie_name, "Wonder woman");
new_node->score = 8.5f;
new_node->next = NULL;
while (true)
{
printf("1 - print_all \n \
2- print_index \n 3- search_and_remove \n 4-insert_index \n 5- add_index\n");
scanf("%d", &input);
getchar();
switch (input)
{
case 1:
print_all(head);
break;
case 2:
print_index(head);
break;
case 3:
search_and_index(head);
break;
case 4:
insert_index(head);
break;
case 5:
add_index(head);
break;
default:
break;
}
}
}
void print_all(struct movie* head)
{
printf("---------------------\n");
printf("Head address = %zd\n", (size_t)head);
struct movie* search = head;
while (search != NULL)
{
printf("%zd \"%s\" %f %zd\n", (size_t)search, search->movie_name,
search->score, (size_t)search->next);
search = search->next;
}
}
void search_and_index(struct movie* head)
{
p_movie search = head;
p_movie prev = NULL;
int count = 0;
char input[20];
printf("Input the name to remove \n");
scanf("%[^\n]s", input);
getchar();
while (search != NULL)
{
if (strcmp(search->movie_name, input) == 0) break;
prev = search;
search = search->next;
count++;
}
if (search == NULL)
{
printf("Wrong name \n");
exit(0);
}
if (prev == NULL)
head = search->next;
else
prev->next = search->next;
free(search);
}
void remove_index(struct movie* head)
{
p_movie search = head;
int input;
p_movie pre = NULL;
printf("input the index number\n");
scanf("%d", &input);
getchar();
for (int i = 0; i < input; i++)
{
pre = search;
search = search->next;
}
if (pre == search || search->next == NULL)
free(search);
else
{
pre->next = search->next;
free(search);
}
}
void insert_index(struct movie* head)
{
p_movie new_node = malloc(sizeof(struct movie));
int index;
char name[20];
p_movie search = head;
p_movie pre = NULL;
printf("Input index where you want to insert.\n");
scanf("%d", &index);
getchar();
printf("Input movie name: \n>>");
scanf("%[^\n]s", name);
getchar();
strcpy(new_node->movie_name, name);
printf("Input score of movie. \n >>");
scanf("%f", &(new_node->score));
getchar();
for (int i = 0; i < index; i++)
{
pre = search;
search = search->next;
}
if (search == head)
{
head = new_node;
new_node->next = search;
}
else
{
pre->next = new_node;
new_node->next = search;
}
}
В некоторых случаях он работает хорошо. Но в некоторых случаях он не запускается.
Например, я выбираю insert_index
. Если я ввожу 1,2,3 ..ect, он работает хорошо.
Но если я ввожу 0 и вводю имя фильма и счет, тогда выберите print_all
,
Это не добавляет мой вход. Просто распечатайте оригинальную структуру.
Как это исправить?
В чем проблема в моем коде?