4.9 Linked list & state machine and multithreading

Source: Internet
Author: User

Introduction of 4.9.1 linked list 4.9.1.1, from the defect of array
    • (1) The array consists of two defects. One is that the types of all elements in the array must be identical. The number of elements in an array must be specified beforehand and cannot be changed once specified.
    • (2) How to solve the array of 2 defects: the first flaw of the array to solve the structural body. Structs allow the element types to be different, so the first flaw in the array is resolved. So structs are invented because arrays don't solve certain problems.
    • (3) How to solve the second flaw of the array? We want the size of the array to be extended in real time, such as when we first defined the number of elements at 10, and then when the program ran, it didn't feel enough to expand to 20. Ordinary arrays obviously not, we can encapsulate the array to achieve the purpose, but also can use a new data structure to solve, this new data structure is linked list
    • Summary: It is almost understandable that a linked list is an array in which the number of elements can become larger/smaller in real time.
Why 4.9.1.2 and University have a new campus
    • (1) When the school was first built, the definition and initialization of similar variables, because the next is full of wasteland, so the size of the school is determined by itself. But after the school was established, there were also other buildings (like, after the variable allocation, the adjacent area of the memory is assigned to the variable address) at this time, your campus development feeling is not enough to expand, but found that the neighbors are full, has been unable to expand, At this time school expansion has two ideas, the first is demolition, the second is the relocation, the third external expansion
    • (2) Demolition basically does not work, whether it is to show life or program running, because the cost is too high.
    • (3) relocation can be done. One idea in the program to solve the expansion of array size is the overall relocation. The specific idea is: first in the blank memory out of a large array of resumes, and then the original array of elements in the entire copy to the new array of the head. Then release the memory space of the original array, and replace our new array with the original array. This mutable array is not supported in the early C language, but in higher-level languages Java C + + is supported.
    • (4) External expansion is to do common, but also the most reasonable, his idea is the integration of 0, in the original fixed case, the expansion of new sub-base. External expansion in the school example is the new campus. The solution to solving array problems in programming is the list of the external extensions.
What does a 4.9.1.3 and a list look like?
    • (1) As the name implies, the list is a chain linked table.

The table here is only a node (each campus), the node has memory can be used to store data love (so called table, data table).

The chain here refers to the method of the chain tables, and the C language is used to connect 2 tables

    • (2) A linked list is made up of several nodes (the nodes of the list are similar), and the nodes are composed of valid data and pointers. A valid data region is used to store information to complete a task, and the pointer area is used to point to the next node in the list to form a linked list.
4.9.1.4, moments don't forget the list is for what?
    • (1) Always remember: The list is used to solve the problem of the size of the array can not be dynamically extended, so the list is actually used when the array. Straightforward point: The list can be completed tasks with the list can also be completed, with the array can be completed by the list of tasks can also be completed. But flexibility is not the same.
    • (2) Simple point: A linked list is used to store data. The advantage of a linked list for data storage relative to an array is flexibility, how many are allocated dynamically, and no additional memory is required. The advantage of arrays is that they are simple to use (simple and rude).
4.9.2, single-linked list implementation 4.9.2.1, single-linked list node composition
    • (1) A linked list is made up of nodes, and the nodes contain valid data parts and pointers.
    • (2) The defined struct node is only a struct, and it does not generate any variables, nor does it account for memory. struct definition is equivalent to a linked list node defined a template, but there is no node, the future son ah actually created Ang Lee indicates that a node needs to be copied with this template.
Application and use of 4.9.2.2, heap memory
    • (1) The memory requirements of the linked list is more flexible, can not use the stack, and can not be used in data segment
    • (2) To use heap memory to create a linked list node: 1, request heap memory, size is the size of a node (check whether the application results are correct). 2. Clean up the heap memory requested. 3, the application to the heap memory as a new node; 4. Fill in the valid data and pointer area of the new node
4.9.2.3, the head pointer of the linked list
    • (1) The head pointer is not a node, but a normal pointer, which accounts for only 4 bytes. The type of the head pointer is the struct node * type, so it can point to the node of the linked list
    • (2) The implementation of a typical list is: The head pointer points to the first node of the list, then the pointer in the first node points to the next node, and so on, and so on, until the last node. This makes up a chain
4.9.2.4, Combat: Build a simple single-linked list

(1) Objective: To construct a linked list that stores a data (for example, a three-by-one number) in a linked list.

4.9.3, single-linked list algorithm values insert node 4.9.3.1, continue to the previous section, Access data for each node in the linked list
    • (1) Only with the head pointer, not the individual bytes of their own pointers, because in practice, we save the list of nodes will not save the pointers, only through the head pointer to access the linked list node
    • (2) The Pnext pointer inside the previous node can help us find the next finger node
4.9.3.2 The code that creates the node is encapsulated into a function
    • (1) The key point of encapsulation is the design of the interface (function parameter and return value) of the function.
#include <stdio.h>#include<stdlib.h>#include<strings.h>structnode{intdata; structNode *Pnext;};structNode * Creat_node (intdata) {    structNode * p = (structNode *)malloc(sizeof(structnode)); if(NULL = =p) {printf ("malloc Error"); returnNULL; } bzero (P,sizeof(structnode)); P->data =data; P->pnext =NULL; returnp;}intMainvoid){    structNode * Pheader =NULL; Pheader= Creat_node (1); Pheader->pnext = Creat_node (2); Pheader->pnext->pnext = Creat_node (3); printf ("Pheadef->data =%d.\n", pheader->data); printf ("Pheader->pnext.data =%d.\n", pheader->pnext->data); printf ("Pheader->pnext->pnext.data =%d.\n", pheader->pnext->pnext->data); return 0;}

4.9.3.3, inserting a new node from the list header

4.9.3.4, inserting a new node from the tail of the list

(1) The tail insert list is simple, because the previously established

structnode{intdata; structNode *Pnext;};//Create a nodestructNode * Creat_node (intdata) {    structNode * p = (structNode *)malloc(sizeof(structnode)); if(NULL = =p) {printf ("malloc Error"); returnNULL; } memset (P,0,sizeof(structnode));//the heap memory for the application cleared 0//bzero (p, sizeof (struct node));P->data =data; P->pnext =NULL; returnp;}//Insert from TailintInsert_tail (structNode * Pheader,structNode *New){    structNode * p =Pheader; if(NULL! = p->Pnext) {P= p->Pnext; } P->pnext =New;}intMainvoid){    structNode * Pheader = Creat_node (1); Insert_tail (Pheader, Creat_node (2)); Insert_tail (Pheader, Creat_node (3)); printf ("Pheader->data =%d.\n", pheader->data); printf ("Pheader->pnext->data =%d.\n", pheader->pnext->data); printf ("Pheader->pnext->pnext->data =%d.\n", pheader->pnext->pnext->data); return 0;}
Headless Node Code
#include <stdio.h>#include<stdlib.h>#include<string.h>structnode{intdata; structNode *Pnext;};//Create a nodestructNode * Creat_node (intdata) {    structNode * p = (structNode *)malloc(sizeof(structnode)); if(NULL = =p) {printf ("malloc Error"); returnNULL; } memset (P,0,sizeof(structnode));//the heap memory for the application cleared 0//bzero (p, sizeof (struct node));P->data =data; P->pnext =NULL; returnp;}//Insert from TailintInsert_tail (structNode * Pheader,structNode *New){    intCNT =0; structNode * p =Pheader;  while(NULL! = p->Pnext) {P= p->Pnext; CNT++; } P->pnext =New; Pheader->data = cnt +1;}intMainvoid){    structNode * Pheader = Creat_node (0); Insert_tail (Pheader, Creat_node (1)); Insert_tail (Pheader, Creat_node (2)); Insert_tail (Pheader, Creat_node (3)); printf ("beader Node data =%d.\n", pheader->data); printf ("node1 data =%d.\n", pheader->pnext->data); printf ("node2 data =%d.\n", pheader->pnext->pnext->data); printf ("node3 data =%d.\n", pheader->pnext->pnext->pnext->data); return 0;}
There's a head node code.4.9.4, single-linked list of the insertion node continued 4.9.4.1, detailed Jie Linqu head Insert Function 4.9.4.2, what is the head node

(1) Problem: Because we directly default in Insert_tail, there is a node pointing to the head pointer, so if the header pointer is defined in the program, a insert_tail error occurs. We have to define the head pointer after the first creat_node after the creation of a new node to the head pointer initialization, otherwise we can not avoid this error, but this solution makes the program seems a bit less logical, it seems that the first node and other nodes are a little different, it seems a bit of an alternative.

(2) Another use of the list is that the first node pointed to by the head pointer is used as a head junction. The head node is characterized by: first, he is immediately behind the head pointer. Second, the data portion of the head node is empty (sometimes not empty, but the number of nodes that store the entire list). The pointer part points to the next node, which is the first node.

(3) It seems that the head node is really not the same as the other nodes. We also have different ways to add nodes when creating a linked list. The head node is created and associated with the head pointer when the head pointer is created, and the subsequent real node that stores the data is done with the function added by the node, such as Insert_node

(4) The list has no head node is different, reflected in the list of inserted nodes, delete nodes, traverse nodes, parse the list of the various algorithm functions are not too. So if a list is designed with a head node, then all the algorithms behind it should be treated like this. If the design of the time there is no head node, then all the algorithms should be followed by no head node to do, the actual programming, both nodes are used, so in the actual programming, one should to see whether others have used the head node.

4.9 Linked list & state machine and multithreading

Contact Us

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.