循环双链表的简单实现(C语言)

小结

双链表就像一群人手拉手站成一排,最旁边的两个人都有一个手空着。循环双链表就是一群人手拉手站成一圈,第一个人的手和最后一个人的手互相牵着。
在这里插入图片描述

双链表与循环双链表的插入删除等操作基本一致,表尾判断有所不同。双链表判断表尾为后继指向NULL,循环双链表判断表尾则看其后继是否指向头指针。

插入

结点后插

// 后插结点
bool InsertNextNode(DLNode *p, DLNode *s){
   
    if(p == NULL && s == NULL)  return true;    // 数据非法性判断
    p->next->prior = s; // p原后继结点的前继指向s
    s->next = p->next;  // s后继结点设为p的原后继结点
    p->next = s;        // p的后继结点设为s
    s->prior = p;       // s的前继结点设为p 
    return true;
}

结点前插

// 前插结点
bool InsertPriorNode(DLNode *p, DLNode *s){
        // 在p结点前插入s结点
    if(p == NULL && s == NULL)  return true;    // 数据非法性判断
    s->prior = p->prior;    // s的前继结点设为原p的前继结点
    p->prior->next = s;     // p的前继结点的后继指向s
    s->next = p;            // s的后继结点设为p
    p->prior = s;           // p的前继结点设为s
    return true;
}

尾插法

// 尾插法
DLinkList List_TailInsert(DLinkList L){
   
    ElemType e;
    DLNode *p = L;           // 声明一个结点,用于定位
    scanf("%d", &e);
    while(e != 9999){
   
        while(p->next != L){
    // 定位至表尾
        p = p->next;
        }
    DLNode *s = (DLNode*)malloc(sizeof(DLNode));    // 声明一个新结点用于插入
    s->data = e;            // 存入数据
    s->next = NULL;
    s->prior = NULL;
    InsertNextNode(p, s);   // 调用后插结点函数实现插入
    scanf("%d", &e);
    }
    return L;
}

头插法

// 头插法
DLinkList List_HeadInsert(DLinkList L){
   
    ElemType e;
    DLNode *p = L;           // 声明一个结点,用于定位
    scanf("%d", &e);
    while(e != 9999){
   
        if(p->next == L){
      // 第一个结点特殊处理
            DLNode *s = (DLNode*)malloc(sizeof(DLNode));    // 声明一个新结点用于插入
            s->data = e;            // 存入数据
            L->next = s;
            L->prior = s;
            s->next = L;
            s->prior = L;       
        }else{
   
            DLNode *s = (DLNode*)malloc(sizeof(DLNode));    // 声明一个新结点用于插入
            s->data = e;    // 存入数据
            s->next = NULL;
            s->prior = NULL; 
            InsertPriorNode(L->next, s);    // 调用前插结点函数,在第一个结点进行前插    
        }
    scanf("%d", &e);
    }
    return L;
}

按位插入

// 按位插入
bool ListInsert(DLinkList L, int pos, ElemType e){
   
    if(L == NULL || pos < 1) return false;
    DLNode *p = L;
    DLNode *s = (DLNode*)malloc(sizeof(DLNode));    // 创建一个新结点用于插入
    s->data = e;
    s->next = NULL;
    s->prior = NULL;
    while(--pos){
      // 定位到插入位序的前一个结点
        p = p->next;       
    }
    if(InsertNextNode(p, s)){
      //调用结点后插函数进行后插操作
        return true;
    }else false;
}

删除

结点后删

// 后删结点
bool DeleteNextNode(DLNode *p){
   
    if(p == NULL) re
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是循环双链表的值排序的C语言代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; struct Node* prev; }; void insertAtEnd(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = (*head_ref); new_node->data = new_data; new_node->next = (*head_ref); if ((*head_ref) == NULL) { new_node->prev = new_node; (*head_ref) = new_node; return; } while (last->next != (*head_ref)) last = last->next; last->next = new_node; new_node->prev = last; (*head_ref)->prev = new_node; } void bubbleSort(struct Node** head_ref) { struct Node *i, *j; int temp; for (i = (*head_ref); i->next != (*head_ref); i = i->next) { for (j = i->next; j != (*head_ref); j = j->next) { if (i->data > j->data) { temp = i->data; i->data = j->data; j->data = temp; } } } } void printList(struct Node* node) { struct Node* temp = node; if (node != NULL) { do { printf("%d ", temp->data); temp = temp->next; } while (temp != node); } } int main() { struct Node* head = NULL; insertAtEnd(&head, 4); insertAtEnd(&head, 2); insertAtEnd(&head, 1); insertAtEnd(&head, 3); printf("Original List: "); printList(head); bubbleSort(&head); printf("\nSorted List: "); printList(head); return 0; } ``` 该代码使用冒泡排序算法对循环双链表进行排序。在每次迭代中,它比较相邻的节点,并交换它们的位置,以便将最小的节点移到列表的前面。最终,整个列表将按升序排列。 注意,这里的循环双链表采用头尾相连的方式,即最后一个节点的 next 指向头节点,头节点的 prev 指向最后一个节点。这样,我们就可以遍历整个列表,同时也可以很方便地将新节点插入到列表的末尾。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值