#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct node {
char* identifier;
char* expression;
struct node* next;
} node;
void add(node** database, char* _identifier, char* _expression, int * size) {
node* tmp = (node*) malloc (sizeof(node));
tmp->identifier = _identifier;
tmp->expression = _expression;
tmp->next = *database;
*database = tmp;
(*size)++;
printf("Added: %s : %s\n", _identifier, _expression); fflush(NULL);
}
void show(node* database, int size) {
if (database == NULL) {
printf("Database empty\n");
}
else {
node* tmp = database;
printf("Identifier list (%d):\n", size); fflush(NULL);
while (tmp != NULL) {
printf("%s : \"%s\" \n", tmp->identifier, tmp->expression);fflush(NULL);
tmp = tmp->next;
}
}
}
int main() {
node* database = NULL;
int size = 0;
add(&database, "a1", "abc", &size);
add(&database, "a2", "def", &size);
add(&database, "a3", "ghi", &size);
char identifier[20];
char expression[1000];
int i;
for (i = 0; i<3; i++) {
scanf("%s %s", identifier, expression);
add(&database, identifier, expression, &size);
}
show(database, size);
printf ("Bye!");
return 0;
}
「添加」功能效果很好手動調用的時候,但它並沒有在循環裏工作,從標準輸入讀取數據。以下是一次運行的結果:
Added: a1 : abc
Added: a2 : def
Added: a3 : ghi
a4 mno
Added: a4 : mno
a5 ghi
Added: a5 : ghi
a6 kml
Added: a6 : kml
Identifier list (6):
a6 : "kml"
a6 : "kml"
a6 : "kml"
a3 : "ghi"
a2 : "def"
a1 : "abc"
Bye!
正如您所看到的節點a1,a2,a3已正確添加到列表中。但是,在正確添加到列表後,a4和a5在添加a6後更改爲全部「a6」。 我花了一天的時間,但我無法弄清楚。任何人?
哦sh * t爲什麼我沒有想到這一點。謝謝你的哥們。 – 2012-04-16 17:41:54