C语言链表的原理及使用方法

一、什么是链表?

链表是一种基本的数据结构,它由一系列节点组成。每个节点包含数据和一个指向下一个节点的指针。与数组不同,链表的节点不必在内存中相邻。这使得链表能够灵活地分配内存,从而允许插入和删除操作更加高效。

链表的类型

1.单链表

单链表是一种基本的链表类型,每个节点包含数据和一个指向下一个节点的指针。

struct Node {
    int data;
    struct Node* next;
};

2.双链表

双链表在单链表的基础上扩展,每个节点除了包含指向下一个节点的指针外,还包含指向前一个节点的指针。

struct DNode {
    int data;
    struct DNode* next;
    struct DNode* prev;
};

二、使用步骤

1.链表节点

每个链表节点包含两个主要部分:数据域和指针域。

  • 数据域:数据域用于存储节点的实际数据,可以是整数、字符、结构体或其他数据类型,具体取决于需求。

  • 指针域:指针域包含指向下一个节点的指针。在双链表中,可能还包含指向前一个节点的指针。指针域使得链表中的节点能够相互连接,形成一个序列。

    // 单链表节点结构
    struct Node {
        int data;
        struct Node* next;
    };
    
    // 双链表节点结构
    struct DNode {
        int data;
        struct DNode* next;
        struct DNode* prev;
    };
    

2.链表操作

创建链表

要创建链表,首先需要分配内存以存储链表的头节点,并初始化它。

struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = 0;  // 初始化头节点的数据
head->next = NULL; // 初始时链表为空

插入,删除和遍历节点

        单链表示例

#include <stdio.h>
#include <stdlib.h>

// 单链表节点结构
struct Node {
    int data;
    struct Node* next;
};

// 创建链表并初始化
struct Node* createLinkedList() {
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 0; 
    head->next = NULL;
    return head;
}

// 插入节点
void insertNode(struct Node* head, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = head->next;
    head->next = newNode;
}

// 删除节点
void deleteNode(struct Node* head, int data) {
    struct Node* current = head;
    while (current->next != NULL) {
        if (current->next->data == data) {
            struct Node* temp = current->next;
            current->next = temp->next;
            free(temp);
            return;
        }
        current = current->next;
    }
}

// 遍历链表并打印节点数据
void traverseLinkedList(struct Node* head) {
    struct Node* current = head->next;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = createLinkedList();
    
    insertNode(head, 1);
    insertNode(head, 2);
    insertNode(head, 3);
    
    printf("Linked List: ");
    traverseLinkedList(head);
    
    deleteNode(head, 2);
    
    printf("Linked List after deleting 2: ");
    traverseLinkedList(head);
    
    return 0;
}

        双链表示例

​

​

#include <stdio.h>
#include <stdlib.h>

// 双链表节点结构
struct DNode {
    int data;
    struct DNode* next;
    struct DNode* prev;
};

// 创建双链表并初始化
struct DNode* createDLinkedList() {
    struct DNode* head = (struct DNode*)malloc(sizeof(struct DNode));
    head->data = 0; 
    head->next = NULL;
    head->prev = NULL;
    return head;
}

// 插入节点
void insertDNode(struct DNode* head, int data) {
    struct DNode* newNode = (struct DNode*)malloc(sizeof(struct DNode));
    newNode->data = data;
    newNode->next = head->next;
    newNode->prev = head;
    if (head->next != NULL) {
        head->next->prev = newNode;
    }
    head->next = newNode;
}

// 删除节点
void deleteDNode(struct DNode* head, int data) {
    struct DNode* current = head->next;
    while (current != NULL) {
        if (current->data == data) {
            current->prev->next = current->next;
            if (current->next != NULL) {
                current->next->prev = current->prev;
            }
            free(current);
            return;
        }
        current = current->next;
    }
}

// 遍历双链表并打印节点数据
void traverseDLinkedList(struct DNode* head) {
    struct DNode* current = head->next;
    while (current != NULL) {
        printf("<- %d <-> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    struct DNode* head = createDLinkedList();
    
    insertDNode(head, 1);
    insertDNode(head, 2);
    insertDNode(head, 3);
    
    printf("Double Linked List: ");
    traverseDLinkedList(head);
    
    deleteDNode(head, 2);
    
    printf("Double Linked List after deleting 2: ");
    traverseDLinkedList(head);
    
    return 0;
}

 

总结

链表的优点和缺点

优点:
  1. 动态大小: 链表的大小可以根据需要动态增加或减少,而数组的大小是固定的。
  2. 插入和删除高效: 在链表中插入和删除节点相对容易和高效,只需要调整节点指针的指向,而不需要移动大量元素。
  3. 不需要预先分配内存: 链表不需要预先分配内存空间,它可以根据需要动态分配内存。
  4. 不需要移动元素: 在链表中插入或删除元素时,不需要像数组那样移动其他元素。
缺点:
  1. 随机访问较慢: 链表中的元素并不是在内存中相邻存储的,因此要访问特定位置的元素,需要从头节点开始遍历。
  2. 额外的空间开销: 每个节点都需要额外的空间来存储指针信息,因此相对于数组来说,链表会有更多的内存开销。

即数组的优点就是链表的缺点,链表的优点就是数组的缺点 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值