# C + + Linked list Learning notes

Source: Internet
Author: User

If you want to save some variables of the same data type, such as n variables of type int, it can be stored in an array and then easily accessed by subscript. But the array has a lot of drawbacks, the first is when declaring an array, the length of the array must be clear, even if the dynamic declaration of an array, the processor must know the length of memory in order to find a contiguous memory to save your variables. The second disadvantage is that in the previous sentence, the array in memory address must be continuous, so that the first address of the array can be based on the subscript to find the offset, quickly access the contents of the array. Now the computer memory is getting bigger, which is not a disadvantage. The third disadvantage, which is also very difficult to overcome, is that inserting an element into an array is very laborious. For example, an array of length n, you want to insert an element in the array subscript 0, in the case of no additional memory, you need to move the elements in the array to one bit, and discard the end element, this time overhead is very large, time complexity is O (n).

The improved version of the array is vector vectors, it is good to overcome the fixed length of the array of shortcomings, the length of the vector can be dynamically increased. If we understand the internal implementation of vectors, then we know that the internal implementation of the vector is an array, but when the number of elements exceeds the vector length, the principle of multiplication or exponential growth will be applied, the original data will be copied past, and then release the original memory.

If your data does not need to be inserted into an array, then the array is a good choice, and if your array of elements is dynamically growing, then the vector will be satisfied.

The list is good to overcome the shortcomings of the array, it does not need continuous memory in memory, insert or delete operation O (1) time complexity can be solved, the length is also dynamic growth. If your element requires frequent insertions and deletions, then the list is a good choice. is the organization of arrays and linked lists in memory

You can see that the array is contiguous in memory, with the start address plus the offset address can be directly out of the elements, the starting address is the array name, the offset address is the array subscript index*sizeof (t), T is the type of the array.

But the list in memory is not continuous, why is called linked list, it is felt like a chain of nodes to string up. So a node is how to string up, is the pointer, each node (except the end node) contains a pointer to the next node, that is, the pointer holds the first address of the next node, so that the number 1th node knows the number 2nd node is saved at what address, 2nd node know what the 3rd node is saved at what address ... And so on Just like the real treasure hunt, you only know the No. 1th Treasure Point, so you first reach the 1th treasure Point, 1th treasure Point you will get the address of the Treasure Point 2nd, and then you reach the 2nd treasure point ... Until you find the treasure you need. The traversal of a linked list is the principle.

Linked lists are beyond the scope of the base data type, so use a linked list, either using an STL library, or implementing a linked list yourself with a basic data type. If it is the normal use of programming, of course, recommend the former, if you want to really understand the data structure of the linked list, or recommend the latter. That not only knows the API provided by the standard library, but also knows the implementation principle inside the data structure. One of the benefits of this is that when you are programming, especially when the complexity of the time space is more sensitive to the program, you can choose the most suitable data structure according to the requirements, improve the efficiency of the program operation.

Each node is connected sequentially, which forms a linked list. Obviously this node is very critical, assuming that we want to construct an int type of linked list, then a node needs to contain two elements, one is the current node is saved by the value, set to int value. The other is the pointer to the next node, and then we assume that the node class is the nodes, then this pointer is node *next. This must not be an int *next. Because the next element that the pointer points to is an instance of a class, not data of type int. So the simplest implementation of node's class is as follows

`Class Node{public:int Value;node *next;node () {value = 0;next = NULL;}};`
The class name is node, contains two elements, one is the value of the current node, the other is a pointer to the downward one, and a constructor that initializes value to 0 and next to null.

After getting this class, suppose we generate two instances of this class, Node1 and Node2, and then point Node1 's next pointer to Node2, which forms a list of two elements. So if we get the Node1 node, we can access the Node2 through the next pointer. For example, the following code

`#include <iostream>using namespace Std;class node{public:int value;node *next;node () {value = 0;next = NULL;}}; int main () {node Node1,node2;node1.value = 1;node2.value = 2;node1.next = &node2;cout << (node1.next)->value << Endl;}`
Mr. Node1 is an instance of the two node classes, and Node2, respectively, initializes their value to 1 and 2. Use the & operator to remove the first address of the Node2 and assign the value to Node1.next. So Node1 's next pointer points to the NODE2. So we can get the next pointer to Node1 and access the value of the Node2 by the "--" operator, the output is 2.

Make a slight modification to the code you just made

`#include <iostream>using namespace Std;class node{public:int value;node *next;node () {value = 0;next = NULL;}}; int main () {node Node1,node2;node1.value = 1;node2.value = 2;node1.next = &node2;cout << sizeof (node) << en Dl;cout << &node1 << endl;cout << &node2 << Endl;}`

The output is like this

So we can draw a very detailed diagram of them in memory based on the output.

This makes up a simple list, if there are new nodes, then the same, chain in the footer or the table head, of course, inserted in the middle is no problem.
But then there is the problem is that Node1 and Node2 are we declared well in advance, and know the names of these two instances, if we need 1000 or even more nodes, this way is obviously unscientific, and in many cases, we are dynamically generated an instance of a class, the return is the first address of this instance. The following code we use a For loop, generate 11 nodes, strung together to form a linked list

`#include <iostream>using namespace Std;class node{public:int value;node *next;node () {value = 0;next = NULL;}}; int main () {node *head,*curr;head = new node (); head->next = Null;head->value = 15;for (size_t i = 0; i < i++) { Curr = new node (); curr->value = I;curr->next = Head;head = Curr;} cout << head->value << Endl;}`
The principle is that the husband becomes a head node, and then dynamically generates 10 nodes, each generating a node, points the node to the head node, and then updates the head node to the current point. To verify correctness, the value of the head node is output at the end of the program, which is 9.

So how to traverse the list, just beginning to say, traversing the list needs to start from beginning to end, access to each element, until the tail of the chain. That is to say, continue to access the current node's next, until null. The following is the traversal output of the linked list

`#include <iostream>using namespace Std;class node{public:int value;node *next;node () {value = 0;next = NULL;}}; int main () {node *head,*curr;head = new node (); head->next = Null;head->value = 15;for (size_t i = 0; i < i++) { Curr = new node (); curr->value = I;curr->next = Head;head = Curr;} while (head!=null) {cout << head->value << endl;head = Head->next;}}`
Here are the output results

A very obvious advantage of a linked list relative to an array is the ability to complete a node insertion or deletion with time complexity O (1).

The principle of insert operation is very simple, assuming that there are now three nodes, one is the current node Curr, one is the next node of the current node, that is, the successor node, assuming next, there is a node to be inserted, assuming insert. The insert operation is to have the successor node of the current node point to the Insert node, and the subsequent node of the Insert node points to the next node. Below are

The principle of the delete operation is similar, that is, the successor node of the current node points to its successor node. As follows

So how do the insertions and deletions work with code, and we have the original linked list, first inserting a node with a value of 20, and outputting all the elements of the list. It then deletes the element in the list that has a value of 20, outputting the entire contents of the element. The code is as follows

`#include <iostream>using namespace Std;class node{public:int value;node *next;node () {value = 0;next = NULL;}}; int main () {node *head=null,*curr=null,//current node *insert=null,//insert node *next=null,//successor *pre=null;//predecessor node head = new node (); Head->next = Null;head->value = 15;for (size_t i = 0; i < i++) {Curr = new node (); curr->value = i;curr-> Next = Head;head = Curr;} Curr = head;//takes out the head node while (Curr->value! = 5) {Curr = Curr->next;} Locate the node with a value of 5 next = curr->next;//Find the successor of the node with a value of 5 Insert = new node ();//Generate a new nodes with a value of 20insert->value = 20;curr->next = insert;//the successor node of the current node is the insertion node Insert->next = next;//The successor node of the insertion node is the successor of the node with a value of 5 Curr = head;//iterates through the linked list, outputting each element while (Curr!=null) { cout << Curr->value<<endl;curr = Curr->next;} Curr = head;//Find the head node while (curr->value!=20) {pre = Curr;curr = Curr->next;} Find a node with a value of 20, note that now is a one-way list, each node does not save its predecessor node, so in the process of traversal to artificially save the precursor node next = curr->next;//Find the current node's successor (the current node is a value of 20 node) pre- >next = next;//The successor node of the current node is the successor of the current node delete curr;//Delete the current node Curr =head;//iterates through the list output for each element while (curr! = NULL) {cout << curr->value << endl;curr = Curr->next;} while (true) {}}`

The output is as follows:

As for the complete list, there are standard libraries in STL, there are very comprehensive APIs, as long as we know the internal implementation principle, it is very simple to invoke these APIs, it will be handy to use them.

C + + Linked list Learning notes

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.