首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> 關於C

C語言雙向鏈表建表:頭插法與尾插法的異同與實現

     編輯:關於C

兩種建表策略對比

維度頭插法尾插法
輸入順序與鏈表順序逆序同序
是否需尾指針不需要需要(或遍歷到尾)
適用場景棧、逆序輸出、單向鏈表隊列、保持輸入順序、雙向鏈表
代碼複雜度略簡單略複雜(維護 tail)

頭插法實現(無頭節點)


Node* HeadInsertCreate(void) {
    Node *head = NULL;
    int x;
    while (scanf("%d", &x) == 1 && x > 0) {
        Node *n = malloc(sizeof(Node));
        n->element = x; n->prior = n->next = NULL;
        n->next = head;
        if (head) head->prior = n;
        head = n;
    }
    return head;  // 返回首節點
}

尾插法實現(無頭節點)


Node* TailInsertCreate(void) {
    Node *head = NULL, *tail = NULL;
    int x;
    while (scanf("%d", &x) == 1 && x > 0) {
        Node *n = malloc(sizeof(Node));
        n->element = x; n->prior = n->next = NULL;
        if (!head) {
            head = tail = n;
        } else {
            tail->next = n;
            n->prior = tail;
            tail = n;
        }
    }
    return head;
}

帶頭尾哨兵的尾插法(推薦生產用)


void List_CreateTail(DList *L) {
    Node *p = L->head;
    int x;
    while (scanf("%d", &x) == 1 && x > 0) {
        Node *n = malloc(sizeof(Node));
        n->element = x; n->prior = n->next = NULL;
        p->next = n; n->prior = p;
        n->next = L->tail; L->tail->prior = n;
        p = n;
    }
}

選擇建議

  1. 教學/算法題:按題目要求選擇,頭插法常用於「逆序輸出」或「單鏈表就地逆置」。
  2. 工程庫:統一使用帶頭尾哨兵的尾插法,順序直觀、操作統一、無邊界特判。
  3. 若必須無哨兵:尾插法維護 tail 指針,頭插法僅維護 head。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved