- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想创建一个二叉搜索树,它包含如下数字和字符串:
现在,我需要按顺序列出它们。部分代码在这里:
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;}
我希望这段代码的输出是:
但是代码给出:
我的错误是什么?它按 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/
我正在尝试以一种可以根据需要循环输出和输出 header 的方式编写 SQL Server 2008 查询。我已经多次以错误的方式完成这些工作,让 ColdFusion 在页面中完成艰苦的工作,但需要
为什么通用寄存器按原样排序(eax、ecx、edx、ebx)?例如,对于“inc”指令,操作码是: inc eax - 40 inc ecx - 41 inc edx - 42 inc ebx - 4
晚上好。 我需要组织一个HashMap,它是这样的: private HashMap>taskOrdered = new HashMap>(); 每个“任务”都是一个具有 3 个属性的类: 人员姓名;
这个问题在这里已经有了答案: C# - sorting by a property (3 个答案) how to sort a collection by datetime in c# (4 个答案
我有以下存储记录和错误的 XML 文档(如果需要可以重新设计)。 11/03/2010 14:12:41 1 11/03/2
关于这个主题有很多问题,仍然无法找到解决这个问题的方法。 我正在做的查询是: SELECT `b`.`ads_id` AS `ads_id`, `b`.`bod_bedrag`
我正在制作一个排名系统。但我想要的是将我得到的结果 ($kn) 从最高到最低排序。我该怎么做? include "includes/core.inc.php"; require "includes/c
我有一个这样的列表, M=[[75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 4, 82, 47, 65], [19, 1, 23, 75, 3
我有 5 个数学问题要解决,30 个人将尝试解决这些问题中的每一个。 我知道每个人解决某个问题的速度有多快: Person1 将能够在 5 秒内解决问题 A,在 7 秒内解决问题 B,在 20 秒内解
考虑 val animals = List("penguin","ferret","cat").toSeq val rdd = sc.makeRDD(animals, 1) 我想订购这个 RDD。我是
我有以下列表: 我的 list [[1]] [1] 11 [[2]] [1] 9 [[3]] [1] 10 我想对它进行排序。我试过了 sort(mylist) Error: mylist must
sort(v; alg::Algorithm=defalg(v), lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)
我有一个数据框,我想生成一个 geom_tile()从中绘制,但我希望图形的排序不是基于字母顺序而是基于此数据框中的变量。 structure(list(V1 = c("a", "y", "w", "
我列出 Facebook 好友是这样的: FB.api('/me/friends?fields=id,name,updated_time&date_format=U&',
我正在多个线程中的UDP上接收消息。每次接待后,我都会提出MessageReceived.OnNext(message)。 因为我使用多个线程,所以消息无序引发,这是一个问题。 如何通过消息计数器命令
我与两个实体有一对多关系: Order: int OrderId string OrderNumber ... OrderItem: int ItemId int sequence decimal Q
我正在尝试从用户将输入的数字(代码)中对结构进行排序,并且我正在使用冒泡排序。我希望程序打印按数字(代码)排序的所有数据,但它只对数字(代码)进行排序。有人可以帮我对数字(代码)中的其他元素进行排序吗
对于我的通用网格,我目前这样做是为了激活排序: Elements.OrderBy(column.SortExpression).AsQueryable(); 其中SortExpression类型为 F
给定两个 DOM 元素,比如 a 和 b,我们如何确定哪个在文档中排在第一位? 我正在对一组元素实现拖放操作。并且可以以任意顺序选择元素,但是在拖动它们时,需要以“正确”的顺序移动这些元素。 最佳答案
我正在将我们的管理系统从 PHP 迁移到 Ruby On Rails。旧系统的一部分使用 SQL 来构建客户列表,然后按最后一次联系的时间对他们进行排序: SELECT `cust
我是一名优秀的程序员,十分优秀!