我確信之前已經詢問過這一點,但在搜索相當長的一段時間後,本網站或其他任何關於它的內容都找不到。在功能未初始化的情況下初始化鏈接列表結構
我很難得到我在函數中創建和修改的結構的值。代碼看起來是這樣的:
struct node {
char name[35];
int employeeID;
struct node *next;
}
typedef struct node employee;
void insertInOrder(employee *head, employee *curr) {
if (head == NULL) {
*curr->next = *head;
*head = *curr;
} else {
if ((head->employeeID<curr->employeeID)&&(curr->employeeID <head->next->employeeID)) {
*curr->next = *head->next;
*head->next = *curr;
} else {
insertInOrder(head->next, curr);
}
}
}
void addEmployee(char name[], employee *head, employee *curr) {
int id;
scanf("%d", &id);
curr = malloc(sizeof(employee));
strcpy(curr->name, name);
curr->employeeID = id;
insertInOrder(head, curr);
}
int main(void) {
char name[35];
int quit = 1;
employee *head, *curr;
head = NULL;
printf("Enter data about the books: \n");
while (quit) {
scanf("%[^\n]%*c", title);
if (title[0] != '#') {
addBook(name, head, curr);
} else {
quit = 0;
}
}
在我調試,我的代碼迭代到我所有的功能,但一旦我將所有我想要的數據後回到主,所有的變量都是空的。我知道它與我使用或傳遞指針的方式有關,但是當我查看代碼時,我總是會得出合乎邏輯的結論,即我擁有的應該按照我的意願去做。請有人指出我的算法存在缺陷。
這是來自舊代碼的工件,謝謝指出。我減少了我的實際代碼,使其更簡單,更易讀。 – brostone51 2014-10-08 20:00:38