Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Chain Storage

Chain storage: stores data elements in a linear table with any group of storage units. Linear table stored in this method is short for Linear Linked List.

Any group of storage units of nodes in the storage linked list can be continuous or discontinuous, or even scattered in any location in the memory. The logical and physical order of nodes in the linked list is not necessarily the same. (Logical adjacent elements are not required to be physically adjacent)

To correctly represent the logical relationship between nodes, the address (or location) indicating the direct successor node must be stored while each node value is stored. It is called a pointer) or link. These two parts constitute the storage image of the data element ai.
The linked list links n nodes of a linear table in the logical order through the pointer field of each node.
Each node only contains a linked list of pointer fields, called a single-chain table.

For ease of operation, a header node (Head pointer) is always attached before the first node of the linked list) head points to the first node (that is, the pointer field of the header node stores the storage location of the first node ). The data field of the header node does not store any information (or information such as the length of the linked list ).

The pointer to the last node in the linear linked list is NULL because the last data element has no direct successor ).

For ease of operation, a header node (Head pointer) is always attached before the first node of the linked list) head points to the first node (that is, the pointer field of the header node stores the storage location of the first node ). The data field of the header node does not store any information (or information such as the length of the linked list ).

The pointer to the last node in the linear linked list is NULL because the last data element has no direct successor ).

Node description and implementation
The type of struct with pointers is described in C language.
Typedef struct Lnode {ElemType data;/* data field, save the value of the node */struct Lnode * next;/* pointer field, type: struct Lnode */} LNode, * LinkList; /* node type name */LNode, node structure; LinkList: linked list structure // LinkList L; L is the name of a single-chain table, and can also be used as the table's header pointer name, point to the first node in the table, that is, the address of the first node in the pointer field of L. // If L is null, it indicates that the linear table is empty and its length is 0.

Node implementation
Nodes are implemented through dynamic allocation and release, that is, they are allocated when needed and released when not needed. The standard functions provided by the C language are: malloc (), realloc (), sizeof (), and free ().

Dynamic Allocation of p = (LNode *) malloc (sizeof (LNode ));
The malloc function allocates space for a node variable of the LNode type and puts its first address in the pointer Variable p.

Dynamic Release of free (p );
The system recycles the memory zone pointed by the pointer Variable p. P must be the last return value when calling the malloc function.

Algorithm Description: The LNode * create_LinkList (void)/* Header insertion method creates a single-chain table. The head of the head node of the chain table is used as the return value */{int data; LNode * head, * p; head = (LNode *) malloc (sizeof (LNode); head-> next = NULL;/* Create the head node of the linked list */while (1) // keep running {scanf ("% d", & data); if (data = 32767) break; p = (LNode *) malloc (sizeof (LNode )); p-> data = data;/* data field assignment */p-> next = head-> next; head-> next = p;/* hook chain, the newly created node is always the first node */} return (head);} // The returned value is the header node head, which is actually the created linked list. In this case, you only need to create an LNode * type variable in the main function to call this function. Such as LNode * head; or LinkList head; head = create_LinkList ();
Create Table by inserting

Although the algorithm for creating a linked list by using the header insertion method is simple, the node sequence in the generated linked list is the same as that of the input table. If you want the two to be in the same order, you can use the tail plug-in method to create a table.

This method inserts a new node into the end of the table of the current linked list to make it the end node of the current linked list.

Algorithm Description: LNode * create_LinkList (void)/* create a single-chain table by means of tail insertion. The head of the head node of the chain table is used as the return value */{int data; LNode * head, * p, * q; head = p = (LNode *) malloc (sizeof (LNode); p-> next = NULL;/* Create the header node head of a single-chain table */while (1) {scanf ("% d", & data); if (data = 32767) break; q = (LNode *) malloc (sizeof (LNode )); q-> data = data;/* data field assignment */q-> next = p-> next; p-> next = q; p = q;/* hook chain, the newly created node is always the last node */} return (head );}
Regardless of the insertion method, if you want to insert n nodes to create a single-chain table, the time complexity of the algorithm is O (n ). For a single-chain table, no matter which operation, as long as the hook chain (or re-hook chain) is involved, if the direct successor is not explicitly given, the hook chain (or re-hook chain) the order must be "first right, then left"; otherwise, some nodes in the linked list will be lost.
Single-chain table search

(1) Search for the I element in the list receiving list by sequence number.
For a single-chain table, you cannot directly access the node by serial number I as in the sequence table. Instead, you can only start from the head node of the chain table and search for each node along the next node of the Chain Domain, until node I is found. Therefore, the Linked List is not a random access structure.
Set the length of a single-chain table to n. To find the I node in the table, the I value is valid only when 1 segment I limit n.

The algorithm IDEA is as follows:

Start from the first node and run the trace scanning command. Use the pointer p to point to the node currently scanned, and use j to calculate the number of scanned nodes. When p scans the next node, j automatically adds 1. The initial value of P points to the header node, and the initial value of j is 1. When j = I, the pointer p refers to the node I.
Algorithm Description ElemType Get_Elem (LNode * L, int I) {int j; LNode * p; p = L-> next; j = 1; /* point p to the first node */while (p! = NULL & j <I) {p = p-> next; j ++;}/* Move pointer p, j count */if (! P | j> I) return (ERROR);/* If p is NULL, it indicates I> n; j> I indicates I = 0 */else return (p-> data);} frequency of moving the pointer p: I = 0: 0; I, [1, n]: I-1 times; I> n: n times. Time complexity of compaction: O (n ).

Search by value
The value search is in the linked list. Do you want to find the nodes whose node value is equal to the given key? If yes, the storage location of the node whose value is key is found for the first time is returned; otherwise, NULL is returned. Start from the Start Node and compare the node values with the given value key one by one along the linked list.

Algorithm Description LNode * Locate_Node (LNode * L, int key) /* Find the first node with the key value in the single-chain table with the L header node */{LNode * p = L-> next; while (p! = NULL & p-> data! = Key) p = p-> next; if (p-> data = key) return p; else {printf ("the node to be searched does not exist !! \ N "); retutn (NULL) ;}} the Algorithm Execution is related to the parameter key, and the average time complexity is O (n ).

Related Article

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.