First, let the elements of the array be composed of two data fields: Data and cur. That is to say, each lower mark of the array corresponds to a data and a cur.
The data field data is used to store data elements, that is, the data we usually need to process. The cursor cur is equivalent to the next pointer in a single-chain table,
The subscript of the element in the array. The linked list described in arrays is called a static linked list.
The first element of the array, that is, the cur of the element whose subscript is 0, stores the subscript of the first node of the backup linked list; and the cur of the last element of the array
Store the subscript of the first element with a value, which is equivalent to the header node of the single-chain table. If the entire linked list is empty, it is 0, indicating no point. See Figure 3-12-2.
Now, if we want to insert an element with the value of "C" between "B" and "ding", we only need to change the cur of "B" to 7, indicates that the next digit is "C", and the cur of "C" is changed to 4, indicating that the next digit is Ding.
3-12-3.
Now, if we delete the first element "A", it means that the position "a" is blank. If there are new users coming in the future, the priority is given here, therefore, the location to be deleted becomes the first priority vacancy, that is, the first element's cur is 1, the first element's location's cur is changed to 8, and the subscript's location's cur is changed to 9, the cur at the last element location is changed to 2, 3-12-4.
Sample Code: (adapted from big talk data structure)
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
# Include <iostream> Using namespace STD;# Define Max size 100 Typedef int elemtype; /* Static linked list storage structure of a linear table */ Typedef struct Node { Elemtype data; Int cur; // 0 indicates no point } Staticlinklist [maxsize]; /* Chain each component in the one-dimensional array into a backup linked list. array [0]. cur is the header pointer, and "0" indicates a null pointer */ Bool initlist (staticlinklist array) { Cout <"initlist..." <Endl; For (INT I = 0; I <maxsize-1; I ++) { Array [I]. cur = I + 1; } Array [maxsize-1]. cur = 0;/* The current static linked list is empty, and the cur of the last element is 0 */ Return true; } /* If the linked list of the standby space is not empty, the allocated node subscript is returned. Otherwise, 0 */is returned */ Int malloc_sll (staticlinklist array) { Int K = array [0]. cur; If (k) Array [0]. cur = array [K]. cur;/* The next component is used for backup */ Return K; } /* Reclaim the idle node with the subscript POs to the backup linked list */ Void free_sll (staticlinklist array, int POS) { Array [POS]. cur = array [0]. cur;/* assign the cur value of the first element to the component cur to be deleted */ Array [0]. cur = Pos;/* assign the subscript of the component to be deleted to the cur of the first element */ } Int listlength (staticlinklist array) { Int I = array [maxsize-1]. cur; Int J = 0; While (I) { I = array [I]. cur; ++ J; } Return J; } /* Insert a new data element ELEM before the POs element in array */ Bool listinsert (staticlinklist array, int POs, elemtype ELEM) { Cout <"Insert list from pos:" <POS <"item" <ELEM <Endl; If (Pos <1 | POS> listlength (array) + 1) Return false; Int K = maxsize-1; Int I = malloc_sll (array);/* obtain the subscript of the idle component */ If (I) { Array [I]. Data = ELEM; For (int l = 1; L <= pos-1; l ++) K = array [K]. cur; Array [I]. cur = array [K]. cur;/* assign the cur value before the POs element to the cur of the new element */ Array [K]. cur = I;/* assign the subscript of the new element to the cur of the element before the POs element */ Return true; } Return false; } /* Delete the POS data element in array */ Bool listdelete (staticlinklist array, int POS) { Cout <"delete list from pos:" <POS <Endl; If (Pos <1 | POS> listlength (array )) Return false; Int K = maxsize-1; For (int l = 1; L <= pos-1; l ++) K = array [K]. cur; Int J = array [K]. cur; Array [K]. cur = array [POS]. cur; Free_sll (array, J ); Return true; } Bool listtraverse (staticlinklist array) { Cout <"list traverse:" <Endl; Int K = maxsize-1; While (array [K]. cur! = 0) { K = array [K]. cur; Cout <array [K]. Data <''; } Cout <Endl; Return true; } Int main (void) { Staticlinklist SSL; Initlist (SSL ); For (INT I = 1; I <5; I ++) Listinsert (SSL, I, I ); Listtraverse (SSL ); Listdelete (SSL, 3 ); Listtraverse (SSL ); Cout <"list length:" <listlength (SSL) <Endl; Return 0; } |
Output:
Static linked lists do not need to move elements during insert or delete operations, but only need to modify the cursor. This improves the need to move the insert and delete operations in the sequential storage structure.
The disadvantages of a large number of elements; however, it does not solve the problem that the table length is difficult to determine due to the continuous distribution of storage; and it also loses the random access feature of the sequential storage structure.