链表(Linked List)的C语言实现

本文介绍了链表数据结构的C语言实现,包括单链接和双链接的未排序非循环链表,以及单链接的循环链表和有哨兵的双链接未排序循环链表。通过定义结构体和实现相关函数,展示了链表的动态内存分配和操作方法。
摘要由CSDN通过智能技术生成

链表中的各对象按线性顺序排列,而其顺序是由各个对象里的指针所决定的。
链表有多种形式,它可以是单链接的或者双链接的,可以是已排序的或未排序的,可以是循环的或非循环的。
定义的结构体以及函数如下:

typedef struct SINGLE_LINKED_LIST
{
    Node * head;
    Node * tail;
} SLL;
typedef struct CIRCULAR_SINGLE_LINKED_LIST
{
    Node * head;
    Node * tail;
} CSLL;
typedef struct DOUBLE_LINKED_LIST
{
    Node * head;
    Node * tail;
} DLL;
typedef struct CIRCULAR_DOUBLE_LINKED_LIST_WITH_SENTINEL
{
    Node * nil;
} CDLLS;

void linked_list_traverse(Node * L);
void linked_list_free(Node* L);

Node* single_linked_list_insert(SLL * L, item_t item);
Node* single_linked_list_search(SLL * L, item_t item);
int single_linked_list_delete(SLL * L, Node * node);

Node* circular_single_linked_list_insert(CSLL * L, item_t item);
Node* circular_single_linked_list_search(CSLL * L, item_t item);
int circular_single_linked_list_delete(CSLL * L, Node * node);

Node* double_linked_list_insert(DLL * L, item_t  item);
Node* double_linked_list_search(DLL * L, item_t item);
int double_linked_list_delete(DLL * L, Node * node);

CDLLS* circular_double_linked_list_with_sentinel_init();
Node* circular_double_linked_list_with_sentinel_insert(CDLLS * L, item_t item);
Node* circular_double_linked_list_with_sentinel_search(CDLLS * L, item_t item);
int circular_double_linked_list_with_sentinel_delete(CDLLS * L, Node * node);

几个公共函数如下:

//functions for all kind of linked list
void linked_list_traverse(Node* L) {
    if (L == NULL) {
        printf("Empty Linked List.\n");
    }
    Node * pos = L;
    while (pos != NULL) {
        printf("%2d is in location:%9p. prev is %9p. next is %9p.\n", pos->item.key, \
            pos, pos->prev, pos->next);
        pos = pos->next;
        if (pos == L) return;
    }
}

void linked_list_free(Node* L) {
    Node * pos = L;
    Node * f = L;
    while (pos != NULL && pos != L) {
        f = pos;
        pos = pos->next;
        free(f);
    }
}
//-----------------------------------------------------------------------

单链接的未排序非循环链表可以如如下实现:

//functions for single linked list
Node* single_linked_list_insert(SLL * L, item_t item) {
    Node * node = (Node*)malloc(sizeof(Node));
    node->item = item;
    node->prev = NULL;
    node->next = L->head;
    if (L->tail == NULL)
        L->tail = node;
    L->head = node;
    return node;
}
Node* single_linked_list_search(SLL * L, item_t item) {
    if (L->head == NULL) {
        fprintf(stderr, "The single linked list is empty.\n");
        return NULL;
    }
    Node * pos = L->head;
    while (pos != NULL) {
        if (item.key == pos->item.key)
            return pos;
        pos = pos->next;
    }
    fprintf(stderr, "The item cannot be found.\n");
    return NULL;
}
int single_linked_list_delete(SLL * L, Node * node) {
    if (L->head == NULL) {
        fprintf(stderr, "The single linked list is empty.\n");
        return 0;
    }
    if (node == NULL) {
        fprintf(stderr, "The node is NULL.\n");
        return 0;
    }
    if (L->head == node) {
        L->head = node->next;
        if (L->head == NULL)
            L->tail = NULL;
        return 1;
    }
    Node * pos = L->head;
    while (pos != NULL && pos->next != node) {
        pos &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值