【C语言教程】“双向循环链表”学习总结及其代码实现

双向循环链表和它名字的表意一样,就是把双向链表的两头连接,使其成为了一个环状链表。只需要将表中最后一个节点的next指针指向头节点,头节点的prior指针指向尾节点,链表就能成环儿,如图所示:

需要注意的是,虽然双向循环链表成环状,但本质上还是双向链表,因此在双向循环链表中,依然能够找到头指针和头节点等。双向循环链表和双向链表相比,唯一的不同就是双向循环链表首尾相连,其他都完全一样。

注意:因为我上面已经讲了双向链表,所以这里只注重讲他们的实现差异。另因为带头节点会更好操作,所以我的代码都有头节点。

1、双向循环链表的创建

初始化时需要将头节点的next和prior都指向自己。

//1、初始化双向循环链表(带头节点)

Status initLinkList(LinkList *list){

    //创建头节点

    *list = malloc(sizeof(Node));

    if (*list == NULL) {

        return ERROR;

    }

    //前驱和后继都指向自己

    (*list)->prior = *list;

    (*list)->data = -1;

    (*list)->next = *list;

    printf("已初始化链表~\n");

    return OK;

}

2、遍历双向循环链表

注意它的尾节点的next不再是Null,而是头节点

//2、遍历双向循环链表

void printfLinkLisk(LinkList list){

    printf("遍历链表:\n");

    if (list == NULL || list->next == list) {

        printf("这是一个空链表\n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向循环链表是一种常用的数据结构之一,可以在链表中进行快速的插入、删除和查找操作。以下是一种简单的C语言实现双向循环链表代码: ``` #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *createNode(int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } void insertAtBegin(struct node **head, int data) { struct node *newNode = createNode(data); if (*head == NULL) { *head = newNode; (*head)->next = *head; (*head)->prev = *head; } else { struct node *last = (*head)->prev; newNode->next = *head; newNode->prev = last; last->next = newNode; (*head)->prev = newNode; *head = newNode; } } void insertAtEnd(struct node **head, int data) { struct node *newNode = createNode(data); if (*head == NULL) { *head = newNode; (*head)->next = *head; (*head)->prev = *head; } else { struct node *last = (*head)->prev; newNode->prev = last; newNode->next = *head; last->next = newNode; (*head)->prev = newNode; } } void deleteNode(struct node **head, int data) { if (*head == NULL) { printf("List is empty.\n"); return; } struct node *cur = *head; struct node *prev = NULL; while (cur->data != data && cur->next != *head) { prev = cur; cur = cur->next; } if (cur->data != data) { printf("%d not found in list.\n", data); return; } if (cur == *head && cur->next == *head) { *head = NULL; free(cur); return; } if (cur == *head) { *head = cur->next; } struct node *next = cur->next; prev->next = next; next->prev = prev; free(cur); } void displayList(struct node *head) { if (head == NULL) { printf("List is empty.\n"); return; } printf("List: "); struct node *cur = head; do { printf("%d ", cur->data); cur = cur->next; } while (cur != head); printf("\n"); } int main() { struct node *head = NULL; insertAtBegin(&head, 10); insertAtBegin(&head, 20); insertAtEnd(&head, 30); insertAtEnd(&head, 40); displayList(head); deleteNode(&head, 20); deleteNode(&head, 50); displayList(head); return 0; } ``` 在这个例子中,我们使用了一个结构体来存储每个节点的数据和指向前一个和后一个节点的指针。我们还定义了一些操作函数,如创建新节点、在链表头部和尾部插入新节点、删除节点和显示链表等。在主函数中,我们创建了一个双向循环链表并进行了一些操作,最后输出链表的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值