In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article "C language how to achieve the inversion of single-linked list structure" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language how to achieve the inversion of single-linked list structure" article.
First, understand the pointer
It's not difficult to understand the structure of a linked list, but once you mix it with a pointer, it's easy to get confused. Therefore, if you want to write code for a linked list, you must first understand the pointer.
Some languages have the concept of "pointer", such as C language; some languages do not have pointers and are replaced by "references", such as Java and Python. Whether it is a "pointer" or a "reference", in fact, they all mean the same thing, the memory address where the referenced object is stored.
Assigning a variable to a pointer actually assigns the address of the variable to the pointer, or conversely, the pointer stores the memory address of the variable, points to the variable, and the variable can be found through the pointer.
P-> next=q. This code says that the next pointer in the p node stores the memory address of the Q node. P-> next=p- > next- > next. This line of code indicates that the next pointer of the p node stores the memory address of the next node of the p node.
The C language standard stipulates that the compiler always reads the definition of a symbol from its name and then parses it in order of priority. Yes, start with the name, not from the beginning or the end, which is the key to understanding complex pointers!
For beginners, the precedence of several operators is very confusing, and their precedence is from high to low:
The part of a definition enclosed in parentheses (). Suffix operator: parentheses () indicate that this is a function and square brackets [] indicate that this is an array. Prefix operator: the asterisk * means "pointer to xxx".
In this chapter, we only use secondary pointers at most, so we will explain secondary pointers. For example, what does int * * p mean?
First of all, look at * p. "*" indicates that P is a pointer. But a pointer to what?
Looking at the previous int*, int is an integer type followed by a "*" pointer to indicate the integer type.
* p is a pointer to an integer type pointer. P holds the address of the pointer of the integer type.
Watch out for pointer loss and memory leaks
I don't know if you have the feeling that when you write the linked list code, the pointer points back and forth, and you don't know where it is. Therefore, when we write, we must be careful not to lose the pointer. How do you often lose the pointer? Let me take the insertion of a single linked list as an example to analyze it for you.
How do you often lose the pointer? Let me take the insertion of a single linked list as an example to analyze it for you.
We want to insert node x between node an and adjacent node b, assuming that the current pointer p points to node a. If we change the code implementation to look like this, pointer loss and memory leaks will occur.
P-> next = x; / / point the next pointer of p to node x; x-> next = p-> next; / / point the next pointer of node x to node b
Beginners often make mistakes here. After completing the first step, the p-> next pointer no longer points to node b, but to node x. Line 2 is equivalent to assigning x to x-> next, pointing to itself. As a result, the whole linked list is broken in half, and all nodes after node b are inaccessible.
For some languages, such as C, memory management is the responsibility of the programmer, and memory leaks will occur if the memory space corresponding to the node is not manually released. Therefore, when we insert a node, we must pay attention to the order of operation, first point the next pointer of node x to node b, and then point the next pointer of node a to node x, so as not to lose the pointer and cause memory leakage. So, for the code we just inserted, we just need to reverse the order of lines 1 and 2. By the same token, when deleting linked list nodes, you must also remember to release memory space manually, otherwise, there will also be memory leaks. Of course, for programming languages like Java, where virtual machines manage memory automatically, you don't need to think so much about it.
3. C language realization of single linked list inversion
Use p to point to the first node, and cur to point to the current node, each time the cur- > next node is removed and placed in front of the p node. Then update the p node to point to the header node. The specific implementation code is as follows
Void revers_list (list1 * * l) {if (! (* l) | |! l) {exit (- 1);} list1 * start=*l; list1 * start_next=NULL; while (start- > next) {/ / get the successor node of the current node start_next= start- > next; / / remove the successor node 72 start- > next= start_next- > next / / raise the successor node to the front start_next- > next = * l; / / Update the header node * l = start_next }} the above is the content of this article on "how to realize the inversion of single-linked list structure in C language". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to 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: 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.