gpt4 book ai didi

c - c中的订购号和字符串

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:00 25 4
gpt4 key购买 nike

我想创建一个二叉搜索树,它包含如下数字和字符串:

  • 48 珍妮弗
  • 35 岁的大卫
  • 27 梅西
  • 3 戈麦斯
  • 85 海伦娜等

现在,我需要按顺序列出它们。部分代码在这里:

void OrderedById(struct CustomerBSTNode *temp){
if(temp != NULL){
OrderedById(temp->left);
printf("%d ", temp->data.ID);
printf("%s\n",temp->data.name);
OrderedById(temp->right);
}
}

所有代码在这里:

struct Customer{
char *name;
int ID;
struct BasketLLNode* baskets; };

struct Product{
int ID;
char *name;
int price; };
struct CustomerLLNode{
struct Customer data;
struct CustomerLLNode *next;}*HeadCustomer,*TempCustomer;}

struct CustomerBSTNode{
struct Customer data;
struct CustomerBSTNode *left;
struct CustomerBSTNode *right;};
struct CustomerBSTNode *get_node(){
struct CustomerBSTNode *temp;
temp = (struct CustomerBSTNode *)malloc(sizeof(struct CustomerBSTNode));
temp->left = NULL;
temp->right = NULL;
return temp; }

void insert(struct CustomerBSTNode *root, struct CustomerBSTNode *newNode){
if(newNode->data.ID < root->data.ID) {
if(root->left == NULL)
root->left = newNode;
else
insert(root->left,newNode); }

if(newNode->data.ID > root->data.ID){
if(root->right == NULL)
root->right = newNode;
else
insert(root->right,newNode); }}
void OrderedById(struct CustomerBSTNode *temp){
if(temp != NULL) {
OrderedById(temp->left);
printf("%d ", temp->data.ID);
printf("%s\n",temp->data.name);
OrderedById(temp->right); }}
int main(){
char ans = 'N';
int key;
struct CustomerBSTNode *new_node, *root, *tmp, *parent,*get_node();
root = NULL;
char CustomerName[25];
while(ans != 'a'){
printf("Enter Elements: ");
new_node = get_node();
scanf("%d",&new_node->data.ID);
scanf("%s",new_node->data.name);

if(root == NULL)
root = new_node;
else
insert(root,new_node);
ans = getch(); }

OrderedById(root);

return 0;}

我希望这段代码的输出是:

  • 3 戈麦斯
  • 27 梅西
  • 35 岁的大卫
  • 48 珍妮弗
  • 85 海伦娜

但是代码给出:

  • 3 海伦娜
  • 27 海伦娜
  • 35 海伦娜
  • 48 海伦娜
  • 85 海伦娜

我的错误是什么?它按 id 排序,但名称在所有行中都相同。

最佳答案

您无法通过 C 语言猜出解决方案。您立即发布的代码 段错误 因为您未能为从 stdin 读取的名称提供任何存储。您显然打算通过 struct Customer name 引用名称。 name 是指向任何地方的未初始化指针。

此外,在调用 new_node = get_node (); 之后,您没有通过 struct CustomerBSTNode, Customer data 提供对任何 struct Customer 的引用> 成员,您错误地将其作为 struct Customer 类型的静态成员而不是指向 struct Customer* 的指针。例如你应该:

struct CustomerBSTNode {
struct Customer *data;
struct CustomerBSTNode *left;
struct CustomerBSTNode *right;
};

(这将需要更改您在其余代码中引用 Customer 成员的方式)

您没有使用 malloc 验证任何内存分配,也没有检查 scanf 的返回值。你怎么知道你是否有任何内存可以存储你的输入,你怎么知道你有任何输入要存储在第一位?始终,始终验证所有分配和所有输入。例如

struct CustomerBSTNode *get_node ()
{
struct CustomerBSTNode *temp;
if (!(temp = malloc (sizeof *temp))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}

temp->left = NULL;
temp->right = NULL;
return temp;
}

    while (printf ("Enter Elements (age, name): ") &&
scanf (" %d %24[^\n]%*c", &key, name) == 2) {

您的 main 函数充满了未使用的变量,几乎不可能说出您是如何解决问题的。纠正眼前的问题并重新处理 main,您可以得到类似于以下的解决方案:

int main (void) {

int key;
char name[MAXNM] = "";
struct CustomerBSTNode *new_node, *root = NULL;

while (printf ("Enter Elements (age, name): ") &&
scanf (" %d %24[^\n]%*c", &key, name) == 2) {

new_node = get_node ();

struct Customer *cust;
if (!(cust = malloc (sizeof *cust))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}

cust->ID = key;
cust->name = strdup (name);
new_node->data = cust;

if (root == NULL) root = new_node;
else insert (root, new_node);
}

printf ("\n\nElements OrderedById\n\n");
OrderedById (root);

return 0;
}

将所有部分放在一起,您可以执行以下操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXNM 25

struct Customer {
char *name;
int ID;
struct BasketLLNode *baskets;
};

struct Product {
int ID;
char *name;
int price;
};

struct CustomerLLNode {
struct Customer *data;
struct CustomerLLNode *next;
} *HeadCustomer, *TempCustomer;

struct CustomerBSTNode {
struct Customer *data;
struct CustomerBSTNode *left;
struct CustomerBSTNode *right;
};

struct CustomerBSTNode *get_node ()
{
struct CustomerBSTNode *temp;
if (!(temp = malloc (sizeof *temp))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}

temp->left = NULL;
temp->right = NULL;
return temp;
}

void insert (struct CustomerBSTNode *root, struct CustomerBSTNode *newNode)
{
if (newNode->data->ID < root->data->ID) {
if (root->left == NULL)
root->left = newNode;
else
insert (root->left, newNode);
}

if (newNode->data->ID > root->data->ID) {
if (root->right == NULL)
root->right = newNode;
else
insert (root->right, newNode);
}
}

void OrderedById (struct CustomerBSTNode *temp)
{
if (temp != NULL) {
OrderedById (temp->left);
printf (" %2d %s\n", temp->data->ID, temp->data->name);
OrderedById (temp->right);
}
}

int main (void) {

int key;
char name[MAXNM] = "";
struct CustomerBSTNode *new_node, *root = NULL;

while (printf ("Enter Elements (age, name): ") &&
scanf (" %d %24[^\n]%*c", &key, name) == 2) {

new_node = get_node ();

struct Customer *cust;
if (!(cust = malloc (sizeof *cust))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}

cust->ID = key;
cust->name = strdup (name);
new_node->data = cust;

if (root == NULL) root = new_node;
else insert (root, new_node);
}

printf ("\n\nElements OrderedById\n\n");
OrderedById (root);

return 0;
}

示例使用/输出

$ ./bin/bstordered
Enter Elements (age, name): 48 Jenifer
Enter Elements (age, name): 35 David
Enter Elements (age, name): 25 Messi
Enter Elements (age, name): 3 Gomez
Enter Elements (age, name): 85 Helena
Enter Elements (age, name):

Elements OrderedById

3 Gomez
25 Messi
35 David
48 Jenifer
85 Helena

花时间浏览代码并理解为什么要进行这些更改。您必须非常仔细地考虑您的数据处理,并确保构建数据结构的每个步骤、每个级别仅采用完全分配的成员、类型等。如果您有进一步的问题,请告诉我。

(注意:您有责任释放您分配的内存。)

关于c - c中的订购号和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36608114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com