【发布时间】:2022-01-23 12:20:36
【问题描述】:
我必须创建一个结构书的链接列表,我的问题是当我创建一个新节点并在其中输入新书的属性时,我以前节点中的所有以前的书都将它们的名称更改为最后一个我进入了,尽管出版年份仍然是原始的。有人可以解释这是如何工作的,以及我必须做些什么才能使其正常工作?
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define N 255
struct Book {
char *title;
int year;
};
struct Node {
struct Book book;
struct Node *next;
};
struct Book createBook(char *title, int year) {
struct Book book = {title, year};
return book;
}
void print(struct Node *head) {
struct Node *curr_node = head;
while (curr_node != NULL) {
printf("%s(%d)\n", curr_node->book.title, curr_node->book.year);
curr_node = curr_node->next;
}
}
void add(struct Node **head, struct Book book) {
struct Node *new_node;
struct Book *new_book;
new_node = (struct Node*) malloc(sizeof( struct Node ));
new_book = &new_node->book;
new_book->title = book.title;
new_book->year = book.year;
new_node->next = (*head);
*head = new_node;
}
void pop(struct Node **head) {
struct Node *next_node = NULL;
if (*head == NULL) {
printf("List is empty!");
return;
}
next_node = (*head)->next;
free(*head);
*head = next_node;
}
void reverse(struct Node **head) {
struct Node *prev_node = NULL;
struct Node *curr_node = *head;
struct Node *next_node = NULL;
while (curr_node != NULL) {
next_node = curr_node->next;
curr_node->next = prev_node;
prev_node = curr_node;
curr_node = next_node;
}
*head = prev_node;
}
void clear(struct Node **head) {
struct Node *curr_node = *head;
struct Node *temp_node = NULL;
while (curr_node != NULL) {
temp_node = curr_node->next;
free(curr_node);
curr_node = temp_node;
}
*head = NULL;
}
int main() {
struct Node *head = NULL;
short process = 1; // 1 for True 0 for False
int option = 0;
char title[N];
int year = 0;
while (process) {
printf("List of actions:\n");
printf("1:\tAdd element\n");
printf("2:\tRemove element\n");
printf("3:\tReverse list\n");
printf("4:\tPrint list\n");
printf("5:\tClear list\n");
printf("0:\tExit\n");
scanf("%d", &option);
switch (option) {
case 1:
printf("Enter book\'s title: ");
scanf("%s", title);
printf("Enter book\'s year of publishing: ");
scanf("%d", &year);
add(&head, createBook(title, year));
printf("\n");
break;
case 2:
pop(&head);
break;
case 3:
reverse(&head);
break;
case 4:
print(head);
break;
case 5:
clear(&head);
break;
case 0:
process = 0;
break;
default:
printf("\n");
printf("**************************\n");
printf("Enter numbers from 1 to 5!\n");
printf("**************************\n");
printf("\n");
continue;
}
}
return 0;
}
【问题讨论】:
-
下次C题不要加C++标签。
标签: c pointers struct linked-list singly-linked-list