在c中订购数字和字符串

时间:2016-04-13 19:43:43

标签: c binary-search-tree

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

  • 48 jennifer
  • 35 david
  • 27 messi
  • 3 gomez
  • 85 helena等。

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

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 gomez
  • 27 messi
  • 35 david
  • 48 jennifer
  • 85 helena

但是代码给出了:

  • 3 helena
  • 27 helena
  • 35 helena
  • 48 helena
  • 85 helena

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

1 个答案:

答案 0 :(得分:1)

您无法猜测自己在C中找到解决方案的方式。您发布的代码 segfaults ,因为您未能为从stdin读取的名称提供任何存储空间。您显然打算通过struct Customer name引用该名称。 name是一个未初始化的未初始化指针。

此外,在您致电new_node = get_node ();后,您不会通过struct Customer成员提及任何struct CustomerBSTNode, Customer data,而您错误地将其作为struct Customer类型的静态成员指向struct Customer*的指针。例如你应该:

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

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

您不会使用malloc验证任何内存分配,也不会检查scanf的回复。你怎么知道你是否有任何记忆你可以存储你输入的内容,你怎么知道你有任何输入存储在一开始?始终,始终验证所有分配和所有输入。 e.g。

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

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

注意:你负责free你分配的内存。)