# C語言怎么實現單鏈表的基本功能
## 一、單鏈表概述
單鏈表(Singly Linked List)是最基礎的數據結構之一,它通過指針將一組零散的內存塊串聯起來。每個節點(Node)包含兩個部分:
- 數據域:存儲實際數據
- 指針域:存儲下一個節點的內存地址
### 1.1 單鏈表的特點
- 動態內存分配,不需要預先知道數據規模
- 插入/刪除時間復雜度O(1)
- 隨機訪問效率低,時間復雜度O(n)
- 需要額外空間存儲指針
### 1.2 與數組的對比
| 特性 | 數組 | 單鏈表 |
|------------|--------------|-------------|
| 內存連續性 | 連續 | 非連續 |
| 大小 | 固定 | 動態擴展 |
| 插入/刪除 | O(n) | O(1) |
| 隨機訪問 | O(1) | O(n) |
## 二、單鏈表的基本實現
### 2.1 節點結構定義
```c
typedef struct Node {
int data; // 數據域(以int為例)
struct Node *next; // 指針域
} Node;
// 創建空鏈表
Node* createList() {
return NULL; // 返回空指針表示空鏈表
}
// 創建帶頭節點的鏈表
Node* createListWithHead() {
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
return head;
}
void insertAtHead(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void insertAtTail(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
void insertAtIndex(Node** head, int index, int data) {
if (index < 0) return;
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
if (index == 0) {
newNode->next = *head;
*head = newNode;
return;
}
Node* current = *head;
for (int i = 0; current != NULL && i < index - 1; i++) {
current = current->next;
}
if (current == NULL) {
free(newNode);
return;
}
newNode->next = current->next;
current->next = newNode;
}
void deleteAtHead(Node** head) {
if (*head == NULL) return;
Node* temp = *head;
*head = (*head)->next;
free(temp);
}
void deleteAtTail(Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
Node* current = *head;
while (current->next->next != NULL) {
current = current->next;
}
free(current->next);
current->next = NULL;
}
void deleteAtIndex(Node** head, int index) {
if (*head == NULL || index < 0) return;
if (index == 0) {
Node* temp = *head;
*head = (*head)->next;
free(temp);
return;
}
Node* current = *head;
for (int i = 0; current != NULL && i < index - 1; i++) {
current = current->next;
}
if (current == NULL || current->next == NULL) return;
Node* temp = current->next;
current->next = temp->next;
free(temp);
}
Node* searchByValue(Node* head, int value) {
Node* current = head;
while (current != NULL) {
if (current->data == value) {
return current;
}
current = current->next;
}
return NULL;
}
Node* searchByIndex(Node* head, int index) {
if (index < 0) return NULL;
Node* current = head;
for (int i = 0; current != NULL && i < index; i++) {
current = current->next;
}
return current;
}
void traverseList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int getLength(Node* head) {
int count = 0;
Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
Node* reverseList(Node* head) {
Node *prev = NULL, *current = head, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
int hasCycle(Node* head) {
if (head == NULL) return 0;
Node *slow = head, *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return 1;
}
return 0;
}
Node* mergeTwoLists(Node* l1, Node* l2) {
Node dummy = {0, NULL};
Node* tail = &dummy;
while (l1 && l2) {
if (l1->data <= l2->data) {
tail->next = l1;
l1 = l1->next;
} else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = l1 ? l1 : l2;
return dummy.next;
}
void freeList(Node** head) {
Node* current = *head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
*head = NULL;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// [此處插入前面定義的所有函數]
int main() {
Node* list = createList();
// 插入測試
insertAtTail(&list, 1);
insertAtTail(&list, 2);
insertAtHead(&list, 0);
insertAtIndex(&list, 1, 9);
printf("Original List: ");
traverseList(list); // 輸出: 0 -> 9 -> 1 -> 2 -> NULL
// 刪除測試
deleteAtIndex(&list, 1);
deleteAtHead(&list);
printf("After deletion: ");
traverseList(list); // 輸出: 1 -> 2 -> NULL
// 反轉測試
list = reverseList(list);
printf("Reversed List: ");
traverseList(list); // 輸出: 2 -> 1 -> NULL
freeList(&list);
return 0;
}
單鏈表作為基礎數據結構,雖然簡單但功能強大。掌握其C語言實現需要理解: - 指針操作和內存管理 - 邊界條件處理 - 時間/空間復雜度分析 - 實際應用場景
通過不斷練習,可以深入理解鏈表的特性和應用,為學習更復雜的數據結構打下堅實基礎。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。