03. Linear table (2) chain storage structure. Single-chain table 1, chain-chain

Source: Internet
Author: User

03. Linear table (2) chain storage structure. Single-chain table 1, chain-chain

Typedef int ElemType;
Status GetElem (LinkList L, int I, ElemType * e) {int j; LinkList p; // declare a node p = p-> next; // Let the node p point to the first node j = 1 of the linked list L; while (p & j <I) // traverse from the first node of the linked list until j = I-1, let p point to the node where the storage I-1 location is located {p = p-> next; // Let p point to the next node j ++;} if (! P | j> I) return ERROR; * e = p-> data; // the query is successful, store the data of the I element in the linked list to the return OK space pointed to by pointer e;} Note: p is a newly defined node, while (p & j <I) the role is to make the node p from the first node of the linked list to traverse until j = I-1, let p point to the storage I-1 location of the node, if p is null at the end of the linked list, the I-th element does not exist.
(2) single-chain table insertionImplementation operation: ListInsert (LinkList * L, int I, ElemType e), insert Data Element e to the I position of a single-chain table L. algorithm idea: s-> next = p-> next; p-> next = s;. declare a node p pointing to the first node of the linked list and initialize j to traverse B from 1. when j <I, let the p pointer move backward and keep pointing to the next node. j accumulates 1 until p is null at the end of the linked list, it indicates that the I-th element does not exist. C. if p is a null node or the traversal position exceeds I, an exception d is thrown. if the search is successful,-generate an empty node in the System s-assign the data element e to s-> data; -Insert standard statement s-> next = p-> next, p-> next = s source code implementation for a single-chain table:/* initial condition: the ordered linear table L already exists, 1 = I <= ListLength (L) * operation structure: insert Data Element e to the I position of a single-chain table L */# define OK 1 # define ERROR 0 typedef int Status;
Typedef int ElemType;
Typedef int ElemType;
Status ListInsert (LinkList * L, int I, ElemType * e) {int j; LinkList p, q; // declare a node p = * L; j = 1; while (p-> next & j <I) // traverse from the first node of the linked list until j = I-1, let p point to the node where the storage I-1 location is located {p = p-> next; // Let p point to the next node j ++;} if (! P | j> I) return ERROR; q = p-> next; // set q to the next node p-> next = q-> next; // set the successor node of q to the successor node of p * e = q-> data; // send the data in the q node to e free (q ); // after the setting is complete, let the system reclaim this node and release the memory return OK ;}
Sublimation NOTE 2: Analysis of s, p, s-> data, s-> next, p-> next? (1) LinkList p, s: declare two nodes p and s, including the data domain and pointer domain; (2) s-> data: the data domain of node s, the value is a data (3) s-> next: pointer field of node s, and its value is the storage address of the next node (4) s-> next = p-> next: Assign the storage address of the next node of node p to the s node pointer field (5) p-> next = s; set node s to the next node of p. In fact, (4) (5) we can understand this: assume that the initial linked list is connected to two nodes p and q, that is ...... -p-q -......, now we need to insert a node s between node p and q. The method is as follows: since p-> next = q, s-> next = q // node s is followed by q p-> next = s // node p is followed by s, in this case, ...... -p-s-q -......
5. Performance Analysis of sequential Storage Structure(1) Search: the time complexity is O (1), and the worst case event complexity is O (2) (2) Delete and insert: the insertion and deletion of a single-chain table are mainly composed of two parts: the first part is to traverse the I-th element of the query; the second part is to insert and delete elements. From the whole algorithm, we can easily export the elements: their time complexity is O (n ). If we do not know the pointer position of the I-th element, the data structure of a single-chain table is in the insert and delete operations, and the sequence storage of the linear table is dismissed, there is no big advantage. However, if we want to insert 10 elements from the position I, for the sequential storage structure, it means that each insert needs to move n-I elements, each time it is O (n ). For a single-chain table, we only need to find the pointer at position I for the first time, which is O (n). Next, we simply move the pointer by assigning values, the time complexity is O (1 ). Conclusion: The more frequently you insert or delete data, the more efficient the single-chain table is.

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.