美文网首页
双向循环链表

双向循环链表

作者: dawter | 来源:发表于2016-08-25 22:54 被阅读0次

    双向循环链表

    • 初始化
    • 添加元素
    • 删除元素
    • 输出整个链表

    实现代码

    /*
     * bidirectional circular linked list
     */
    #include <stdio.h>
    #include <stdlib.h>
    
    struct bclist {
            struct bclist *next;
            struct bclist *prev;
            int data;
    };
    
    struct bclist* bclist_init(int data)
    {
            struct bclist *p = malloc(sizeof(struct bclist));
            if(p) {
                    p->next = p;
                    p->prev = p;
                    p->data = data;
            }
            return p;
    }
    
    struct bclist* bclist_add(struct bclist* h, int data)
    {
            struct bclist *p = h;
    
            if(NULL == h) {
                    return h;
            }
    
            do {
                    if(p->data == data) {
                            return h;
                    }
                    p = p->next;
            }while(p != h);
    
            p = malloc(sizeof(struct bclist));
            if(NULL == p) {
                    return h;
            }
            p->next = h;
            p->prev = h->prev;
            h->prev = p;
            p->prev->next = p;
    
            p->data = data;
    
            return h;
    }
    
    struct bclist* bclist_delete(struct bclist* h, int data)
    {
            struct bclist *p = h;
            struct bclist *q = NULL;
    
            do {
                    if(p->data == data) {
                            q = p;
                            p = p->prev;
                            q->next->prev = q->prev;
                            q->prev->next = q->next;
                            free(q);
                    }
                    p = p->next;
            } while(p != h);
    
            return h;
    }
    
    struct bclist* bclist_free(struct bclist* h)
    {
            struct bclist* p = h;
            do {
                    p = p->next;
            } while(p != h);
    
            return h;
    }
    
    void bclist_print(struct bclist* h)
    {
            struct bclist* p = h;
    
            do {
                    printf("%d ", p->data);
                    p = p->next;
            }while(p != h);
    
            printf("\n");
    }
    
    int main(int argc, char** argv)
    {
            const int ret = 0;
            struct bclist* h = NULL;
    
            h = bclist_init(0);
            bclist_add(h, 1);
            bclist_add(h, 2);
            bclist_add(h, 3);
            bclist_add(h, 2);
            bclist_print(h);
    
            bclist_delete(h, 2);
            bclist_add(h, 5);
            bclist_print(h);
    
            return ret;
    }
    

    尚未实现

    • bclist_free删除链表所有元素

    相关文章

      网友评论

          本文标题:双向循环链表

          本文链接:https://www.haomeiwen.com/subject/mbbasttx.html