排行榜 统计
  • 文章总数:649 篇
  • 评论总数:10704 条
  • 分类总数:4 个
  • 最后更新:4月4日
none

单循环链表-这么好的单链表结构怎么能不会呢?带哨兵位头节点双向循环链表

本文阅读 6 分钟
首页 正文
本文最后更新于2022年11月23日,已超过893天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

您阅读这篇文章共耗时:

  带头循环双向链表

  优势是什么

  先看看长啥样子

  每一个节点都记录该节点的前后的节点,这会有什么好处呢?

  头插头删,尾插尾删特别方便时间复杂度都是O(1)

  另一个优势是既能从前往后走,又能从后往前走。

  带哨兵位头节点双向循环链表的基本操作

  这一次,会写的规范一点。

  准备3个文件,一个头件,一个链表操作文件,一个主函数所在的文件,和通讯录那一篇设计是一样的。

  H:带头,D:双向,Loop:循环,List:链表

  .h

   #include

    #include
    #include
    typedef int LTDataType;
    typedef struct ListNode
    {
        LTDataType _data;
        struct ListNode* _next;
        struct ListNode* _prev;
    }ListNode;
    //开辟+初始化头节点
    ListNode* AollocListNode(int x);
    // 双向链表销毁
    void ListDestory(ListNode* pHead);
    // 双向链表打印
    void ListPrint(ListNode* pHead);
    // 双向链表尾插
    void ListPushBack(ListNode* pHead, LTDataType x);
    // 双向链表尾删
    void ListPopBack(ListNode* pHead);
    // 双向链表头插
    void ListPushFront(ListNode* pHead, LTDataType x);
    // 双向链表头删
    void ListPopFront(ListNode* pHead);
    // 双向链表查找
    ListNode* ListFind(ListNode* pHead, LTDataType x);
    // 双向链表在pos的前面进行插入
    void ListInsert(ListNode* pos, LTDataType x);
    // 双向链表删除pos位置的节点
    void ListErase(ListNode* pos, ListNode* pHead);

  开辟节点+初始化

  申请一个节点,并初始化。

  有下面两种形式,但当链表为空的时候,是成环的,建议用下面一种。

  这样设计以后函数形参中的指针是不可为NULL的,可以加一个assert判断一下。

   ListNode* AollocListNode(LTDataType x)

    {
        ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
        newnode->_data = x;
        newnode->_next = newnode;
        newnode->_prev = newnode;
        return newnode;
    }

  打印

  这里需要另外一个指针遍历去遍历链表,当回到头节点的时候即结束,如果使用pHead去遍历,那就死循环了。

   void ListPrint(ListNode* pHead)

    {
        ListNode* p = pHead->_next;//指向第一个节点
        while (p != pHead)
        {
            printf("%d->", p->_data);
            p = p->_next;
        }
        printf("NULL\n");
    }

  销毁

  销毁链表,释放所有节点

  循环中,先把除头节点外的所有节点删除,出了循环再删除头节点。

  循环结束的条件和打印一样,当指向头节点的时候就结束了

  删除一个节点,指针的指向怎么改变呢?

  拷贝第一个节点,头节点指向指向第二个节点,第二个节点指向头节点,指向完成后,就可以删除了。

   void ListDestory(ListNode* pHead)

    {
        assert(pHead);
        while (pHead->_next != pHead)
        {
            struct ListNode* t = pHead->_next;//先记录要被释放的节点(第一个节点)
            pHead->_next = t->_next;
            t->_next->_prev = pHead;
            free(t);
        }
        free(pHead);
    }

  插入 头插

  头插指的是在头节点后面插入一个新的节点作为第一个节点。

  改变指向有两种形式,一种是再来一个临时变量记录中间节点。另一种是在原有的变量种改变指向。

  这里详细介绍原有变量改变指向

  ①②顺序可以颠倒

  有两种形式去找原第一个节点,可以通过,也可以通过头节点。

  通过头节点去找原第一个节点,③④顺序不可颠倒。通过去找原第一个节点,③④顺序无所谓。

  ④:pHead->_next =

  ③:pHead->_next->_prev =

  先执行④单循环链表,pHead->_next不再指向原第一个节点

   void ListPushFront(ListNode* pHead, LTDataType x)

    {
        assert(pHead);
        ListNode* newnode = AollocListNode(x);
        newnode->_prev = pHead;
        newnode->_next = pHead->_next;
        pHead->_next->_prev = newnode;
        pHead->_next = newnode;
        //newnode->_next->_prev = newnode;
    }

  有中间变量的形式

  这样看起来逻辑会更清晰,但我还是会用上面一种,要多用才多熟悉,这些指针指向,所以下面我都是不用中间变量的。

   void ListPushFront(ListNode* pHead, LTDataType x)

    {
        assert(pHead);
        ListNode* newnode = AollocListNode(x);
        ListNode* t = pHead->_next;//记录原第一个节点
        newnode->_next = t;
        t->_prev = newnode;
        pHead->_next = newnode;
        newnode->_prev = pHead;
    }


  尾插

  尾插:的下一个指向的是头节点,一种是靠pHead,另一种是靠原最后一个节点去找头节点。组合方式很多不一一列举,不创建另外的变量记录原最后一个节点时,原最后一个节点改变指向之前,头节点的指向不能动。

   void ListPushBack(ListNode* pHead, LTDataType x)

    {
        assert(pHead);
        ListNode* newnode = AollocListNode(x);
        newnode->_next = pHead;
        newnode->_prev = pHead->_prev;
        
        pHead->_prev->_next = newnode;//原最后一个节点 指向新节点
        pHead->_prev = newnode;//头节点指向新节点
    }


  删除 头删

  这个很简单了,就是刚刚销毁操作,不带循环,当个快乐的cv工程师吧。

   void ListPopFront(ListNode* pHead)

    {
        assert(pHead);
        ListNode* t = pHead->_next;//记录第一个节点
        pHead->_next = t->_next;
        t->_next->_prev = pHead;
        free(t);
    }

  尾删

  也很简单,记录最后一个节点,在将倒数第二个节点和头节点相互连接,再去释放。

   void ListPopBack(ListNode* pHead)

    {
        assert(pHead);
        ListNode* t = pHead->_prev;//记录最后一个节点
        t->_prev->_next = pHead;
        pHead->_prev = t->_prev;
        free(t);
    }

  找并返回地址,pos接收

  通过data找节点,再把节点的地址返回。在通过这个pos执行下面两个操作。

  循环结束的条件是回到了头节点。

   ListNode ListFind(ListNode pHead, LTDataType x)

    {
        assert(pHead);
        ListNode* p = pHead->_next;//指向第一个节点
        while (p != pHead)
        {
            if (x == p->_data)
                return p;
            p = p->_next;
        }
        return NULL;
    }

  在pos前插入

  双向单链表的优势来了,只要知道地址,不需要遍历,可以直接插入,时间复杂度O(1)。如果是单向的,还要找前驱。

  需要对pos判断一下不能为NULL

   void ListInsert(ListNode* pos, LTDataType x)

    {
        assert(pos);
        ListNode* newnode = AollocListNode(x);
        newnode->_next = pos;
        newnode->_prev = pos->_prev;
        pos->_prev->_next = newnode;
        pos->_prev = newnode;
    }

  删除在pos前的节点

  删除的时同样要注意pos不能为NULL。不能删除头节点单循环链表,不然主函数中的头指针会非法访问。

   void ListErase(ListNode pos, ListNode pHead)

    {
        assert(pos);
        assert(pos->_prev != pHead);//判断pos前一个节点不为头节点
        ListNode* t = pos->_prev;//记录pos前一个节点
        t->_prev->_next = pos;
        pos->_prev = t->_prev;
        free(t);
    }

  .c

   #include"HDLoopList.h"

    //开辟+初始化节点
    ListNode* AollocListNode(LTDataType x)
    {
        ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
        newnode->_data = x;
        newnode->_next = newnode;
        newnode->_prev = newnode;
        return newnode;
    }
    void ListPrint(ListNode* pHead)
    {
        ListNode* p = pHead->_next;//指向第一个节点
        while (p != pHead)
        {
            printf("%d->", p->_data);
            p = p->_next;
        }
        printf("NULL\n");
    }
    void ListDestory(ListNode* pHead)
    {
        assert(pHead);
        while (pHead->_next != pHead)
        {
            struct ListNode* t = pHead->_next;//先记录要被释放的节点(第一个节点)
            pHead->_next = t->_next;
            t->_next->_prev = pHead;
            free(t);
        }
        free(pHead);
    }
    void ListPushFront(ListNode* pHead, LTDataType x)
    {
        assert(pHead);
        ListNode* newnode = AollocListNode(x);
        newnode->_prev = pHead;
        newnode->_next = pHead->_next;
        pHead->_next->_prev = newnode;
        pHead->_next = newnode;
        //newnode->_next->_prev = newnode;
    }
    //void ListPushFront(ListNode* pHead, LTDataType x)
    //{
    //    ListNode* newnode = AollocListNode(x);
    //    ListNode* t = pHead->_next;
    //
    //    newnode->_next = t;
    //    t->_prev = newnode;
    //
    //    pHead->_next = newnode;
    //    newnode->_prev = pHead;
    //}
    void ListPushBack(ListNode* pHead, LTDataType x)
    {
        assert(pHead);
        ListNode* newnode = AollocListNode(x);
        newnode->_next = pHead;
        newnode->_prev = pHead->_prev;
        
        pHead->_prev->_next = newnode;//原最后一个节点 指向新节点
        pHead->_prev = newnode;//头节点指向新节点
    }
    void ListPopFront(ListNode* pHead)
    {
        assert(pHead);
        ListNode* t = pHead->_next;//记录第一个节点
        pHead->_next = t->_next;
        t->_next->_prev = pHead;
        free(t);
    }
    void ListPopBack(ListNode* pHead)
    {
        assert(pHead);
        ListNode* t = pHead->_prev;//记录最后一个节点
        t->_prev->_next = pHead;
        pHead->_prev = t->_prev;
        free(t);
    }
    ListNode* ListFind(ListNode* pHead, LTDataType x)
    {
        assert(pHead);
        ListNode* p = pHead->_next;//指向第一个节点
        while (p != pHead)
        {
            if (x == p->_data)
                return p;
            p = p->_next;
        }
        return NULL;
    }
    void ListInsert(ListNode* pos, LTDataType x)
    {
        assert(pos);
        ListNode* newnode = AollocListNode(x);
        newnode->_next = pos;
        newnode->_prev = pos->_prev;
        pos->_prev->_next = newnode;
        pos->_prev = newnode;
    }
    void ListErase(ListNode* pos, ListNode* pHead)
    {
        assert(pos);
        assert(pos->_prev != pHead);//判断pos前一个节点不为头节点
        ListNode* t = pos->_prev;//记录pos前一个节点
        t->_prev->_next = pos;
        pos->_prev = t->_prev;
        free(t);
    }

  test.c

#include"HDLoopList.h" int main() { ListNode* phead = NULL; phead = AollocListNode(0); for (int i = 1; i [1]: https://xuan.ddwoo.top/index.php/archives/372/ [2]: https://xuan.ddwoo.top/index.php/archives/368/  
本文来自投稿,不代表本站立场,如若转载,请注明出处:http://xuan.ddwoo.top/index.php/archives/374/
-- 展开阅读全文 --
js 怎么使用正则表达式-JavaScript正则表达式常用技巧
« 上一篇 11-23
几天成为淘宝美工-想学淘宝美工设计,大概多久才能学会?
下一篇 » 11-24
------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

发表评论

本站已加入互联网信息服务许可,请规范您的言行哦~

V注册会员 L评论等级
R34 条回复
  1. 2023-05-22     Win 8.1 /    Chrome

    Nicely put. Thanks.
    [url=https://researchproposalforphd.com/]buy research paper[/url] parts of a research proposal [url=https://writingresearchtermpaperservice.com/]buy a research paper[/url] writing proposal

  2. 2023-05-21     Win 7 /    Chrome

    Thanks, I appreciate this.
    [url=https://service-essay.com/]college paper writing service[/url] college paper writing service [url=https://custompaperwritingservices.com/]paper writing services[/url] best college paper writing service

  3. 2023-05-21     Win 7 /    Chrome

    Factor certainly utilized!!
    [url=https://theessayswriters.com/]help me write my essay[/url] website that writes essays for you [url=https://bestcheapessaywriters.com/]who can write my essay[/url] essay writter

  4. 2023-05-20     Win 8 /    Chrome

    You said it adequately..
    [url=https://writinganessaycollegeservice.com/]writing essay[/url] writing an essay [url=https://essayservicehelp.com/]essay writing site[/url] writing a descriptive essay

  5. 2023-04-30     Win 10 /    Chrome

    Nicely put, With thanks!
    [url=https://payforanessaysonline.com/]buy essays[/url] buy college essays [url=https://buycheapessaysonline.com/]pay someone to write your paper[/url] pay someone to write paper

  6. 2023-04-29     Win 8.1 /    Chrome

    Thanks a lot. Wonderful information!
    [url=https://phdthesisdissertation.com/]dissertation abstract[/url] proquest dissertations [url=https://writeadissertation.com/]dissertation writing service[/url] buy dissertations

  7. 2023-04-28     Win 10 /    Chrome

    Cheers! Very good stuff!
    [url=https://bestpaperwritingservice.com/]custom paper[/url] custom research paper writing services [url=https://bestonlinepaperwritingservices.com/]pay for papers[/url] best college paper writing service

  8. 2023-04-27     Win 10 /    Chrome

    Wow many of terrific facts!
    [url=https://researchproposalforphd.com/]research paper services[/url] proposal essay [url=https://writingresearchtermpaperservice.com/]college term papers[/url] term papers

  9. 2023-04-26     Win 7 /    Chrome

    Thanks, I like this.
    [url=https://essaytyperhelp.com/]paperhelp[/url] best essay writing service [url=https://helptowriteanessay.com/]essay helper[/url] how to write a college essay

  10. 2023-04-25     Win 7 /    Chrome

    Fantastic postings. Thanks!
    [url=https://dissertationwritingtops.com/]dissertation assistance[/url] dissertation def [url=https://helpwritingdissertation.com/]dissertation writer[/url] writing dissertations

  11. 2023-04-24     Win 7 /    Chrome

    Fantastic information, With thanks.
    [url=https://customthesiswritingservice.com/]thesis sentence[/url] tentative thesis [url=https://writingthesistops.com/]thesis creator[/url] define thesis statement

  12. 2023-04-24     Win 7 /    Chrome

    Kudos. Quite a lot of material!
    [url=https://writinganessaycollegeservice.com/]online check writing service[/url] best executive resume writing service [url=https://essayservicehelp.com/]college essay writing[/url] write my essay service

  13. 2023-04-23     Win 7 /    Chrome

    Amazing facts. With thanks!
    [url=https://ouressays.com/]college term papers[/url] term paper help [url=https://researchpaperwriterservices.com/]proposal writing[/url] research proposal apa

  14. 2023-04-23     Win 8.1 /    Chrome

    Excellent postings. Cheers.
    [url=https://writinganessaycollegeservice.com/]essay writing service dublin[/url] essay writing service buy [url=https://essayservicehelp.com/]essay writing site[/url] essay writing service uk cheap

  15. 2023-04-23     Win 7 /    Chrome

    Nicely put, Thank you!
    [url=https://phdthesisdissertation.com/]writing dissertation[/url] dissertation writer [url=https://writeadissertation.com/]define dissertation[/url] writing a dissertation

  16. 2023-04-22     Win 10 /    Chrome

    Fantastic info. With thanks!
    [url=https://bestpaperwritingservice.com/]custom papers[/url] research paper writing service [url=https://bestonlinepaperwritingservices.com/]paper writer services[/url] buy college paper

  17. 2023-04-22     Win 7 /    Chrome

    You made your stand very effectively!.
    [url=https://essayssolution.com/]what should i write my persuasive essay on[/url] ai essay writer [url=https://cheapessaywriteronlineservices.com/]write my essay for cheap[/url] write an essay for me free

  18. 2023-04-21     Win 10 /    Chrome

    You revealed this fantastically.
    [url=https://payforanessaysonline.com/]pay to get essay written[/url] buy custom essays online [url=https://buycheapessaysonline.com/]pay someone to do your essay[/url] pay to get essays written

  19. 2023-04-21     Win 10 /    Chrome

    Really lots of valuable material!
    [url=https://homeworkcourseworkhelps.com/]i do my homework in spanish[/url] do my finance homework [url=https://helpmedomyxyzhomework.com/]coursework uk[/url] hire someone to do my homework

  20. 2023-04-20     Win 10 /    Chrome

    Useful write ups. Regards!
    [url=https://quality-essays.com/]pay for my essay[/url] buy essay writing [url=https://buyanessayscheaponline.com/]order essay online[/url] order of writing an essay

  21. 2023-04-20     Win 7 /    Chrome

    Regards! Great stuff!
    [url=https://writinganessaycollegeservice.com/]best resume writing service[/url] cheap custom writing service [url=https://essayservicehelp.com/]best resume writing service 2019[/url] professional essay writing service

  22. 2023-04-19     Win 8.1 /    Chrome

    Helpful knowledge. Thank you.
    [url=https://essaypromaster.com/]online essay writer[/url] online research paper writer [url=https://paperwritingservicecheap.com/]write my papers[/url] website that writes papers for you

  23. 2023-04-19     Win 10 /    Chrome

    Superb advice. Thank you.
    [url=https://essaypromaster.com/]paper writer services[/url] paper writing services [url=https://paperwritingservicecheap.com/]write a paper for me[/url] college paper writers

  24. 2023-04-18     Win 8.1 /    Chrome

    You expressed this perfectly.
    [url=https://quality-essays.com/]buy an essay online reviews[/url] buy essay writing [url=https://buyanessayscheaponline.com/]pay to do essay[/url] college essay writer for pay

  25. 2023-04-17     Win 8.1 /    Chrome

    Very good information, Appreciate it.
    [url=https://essaypromaster.com/]paper writing website[/url] automatic essay writer [url=https://paperwritingservicecheap.com/]write my college paper for me[/url] professional essay writers

  26. 2023-04-16     Win 8.1 /    Chrome

    You expressed it really well!
    [url=https://theessayswriters.com/]write my essay for me cheapdo my essay for me[/url] writing argumentative essay [url=https://bestcheapessaywriters.com/]write my essay 4 me[/url] write my essay free online

  27. 2023-04-15     Win 10 /    Chrome

    You said it very well!
    [url=https://argumentativethesis.com/]thesis topic[/url] best master thesis writing service [url=https://bestmasterthesiswritingservice.com/]phd thesis defense[/url] medical thesis writing service india

  28. 2023-04-15     Win 7 /    Chrome

    Nicely put. Kudos.
    [url=https://ouressays.com/]proposal paper[/url] buying a research paper [url=https://researchpaperwriterservices.com/]writing research proposal[/url] research proposal in education

  29. 2023-04-14     Win 8.1 /    Chrome

    With thanks. A good amount of facts!
    [url=https://homeworkcourseworkhelps.com/]pay someone to do my math homework[/url] homework [url=https://helpmedomyxyzhomework.com/]i do my homework in spanish[/url] do my finance homework

  30. 2023-04-14     Win 8.1 /    Chrome

    You made your point very well!!
    [url=https://customthesiswritingservice.com/]thesis binding[/url] customer service thesis pdf [url=https://writingthesistops.com/]thesis writing service reviews[/url] customer service thesis

  31. 2023-04-10     Win 10 /    Chrome

    You said it adequately.!
    buy essay custom [url=https://seoqmail.com/]pay for essay papers[/url]

  32. 2023-04-09     Win 8.1 /    Chrome

    With thanks, Ample postings!
    resorts online casino nj [url=https://bestonlinecasinoreal.us/]lucky 7 casino online[/url] online casino best payouts

  33. 2023-04-08     Win 10 /    Chrome

    Information effectively regarded!!
    best content writing websites https://bestmasterthesiswritingservice.com academic writing help https://writingresearchtermpaperservice.com

  34. 2023-04-08     Win 7 /    Chrome

    Many thanks, A lot of forum posts!
    [url=https://argumentativethesis.com/]thesis writing service in singapore[/url] thesis statement examples for customer service [url=https://bestmasterthesiswritingservice.com/]strong thesis statement[/url] samle thesis

没有更多评论了

作者信息

热门文章

珍惜时间哦~

今日一言

- -
加载中...
换一句

标签TAG

热评文章