2014-10-08 122 views
0

我確信之前已經詢問過這一點,但在搜索相當長的一段時間後,本網站或其他任何關於它的內容都找不到。在功能未初始化的情況下初始化鏈接列表結構

我很難得到我在函數中創建和修改的結構的值。代碼看起來是這樣的:

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; 
    } 
} 

在我調試,我的代碼迭代到我所有的功能,但一旦我將所有我想要的數據後回到主,所有的變量都是空的。我知道它與我使用或傳遞指針的方式有關,但是當我查看代碼時,我總是會得出合乎邏輯的結論,即我擁有的應該按照我的意願去做。請有人指出我的算法存在缺陷。

回答

1

addBook需要Book類型的指針,但你傳遞一個類型的指針Employee

編輯:

所以,首先你不需要做這樣的事情*curr->next = *head。它應該是curr->next = head。 另外,head->next可以爲null,不會被檢查。最後,head需要始終指向列表的開頭。

編輯2:

以下代碼應該工作。 head總是指向列表的開始。爲了做到這一點,我們必須通過頭指針的地址。我們需要這樣做,因爲我們將修改地址head

我也清理了一些東西。

void insertInOrder(employee **head, employee *curr) { 

    if (*head == NULL) { 
    // We are inserting the first element 
    *head = curr; 
    } 
    else if ((*head)->next == NULL) { 
    // This is the second element. We either insert it in front of head or before head. 
    if ((*head)->employeeID < curr->employeeID) { 
     (*head)->next = curr; 
    } 
    else { 
     curr->next = *head; 
     *head = curr; 
     (*head)->next = NULL; 
    } 
    } 

    else { 
    // We iterate through the list trying to find the best spot to insert curr. 
    employee *temp = *head; 
    while (temp->next != NULL) { 
     if ((temp->employeeID < curr->employeeID) && (curr->employeeID < temp->next->employeeID))  { 
      curr->next = temp->next; 
      temp->next = curr; 
      break; 
     } 
     temp = temp->next; 
    } 
    // curr has the greatest id so it is inserted at the end 
    if (temp->next == NULL) 
     temp->next = curr; 
    } 
} 

void addEmployee(char name[], employee **head) { 
    int id; 
    printf("Enter id\n"); 
    scanf("%d", &id); 
    employee *curr = malloc(sizeof(employee)); 
    strcpy(curr->name, name); 
    curr->employeeID = id; 
    curr->next = NULL; 
    insertInOrder(head, curr); 
} 

int main(void) { 
    int quit = 1; 
    employee *head = NULL; 
    char title[100]; 
    printf("Enter data about the employees: \n"); 
    while (quit) { 
    scanf("%s", title); 
    if (title[0] != '#') 
     addEmployee(title, &head); 
    else break; 
    } 
    return 0; 
} 
+0

這是來自舊代碼的工件,謝謝指出。我減少了我的實際代碼,使其更簡單,更易讀。 – brostone51 2014-10-08 20:00:38

0

在函數內部沒有必要使用*頭或* CURR ..because - >是由爲指針而不是隻直接使用頭戴式>左& curr->未來

感謝

+0

道歉,這是從我身上試圖不同的可能性遺留下來。我已將代碼更改爲我實際擁有的代碼。 – brostone51 2014-10-08 20:04:23