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

C 語言實現帶頭結點單鏈表 13 大基本操作完整庫

     編輯:關於C語言

帶頭結點單鏈表:操作統一、邏輯清晰的標準實現

帶頭結點單鏈表將「空表判斷」、「首節點操作」等邊界條件統一化,是數據結構教學與工程實踐的首選。本文提供 13 個核心操作的完整、可編譯運行的 C 語言實現,涵蓋初始化、創建、遍歷、增刪改查、清空等全生命周期。

數據結構定義

typedef int elemType;
typedef struct Node {
    elemType element;
    struct Node *next;
} Node;

13 大核心操作實現

1. 初始化 InitialList

void InitialList(Node **head) {
    *head = (Node*)malloc(sizeof(Node));
    if (!*head) { perror("malloc"); exit(1); }
    (*head)->next = NULL;
}

2. 尾插創建 CreateList

void CreateList(Node *head) {
    Node *pInsert, *pMove = head;
    pInsert = (Node*)malloc(sizeof(Node));
    pInsert->next = NULL;
    scanf("%d", &pInsert->element);
    while (pInsert->element > 0) {
        pMove->next = pInsert;
        pMove = pInsert;
        pInsert = (Node*)malloc(sizeof(Node));
        pInsert->next = NULL;
        scanf("%d", &pInsert->element);
    }
    free(pInsert);
}

3. 打印 PrintList

void PrintList(Node *head) {
    Node *p = head->next;
    while (p) { printf("%d ", p->element); p = p->next; }
    printf("\n");
}

4. 清空 ClearList (保留頭結點)

void ClearList(Node *head) {
    Node *p = head->next;
    while (p) {
        head->next = p->next;
        free(p);
        p = head->next;
    }
}

5. 長度 SizeList

int SizeList(Node *head) {
    int n = 0;
    for (Node *p = head->next; p; p = p->next) n++;
    return n;
}

6. 判空 IsEmptyList

int IsEmptyList(Node *head) { return head->next == NULL; }

7. 按位置查找 GetElement (1-based)

int GetElement(Node *head, int pos) {
    Node *p = head->next;
    for (int i = 1; p && i < pos; i++) p = p->next;
    return p ? p->element : -1;
}

8. 按值查找地址 GetElemAddr

elemType* GetElemAddr(Node *head, int x) {
    for (Node *p = head->next; p; p = p->next)
        if (p->element == x) return &p->element;
    return NULL;
}

9. 修改節點值 ModifyElem

Node* ModifyElem(Node *head, int pos, int x) {
    Node *p = head->next;
    for (int i = 1; p && i < pos; i++) p = p->next;
    if (p) p->element = x;
    return head;
}

10. 頭插 InsertHeadList

Node* InsertHeadList(Node *head, int x) {
    Node *n = (Node*)malloc(sizeof(Node));
    n->element = x;
    n->next = head->next;
    head->next = n;
    return head;
}

11. 尾插 InsertTailList

Node* InsertTailList(Node *head, int x) {
    Node *n = (Node*)malloc(sizeof(Node));
    n->element = x; n->next = NULL;
    Node *p = head;
    while (p->next) p = p->next;
    p->next = n;
    return head;
}

12. 指定位置插入 InsertNode (pos 從 0 開始,頭結點後為 0)

Node* InsertNode(Node *head, int pos, int x) {
    Node *pre = head;
    for (int i = 0; pre && i < pos; i++) pre = pre->next;
    if (!pre) return head;
    Node *n = (Node*)malloc(sizeof(Node));
    n->element = x;
    n->next = pre->next;
    pre->next = n;
    return head;
}

13. 按位置刪除 DeletePosNode / 按值刪除 DeleteValueNode

int main() {
    Node *list; InitialList(&list);
    CreateList(list);        // 輸入: 10 20 30 0
    PrintList(list);         // 10 20 30
    printf("Len: %d\n", SizeList(list));
    InsertHeadList(list, 5); PrintList(list); // 5 10 20 30
    InsertTailList(list, 40); PrintList(list); // 5 10 20 30 40
    InsertNode(list, 2, 99); PrintList(list);  // 5 10 99 20 30 40
    DeletePosNode(list, 3); PrintList(list);   // 5 10 20 30 40
    DeleteValueNode(list, 5); PrintList(list); // 10 20 30 40
    ClearList(list); PrintList(list);          // (空)
    return 0;
}

設計精要總結

原則體現
統一邊界所有操作從 head 開始,無需判斷 head==NULL
內存安全malloc 檢查、free 後置 NULL (此處省略為簡潔)
位置語義查找/刪除用 1-based;插入用 0-based (頭結點後) 更直觀
返回值修改結構的操作返回 head,便於鏈式調用

總結

這 13 個操作構成了單鏈表 ADT (抽象數據類型) 的完整語義集。帶頭結點設計消除了 80% 的特殊情況判斷,代碼行數減少、可讀性提升。建議將此作為個人「鏈表工具庫」模板,面試手寫、項目複用、教學演示皆宜。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved