2012年4月22日 星期日

鏈結串列

http://codepad.org/agZ3XDUC

不管什麼操作都是要先判斷串列是不是為空的
也就是大致上都是長這樣子
if (head == NULL) {
    ....
}
else {
    // 非空時的處理
}

插入的寫法:
while (ptr->next != NULL) {
    ptr = ptr->next;
}
ptr->next = malloc(...);

走訪的寫法:
while (ptr != NULL) {
    // print node
    ptr = ptr->next
}

刪除整條串列:
while (ptr != NULL) {
    nextStudent = ptr->next;
    delete ptr;
    ptr = nextStudent;
}

沒有留言:

張貼留言