05. Linear table (4) chain storage structure. Static linked list, linear chain

Source: Internet
Author: User

05. Linear table (4) chain storage structure. Static linked list, linear chain
Chain Storage Structure-static linked listI. Static linked list1. Static linked list Storage StructureA single-chain table is implemented through pointers, but we can also use arrays to describe a single-chain table, that is, a static linked list. How to Implement static linked list? The element of the constructed array is composed of two data fields: data and cur. That is, each lower mark of the array corresponds to one data and one cur. Data Field data: used to store data elements, that is, the data to be processed. cursor cur: used to store the subscript of the element in the array, which is equivalent to the next pointer in a single-chain table. To facilitate data insertion, we usually create a larger array so that there is some free space without Overflow. Static linked list storage structure of a linear table: # define MAXSIZE 1000 // assume that the length of the linked list is 1000 (elements) typedef struct {ElemType data; // data field, int type int cur; // Cursor (Cursor). If it is 0, it indicates no point} Component, StaticLinkList (MAXSIZE );2. Backup linked listSince the first and last elements of the array are processed as special elements and no data is stored, we call unused array elements a backup linked list. Therefore, we stipulate that: (1) the cursor cur of the first element of the array (that is, the element with the subscript 0) stores the subscript of the first idle space element (the first element of the standby linked list ); (2) cursor cur of the last element of the array stores the subscript of the first element with a numerical value (equivalent to the header node in a single-chain table ). When the entire linked list is empty, the cursor cur of the last element is 0. (3) The last cur with a value element in the linked list is 0.

Sublimation Note: How to chain each component in a one-dimensional array list into a backup linked list? Typedef int StatusStatus InitList (StaticLinkList list) {int I; // I is the array subscript, MAXSIZE is the chain table length for (I = 0; I <MAXSIZE-1; I ++) {list [I]. cur = I + 1; // The cursor cur of the first element of the array points to the first node storage location of the standby linked list (array subscript), and so on} list [MAXSIZE-1]. cur = 0; // The current static linked list is empty, and the last cur with a value element is 0} 2. Insert/delete a static linked listTo insert or delete a static linked list, the most important thing is to solve the problem of how to use static simulation to allocate the storage space of the Dynamic Linked List structure. You need to apply for it and release it when it is unavailable. 1. Insert a static linked list (1) algorithm ideasTo identify which components in the array are not used, the solution is to use the cursor cur to link all components that violate the used and deleted components into a backup linked list (that is, an empty linked list ), each time you insert data, you can obtain the first node (the first node that is not used) from the backup linked list. Implement the algorithm to obtain the idle component subscript Malloc_SLL:. obtain the cursor cur = I for the first element of the array, which stores the first idle node of the backup linked list; B. assign the cursor cur = I + 1 of the array element I to the header pointer c. returns the subscript of the used array element.
Int I = list [0]. cur; // For example, I = list [0]. cur = 7 list [0]. cur = list [I]. cur; // header pointer list [0]. cur = list [7]. cur = 8 return I;
(2) source code implementation/* 1. if the linked list of the standby space is empty, the allocated node subscript is returned. Otherwise, 0 */int Malloc_SLL (StaticLinkList list) {int I = list [0] is returned. cur; // obtain the first node subscript of the standby linked list (the first element of the current array stores the first idle subscript of the standby) if (list [0]. cur) // If list [0]. cur! = 0 indicates that the array contains non-empty elements {list [0]. cur = list [I]. cur; // to use the node of a backup linked list, we need to store the cur of the first element of the array as the standby element} return I; // returns the used subscript}
// Note: if the previous list is [0]. cur = 7 (array bottom value), the current component marked as 7 (array element) is ready to be used, there must be a replacement, so the Component 7 (list [I]. cur, where I = 7) of the cur value = 8, assigned to the Header element (list [0]. cur), and then you can continue to allocate new idle components. /// * 2. insert a new data element e */typedef int Statustypedef int ElemTypeStatus ListInsert (StaticLinkList L, int I, ElemType e) {int j, k, m; k = MAX_SIZE-1; // Note: k is the subscript of the last element first if (j <1 | j> ListLength (L) + 1) return ERROR; j = Malloc_SLL (L); //. returns the subscript of the idle component if (j) {L [j]. data = e; // B. assign data to the data for (m = 1; m <I-1; I ++) of this component // c. locate the position before the I element and save the cur (= 1) of the last element of the array to the variable k = L [k]. cur; // k = 1 L [j]. cur = L [k]. cur; // assign the cur value before the I element to the cur L [k ]. Cur = j; // assign the subscript of the new element to the return OK;} return ERROR;} Comment: The I element refers to the I element of the linked list, non-array subscript storage location. I-insert static linked list locations; j-idle storage space locations;
2. Delete static linked listSource code implementation/* 1. reclaim the idle node with the subscript k to the backup linked list */void Free_SSL (StaticLinkList space, int k) {space [k]. cur = space [0]. cur; // assign the first element cur (its value is the first idle element subscript of the backup linked list) to the cur space [0] to delete the component. cur = k; // assign the subscript of the component to be deleted to the cur}/* 2 of the first element. delete the I-th Data Element e */typedef int StatusStatus ListDelete (StaticLinkList L, int I) {int I, k; if (I <1 | I> ListLength (L) return ERROR; k = MAXSIZE-1; // store the subscript for (j = 1; j <= I-1; j ++) k = L [k]. cur; // locate the previous element of the element to be deleted, and assign its cur value K (subscript of the element to be deleted) j = L [k]. cur; // assign the cursor value of the deleted element to j, that is, the value is the subscript of the next element L [k]. cur = L [j]. cur; Free_SSL (L, j);}/* 3. initial Condition: the static linked list L already exists. Returns the number of data elements in L */int ListLength (StaticLinkList L) {int j = 0; int I = L [MAXSIZE-1]. cur; while (I) {I = L [I]. cur; j ++;} return j ;}
Iii. Advantages and Disadvantages of static linked lists 1. AdvantagesDuring the insert and delete operations, you only need to modify the cursor and do not need to move elements. This improves the disadvantages of the insert and delete operations in the sequential storage structure that need to move a large number of elements; 2. Disadvantages(1) the problem that the table length is hard to be determined due to the continuous storage allocation is not solved; (2) the random access feature of the sequential storage structure is lost;

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.