【发布时间】:2019-01-09 13:23:09
【问题描述】:
我在创建一个在 C 中创建两个单独的链表的函数时遇到问题。
在我的程序中,用户输入一个等式,例如 7 + 9 * 8,一个字符一个字符,然后程序用它们创建列表。
代码如下:(where place is where is应该在列表中,data是数字/运算符本身。两者都来自程序的另一部分)
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;
}
}
我通过大量 printf 语句确定它将成功地将等式中的第一个 number 添加到列表中,它如何不添加第一个 operator 以及第二个数字程序完全崩溃。
当我把 temp2->next = temp1.
非常感谢任何帮助!
【问题讨论】:
-
学习使用 gdb 将是给自己的一份很棒的礼物,因为它可以节省查找故障位置的时间。
标签: c list linked-list