Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ take the lead in two-way circular linked list

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how C++ can take the lead in a two-way circular linked list". The content is simple and clear. I hope it can help you solve your doubts. Next, let the editor lead you to study and learn the article "how C++ can take the lead in two-way circular linked list".

What is a leading two-way circular linked list

What is taking the lead? Both ways? Cycle? (lead two-way circular linked list)

Lead: indicates that there is a sentinel node in the linked list, that is, the header node, which does not store any valid data!

Bidirectional: each node has two pointers to its previous node and its second node!

Loop: the last node next no longer points to NULL points to the Sentinel node, the Sentinel node prev points to the last node! (if it is a single linked list, the sentry does not point to the last node!)

Lead two-way circular linked list: the structure is the most complex and is generally used to store data separately. The linked list data structure used in practice is to take the lead in two-way circular linked list. In addition, although the structure of this structure is complex, but the use of code implementation will find that the structure will bring a lot of advantages, but the implementation is simple, after our code implementation will know!

Common interface implementation of leading two-way circular linked list

? Build the structure of the leading two-way circular linked list!

? Lead the initialization of the two-way circular linked list and create a new node!

? Insert the head into the node!

? Insert the tail into the node!

? Head delete node!

? Delete the node at the tail!

? Insert node in front of pos node!

We first need to find the address of the pos node!

ListNode* ListFind (ListNode* phead, LTDataType x) {assert (phead); ListNode* cur = phead- > next; while (cur! = phead) {if (cur- > data = = x) {return cur;} cur = cur- > next;} return NULL;}

? Delete the specified pos node!

After reading so many diagrams above, I believe it is very easy for you to delete the specified node. I just went straight to the code!

Void ListErase (ListNode* pos) {assert (pos); ListNode* prev = pos- > prev; ListNode* next = pos- > next; prev- > next = next; next- > prev = prev; free (pos); pos = NULL;}

? Finally, don't forget to destroy the linked list!

Void ListDestory (ListNode* phead) {ListNode* cur = phead- > next; while (cur! = phead) {ListNode* next = cur- > next; free (cur); cur = next;} free (phead); phead = NULL;}

In fact, many friends have a headache at the sight of the data structure, but I would like to say, do not disrelish the trouble of drawing, must draw more pictures, this is more conducive to our implementation of the code, relying on the head fantasy is unimaginable!

These are all the contents of the article "how C++ can take the lead in two-way circular linked list". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 295

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report