In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to interpret and analyze the C++ linked list, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
C++ language is a good learning tool for learning data structures. If you can fully understand the function and use of C++ linked lists in C++, it will be easy to understand its C++ description and Java description, and it will not be difficult to learn any language in the future.
The meaning of the exchange node of a single linked list is that given a single linked list, any two nodes in it are required to be exchanged. Note that the header node of the linked list does not participate in the node exchange. This looks relatively simple, but it still requires some basic skills to achieve it.
For this problem, the key is to use four pointers to save the front and back node positions of the two swapped nodes. For specific implementation, see the implementation source code. In fact, there is a more logical implementation: simply use two pointers to save the previous node of the current two swap nodes.
Then delete the nodes to be exchanged in turn, and then alternately insert the deleted two nodes after the previous node of the record, that is, actually convert this process into the two basic operations for the C++ linked list. However, it should be noted that in this implementation, there will be problems when the two switching nodes are adjacent nodes, which should be dealt with separately, and the specific reasons can be known by manual operation. The latter method is not given here.
What should be explained in the implementation code is that the exchange C++ linked list node passes in two exchange node pointers, but in order to test the simple implementation, the two nodes are replaced with the keywords (value range) of the node to be exchanged, and then locate in the C++ linked list.
The specific implementation source code is:
/ / Link.h # include # include struct Node {public: Node (): _ val (0), _ next (NULL) {} Node (int val): _ val (val), _ next (NULL) {} Node (int val,Node* next): _ val (val), _ next (next) {} ~ Node () {if (_ next) delete _ next;} public: int _ val; Node* _ next;}; typedef Node* LinkNode Node* CreateLink (int len,int MAX_BOUND = 100) {srand ((unsigned int) time (NULL)); LinkNode head = new Node (- 1); LinkNode tmp = head; for (int I = 0; I
< len; ++i) { //tmptmp = tmp->_ next = new Node (rand ()% MAX_BOUND); tmptmp = tmp- > _ next = new Node (I);} tmp- > _ next = NULL; return head;} void ExchLinkNode (const LinkNode head,int i1 minint i2) {/ / head is not allowed to be exchanged LinkNode prenode1 = NULL; / / save the previous node of the node1 to be exchanged LinkNode postnode1 = NULL; / / save the last node of the node1 to be exchanged LinkNode prenode2 = NULL / / save the previous node of the node2 to be exchanged LinkNode postnode2 = NULL; / / save the latter node of the node2 to be exchanged LinkNode node1 = NULL; / / save the node to be exchanged LinkNode node2 = NULL; / / save the node to be exchanged LinkNode tmp = head; / / locate the two nodes while ((tmp- > _ val! = i1) & & (tmp! = NULL)) {tmptmp = tmp- > _ next } if (tmp = = NULL) {return;} else {node1 = tmp;} tmp = head; while ((tmp- > _ val! = i2) & & (tmp! = NULL)) {tmptmp = tmp- > _ next;} if (tmp = = NULL) {return;} else {node2 = tmp;} / / may not exchange if (node1 = = head) {return;} else if (node2 = = head) {return } / / you don't have to exchange if (node1 = = node2) {return;} tmp = head; while (tmp- > _ next! = node1) {tmptmp = tmp- > _ next;} prenode1 = tmp; tmp = head; while (tmp- > _ next! = node2) {tmptmp = tmp- > _ next;} after reading the above, have you mastered how to interpret and analyze the C++ linked list? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.