双向循环链表的操作

    xiaoxiao2024-12-17  67

    一、双向循环链表的实现,如果理解了前面的双向链表和循环链表,那么理解双向循环链表是一件很简单的事情,这里的实现是在我之前的双向链表的基础上,将尾结点的next指向头结点,将头结点的pioneer指向尾结点,就可以实现双向循环链表了。

    typedef struct DNode { int data; struct DNode *front; struct DNode *next; }DNode; DNode *create_DNode_loop_list(int n) { int a, i; DNode *head, *pioneer, *previous; head = (DNode *)malloc(sizeof(DNode)); head->data = 0; head->next = NULL; head->front = NULL; pioneer = head; previous = head; for(i = 0; i < n; i++){ scanf("%d", &a); // 1:malloc space for the new Node and let the pioneer point to it pioneer = (DNode *)malloc(sizeof(DNode)); // 2:initiate the new Node pioneer->data = a; pioneer->next = NULL; pioneer->front = previous; // 3:use the previous to link the previous to the new Node previous->next = pioneer; // 4:update the previous previous = pioneer; } pioneer->next = head; head->front = pioneer; return head; }
    最新回复(0)