I'm having problems creating a function that creates two separate linked lists in C.
我在创建一个在C中创建两个单独链接列表的函数时遇到问题。
In my program, the user enters an equation such as 7 + 9 * 8, character by character, the program then makes lists with them.
在我的程序中,用户逐字符地输入诸如7 + 9 * 8的等式,然后程序用它们制作列表。
The code is as follows: (Where place is where is should be in the list and data is the number/operator itself. Both come from another part of the program)
代码如下:(位置在哪里应该在列表中,数据是数字/运算符本身。两者都来自程序的另一部分)
struct trees {
char data;
int posicion;
struct trees *next;
struct trees *childI;
struct trees *childD;
};
struct trees *root;
struct trees *operador_uno;
struct trees *numero_uno;
char *numeros;
char *operadores;
int num, num_operadores;
void crearLista(int place, char data) {
int i;
struct trees *temp1 = (struct trees *)calloc(1, sizeof(struct trees));
temp1->data = data;
if(place == 0) {
if((data == '/') || (data == '*') || (data == '+') || (data == '-')){
temp1->next = operador_uno;
operador_uno = temp1;
}
else {
temp1->next = numero_uno;
numero_uno = temp1;
}
}
else {
struct trees *temp2;
if((data == '/') || (data == '*') || (data == '+') || (data == '-')) {
struct trees *temp2 = operador_uno;
}
else {
struct trees *temp2 = numero_uno;
}
for(i = 0; i < place - 1; i++) {
temp2 = temp2->next; // [CRASH]
}
temp1->next = temp2->next;
temp2->next = temp1; // [CRASH]
}
for(i = 0; i < place && place != 0; i++) {
struct trees *temp1 = operador_uno;
temp1 = temp1->next;
}
for(i = 0; i < place + 1; i++) {
struct trees *temp2 = numero_uno;
temp2 = temp2->next;
}
}
I identified through a tonne of printf statements that it will add the first number in the equation to the list successfully, how it does not add the first operator and with the second number the program crashes altogether.
我通过一系列printf语句确定它会将方程式中的第一个数字成功添加到列表中,它如何不添加第一个运算符,第二个数字表示程序完全崩溃。
The crashing problem appears to occur in the places I have written [CRASH], when I put temp2->next = temp1.
崩溃的问题似乎发生在我写的地方[CRASH],当我把temp2-> next = temp1。
Any help greatly appreciated!
任何帮助非常感谢!
1 个解决方案
#1
3
maybe not the only issue but:
也许不是唯一的问题,但是:
struct trees *temp2;
if((data == '/') || (data == '*') || (data == '+') || (data == '-')) {
struct trees *temp2 = operador_uno; // not the same "temp2" as above
}
else {
struct trees *temp2 = numero_uno; // not the same "temp2" as above
}
for(i = 0; i < place - 1; i++) {
temp2 = temp2->next; // [CRASH] because temp2 isn't initialized
}
struct trees *temp2 = operador_uno;
is shadowing temp2
declared in the outer scope. So the outer temp2
is never initialized, your initialization sets a value to a variable that goes out of scope.
struct trees * temp2 = operador_uno;是在外部范围内声明的影子temp2。因此,外部temp2永远不会被初始化,您的初始化会将值设置为超出范围的变量。
So remove struct trees *
so the same temp2
variable is used (and initialised), like this (I'd prefer a ternary expression, though):
所以删除结构树*所以使用(并初始化)相同的temp2变量,就像这样(我更喜欢三元表达式):
if((data == '/') || (data == '*') || (data == '+') || (data == '-'))
{
temp2 = operador_uno;
}
else
{
temp2 = numero_uno;
}
And turn on compiler warnings which would have told you that:
并启用编译器警告,这些警告会告诉您:
- you're using the outer
temp2
uninitialized - you're not using the inner
temp2
variables
你正在使用外部temp2未初始化
你没有使用内部temp2变量
#1
3
maybe not the only issue but:
也许不是唯一的问题,但是:
struct trees *temp2;
if((data == '/') || (data == '*') || (data == '+') || (data == '-')) {
struct trees *temp2 = operador_uno; // not the same "temp2" as above
}
else {
struct trees *temp2 = numero_uno; // not the same "temp2" as above
}
for(i = 0; i < place - 1; i++) {
temp2 = temp2->next; // [CRASH] because temp2 isn't initialized
}
struct trees *temp2 = operador_uno;
is shadowing temp2
declared in the outer scope. So the outer temp2
is never initialized, your initialization sets a value to a variable that goes out of scope.
struct trees * temp2 = operador_uno;是在外部范围内声明的影子temp2。因此,外部temp2永远不会被初始化,您的初始化会将值设置为超出范围的变量。
So remove struct trees *
so the same temp2
variable is used (and initialised), like this (I'd prefer a ternary expression, though):
所以删除结构树*所以使用(并初始化)相同的temp2变量,就像这样(我更喜欢三元表达式):
if((data == '/') || (data == '*') || (data == '+') || (data == '-'))
{
temp2 = operador_uno;
}
else
{
temp2 = numero_uno;
}
And turn on compiler warnings which would have told you that:
并启用编译器警告,这些警告会告诉您:
- you're using the outer
temp2
uninitialized - you're not using the inner
temp2
variables
你正在使用外部temp2未初始化
你没有使用内部temp2变量