数据结构_链表
发布日期:2021-05-14 23:43:05 浏览次数:17 分类:精选文章

本文共 3066 字,大约阅读时间需要 10 分钟。

链表结构与通用链表实现

链表是一种常见的数据结构,广泛应用于内存管理、文件操作等领域。它具有动态分配与释放的特点,非常适合处理非固定长度的数据。以下将从链表的基本概念、单向链表和双向链表的实现、中生命周期、以及通用链表的规范化编码入手分析。

链表的基本概念

链表是一系列节点的无序排列,每个节点包含数据和指针字段。指针字段用于连接节点之间,具体包括:

  • 单向链表:仅包含下一个节点的指针(next),节点之间单向连接。
  • 双向链表:包含上一个节点和下一个节点的指针(prev和next),实现双向连接。
  • 循环链表:最后一个节点的next指针指向第一个节点,形成环状结构。
  • 通用链表:采用前向指针结构,只需定义统一的指针域,便于不同结构体的使用。
  • 单向链表适合应用场景较简单的情况,而双向链表能够更方便地反向遍历,增强灵活性。

    单向链表实现

    单向链表的核心是节点间单向连接,操作包括插入、删除和遍历。以下是实现过程示例代码:

    #include 
    #include
    struct student
    {
    char name[20];
    int id;
    int score;
    struct student* next;
    };
    typedef struct student stud;
    void output(void)
    {
    for (stud* current = head; current->next != NULL; current = current->next)
    {
    printf("%d %s %d\n", current->next->id, current->next->name, current->next->score);
    }
    }
    void insert(void)
    {
    stud* p = (stud*)malloc(sizeof(stud));
    scanf("%d %s %d", &p->id, p->name, &p->score);
    stud* current = head;
    while (current->next != NULL)
    {
    current = current->next;
    }
    current->next = p;
    }
    stud* creat(stud* p)
    {
    p = (stud*)malloc(sizeof(stud));
    p->next = NULL;
    return p;
    }
    int main(void)
    {
    head = creat(head);
    char n = '\0';
    while (n != 'q')
    {
    insert();
    printf("按任意键继续,或者按'q'退出:\n");
    while (getchar() != '\n')
    {
    continue;
    }
    scanf("%c", &n);
    }
    printf("输入的学生信息为:\n");
    output();
    return 0;
    }

    双向链表的规范化编码

    通用链表规范化编码能够实现不同数据结构共享指针域。以下展示通用链表定义及其实现:

    #ifndef __list_head_H__
    #define __list_head_H__
    struct list_head
    {
    struct list_head* prev;
    struct list_head* next;
    };
    void INIT_LIST_HEAD(struct list_head* list)
    {
    list->next = list;
    list->prev = list;
    }
    void list_add(struct list_head* node, struct list_head* head)
    {
    node->next = head->next;
    node->prev = head;
    head->next = node;
    head->next->prev = node;
    }
    void list_add_tail(struct list_head* node, struct list_head* head)
    {
    node->next = head->prev;
    node->prev = head;
    head->prev->next = node;
    head->prev = node;
    }
    void list_del(struct list_head* node)
    {
    node->prev->next = node->next;
    node->next->prev = node->prev;
    }
    #define list_for_next_each(pos, head) \
    for (pos = head->next; pos != head; pos = pos->next)
    #define list_for_prev_each(pos, head) \
    for (pos = head->prev; pos != head; pos = pos->prev)
    #define container_of(ptr, type, member) \
    (type*) ( (int)ptr - (int)&( (type*)0 )->member )
    #endif

    芯片LENR与Fir Filter设计

    以下是芯片LENR和Fir Filter设计时所采用的链表结构:

    struct Node
    {
    uint32 channel constitutional;
    uint32 amp_level;
    uint32 cnt;
    struct Node* next;
    struct Node* prev;
    };
    int data = 0;
    int main(void)
    {
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->next = &head;
    head->prev = &head;
    for (int i = 0; i < 32; i++)
    {
    node[i].amp_level = rand() % 1024;
    node[i].cnt = rand() % 1000;
    insert_node(head, &node[i]);
    }
    display_link(head);
    delete_node(head, ...
    ...
    }

    链表在设计中扮演着重要角色,特别是在处理动态数据要求时。通用链表结构的规范化编码能够极大地提升代码的复用性和可维护性。

    上一篇:git本地版本控制
    下一篇:数据结构_树

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年05月14日 14时54分00秒