数据结构:循环链表


循环链表是另一种形式的链式存储结构,其特点是最后一个节点的指针指向链表的第一个节点,形成一个闭环。本文将详细介绍如何在C语言中实现循环链表。

1.循环链表的基本结构

1.1节点结构体的定义

//结点的构建  
typedef int ElemType;  
typedef struct LNode {  
    ElemType data;  //数据域  
    struct LNode* next;     //节点域  
}LNode,*LinkNode;

1.2循环链表的初始化

//循环链表初始化  
void InitLinkList(LinkNode L,int initdata) {  
    L = (LNode*)malloc(sizeof(LNode));  //向内存请求存储空间  
    L->data = initdata;  
    L->next = L;    //使头结点指向自己  
}

2.循环链表的一些基本的操作

2.1插入节点

在循环链表中插入节点可以有不同的位置,例如在头部、尾部或指定位置。以下是在尾部插入节点的示例:

void InsertTail(LinkNode* L, ElemType e) {
    LinkNode newNode = (LinkNode)malloc(sizeof(LNode));
    if (newNode == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = e;
    newNode->next = L->next;  // 新节点的next指向头节点的next
    L->next = newNode;  // 头节点的next指向新节点
}

2.2删除节点

void DeleteNode(LinkNode* L, ElemType e) {
    LinkNode current = L, prev = NULL;
    while (current->next != L && current->data != e) {
        prev = current;
        current = current->next;
    }
    if (current->data == e) {
        if (prev == NULL) {  // 删除头节点
            L = current->next;
        } else {
            prev->next = current->next;
        }
        free(current);
    } else {
        printf("Element not found\n");
    }
}

2.3遍历循环链表

遍历循环链表是查看链表中所有节点数据的一种方法。由于循环链表的特性,需要特别注意避免无限循环:

void TraverseList(LinkNode L) {
    if (L == NULL) return;
    LinkNode current = L->next;  // 从头节点的next开始遍历
    do {
        printf("%d ", current->data);
        current = current->next;
    } while (current != L->next);  // 当遍历回到头节点的next时停止
    printf("\n");
}

2.4清空循环链表

void ClearList(LinkNode* L) {
    LinkNode current = L, nextNode;
    while (current != L) {
        nextNode = current->next;
        free(current);
        current = nextNode;
    }
    free(L);
    L = NULL;
}

2.5寻找节点

LinkNode FindNode(LinkNode L, ElemType e) {
    LinkNode current = L->next;  // 从头节点的next开始遍历
    do {
        if (current->data == e) {
            return current;
        }
        current = current->next;
    } while (current != L->next);  // 当遍历回到头节点的next时停止
    return NULL;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的大学生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值