如何将每个连续输入的数据点扫描到线性链表中的后续节点?
How do I scan each consecutively entered data point into subsequent nodes in a linear linked list?
我将此作为初级 C 编程课程的一部分进行,并且我正在尝试使用 print 语句进行调试。分配如下:
这是我的代码:
/* A program that takes in user input providing name, symbol, and atomic weight of first 10 elements from a table, then prints that data out.
By John
January 17 2022
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct element
{
char * name;
char * symbol;
float * weight;
} element;
typedef struct list
{
element data;
struct list* next;
} list;
list * create_list(char * name, char * symbol, float * weight_ptr)
{
list * head = malloc(sizeof(list));
head -> data.name = name;
head -> data.symbol = symbol;
head -> data.weight = weight_ptr;
head -> next = NULL;
return head;
}
list * add_to_front(char * name, char * symbol, float * weight_ptr, list * head)
{
list * h;
h = create_list(name, symbol, weight_ptr);
printf("\nold head name = %s\n\nnew head name = %s\n\n",
head -> data.name, h -> data.name);
h -> next = head;
return h;
}
list * info_to_list(char * name, char * symbol, float * weight_ptr)
{
/* char * name;
char * symbol;
float weight; */
list * head;
for (int counter = 0; counter < 10; counter++)
{
printf("Enter the name:");
scanf("%s", name);
printf("\n");
printf("Enter the symbol:");
scanf("%s", symbol);
printf("\n");
printf("Enter the weight:");
scanf("%f", weight_ptr);
if(counter == 0)
{
head = create_list(name, symbol, weight_ptr);
}
else
{
printf("inside info to list function, name in prior head is: %s",
head -> data.name);
head = add_to_front(name, symbol, weight_ptr, head);
printf("\ninside info to list function: name in new first head: %s\n",
head -> data.name);
}
}
return head;
}
void print_list(list * h)
{
while(h != NULL)
{
printf("\n\nName: %s\n", h -> data.name);
printf("Symbol: %s\n", h -> data.symbol);
printf("Weight: %.3f\n\n", *(h -> data.weight));
h = h -> next;
}
}
int main(void)
{
list * head;
//float weight = 0.0;
int name_size = 15; //no element name in first 10 reaches even 14 characters
int symbol_size = 3; // symbol size up to 2 letters plus 1 for sentinel
//int float_size = 30;
char * name = (char *)malloc(name_size * sizeof(char));
char * symbol = (char *)malloc(symbol_size * sizeof(char));
//float * weight_ptr = (float *) malloc(float_size * sizeof(float));
float * weight_ptr = (float *) malloc(sizeof(float));
head = info_to_list(name, symbol, weight_ptr);
print_list(head);
free(head);
free(name);
free(symbol);
free(weight_ptr);
return 0;
}
然而,当我编译和 运行 这段代码时,每当我输入一个元素名称,例如 Hydrogen,打印的语句会说旧“head”中的元素名称是氢,而在氢的新“头”。如何确保我输入的每一个连续的元素名都被扫描到链表中的每一个连续的节点中?
在 add_to_front 函数中,我的理解是我将旧的 head 作为参数传递,并创建一个新的 head,并将来自控制台的新输入信息插入到新的 head,因此我旧头部应该有旧元素信息,新头部应该有新元素信息,但事实并非如此。
谢谢!
评论员引导我找到正确答案。基本上我需要将指针分配给 head ->data.name、head ->data.symbol 和 head ->data.weight 的指针,而不是让它们指向缓冲区,这是我原来的错误。我是这样做的:
list * create_list(char * name, char * symbol, float * weight_ptr)
{
list * head = malloc(sizeof(list));
head -> data.name = strdup(name);
head -> data.symbol = strdup(symbol);
float local_weight = *weight_ptr;
head -> data.weight = &local_weight;
head -> next = NULL;
return head;
}
我将此作为初级 C 编程课程的一部分进行,并且我正在尝试使用 print 语句进行调试。分配如下:
这是我的代码:
/* A program that takes in user input providing name, symbol, and atomic weight of first 10 elements from a table, then prints that data out.
By John
January 17 2022
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct element
{
char * name;
char * symbol;
float * weight;
} element;
typedef struct list
{
element data;
struct list* next;
} list;
list * create_list(char * name, char * symbol, float * weight_ptr)
{
list * head = malloc(sizeof(list));
head -> data.name = name;
head -> data.symbol = symbol;
head -> data.weight = weight_ptr;
head -> next = NULL;
return head;
}
list * add_to_front(char * name, char * symbol, float * weight_ptr, list * head)
{
list * h;
h = create_list(name, symbol, weight_ptr);
printf("\nold head name = %s\n\nnew head name = %s\n\n",
head -> data.name, h -> data.name);
h -> next = head;
return h;
}
list * info_to_list(char * name, char * symbol, float * weight_ptr)
{
/* char * name;
char * symbol;
float weight; */
list * head;
for (int counter = 0; counter < 10; counter++)
{
printf("Enter the name:");
scanf("%s", name);
printf("\n");
printf("Enter the symbol:");
scanf("%s", symbol);
printf("\n");
printf("Enter the weight:");
scanf("%f", weight_ptr);
if(counter == 0)
{
head = create_list(name, symbol, weight_ptr);
}
else
{
printf("inside info to list function, name in prior head is: %s",
head -> data.name);
head = add_to_front(name, symbol, weight_ptr, head);
printf("\ninside info to list function: name in new first head: %s\n",
head -> data.name);
}
}
return head;
}
void print_list(list * h)
{
while(h != NULL)
{
printf("\n\nName: %s\n", h -> data.name);
printf("Symbol: %s\n", h -> data.symbol);
printf("Weight: %.3f\n\n", *(h -> data.weight));
h = h -> next;
}
}
int main(void)
{
list * head;
//float weight = 0.0;
int name_size = 15; //no element name in first 10 reaches even 14 characters
int symbol_size = 3; // symbol size up to 2 letters plus 1 for sentinel
//int float_size = 30;
char * name = (char *)malloc(name_size * sizeof(char));
char * symbol = (char *)malloc(symbol_size * sizeof(char));
//float * weight_ptr = (float *) malloc(float_size * sizeof(float));
float * weight_ptr = (float *) malloc(sizeof(float));
head = info_to_list(name, symbol, weight_ptr);
print_list(head);
free(head);
free(name);
free(symbol);
free(weight_ptr);
return 0;
}
然而,当我编译和 运行 这段代码时,每当我输入一个元素名称,例如 Hydrogen,打印的语句会说旧“head”中的元素名称是氢,而在氢的新“头”。如何确保我输入的每一个连续的元素名都被扫描到链表中的每一个连续的节点中?
在 add_to_front 函数中,我的理解是我将旧的 head 作为参数传递,并创建一个新的 head,并将来自控制台的新输入信息插入到新的 head,因此我旧头部应该有旧元素信息,新头部应该有新元素信息,但事实并非如此。
谢谢!
评论员引导我找到正确答案。基本上我需要将指针分配给 head ->data.name、head ->data.symbol 和 head ->data.weight 的指针,而不是让它们指向缓冲区,这是我原来的错误。我是这样做的:
list * create_list(char * name, char * symbol, float * weight_ptr)
{
list * head = malloc(sizeof(list));
head -> data.name = strdup(name);
head -> data.symbol = strdup(symbol);
float local_weight = *weight_ptr;
head -> data.weight = &local_weight;
head -> next = NULL;
return head;
}