Data Structure sequence table ideas, complete code implementation, and data structure Sequence
This article reposted self fun learning algorithm, for personal learning reference use http://blog.csdn.net/rainchxy/article/details/77946835
Data Structure 3rd sequence table
A sequence table is the simplest linear structure. Logically, adjacent data is stored in the computer, and the first few elements can be quickly located without being empty, therefore, a large number of elements need to be moved during insertion and deletion.
An ordered table can be allocated with a continuous storage space Maxsize. elem is used to record the base address, and length is used to record the actual number of elements, that is, the length of the ordered table,
Struct definition:
After struct is defined, if you want to define an ordered table L, you can write:
SqList L;
1. Sequence Table Initialization
Initialization refers to allocating a pre-defined size space to the sequence table. The base address elem is used to record the first address of the space, which is useless and the number of elements is 0. Previously we have predefined a Maxsize maximum space, so we can use new to allocate such a large space. If the allocation is successful, the first address of the space will be returned. Assume that the number of integers needs to be stored in the sequence table, you can initialize it as follows:
BoolInitList (SqList & L) // construct an empty sequence table L
{// L plus & indicates the reference type parameter. The internal change exit function is still valid.
// No & internal changes, invalid after jumping out of the Function
L. elem = new int [Maxsize]; // assign Maxsize space to the sequence table
If (! L. elem) return false; // storage allocation failed
L. length = 0; // The length of the empty table is 0.
Return true;
}
2. Create a sequence table
An ordered table is created to input data to an ordered table. The type of the input data must be the same as that in the type definition. Assume that the number of integers needs to be stored in the sequence table, you can create it as follows:
BoolCreateList (SqList & L) // create an ordered table L
{// L plus & indicates the reference type parameter. The internal change exit function is still valid.
// No & internal changes, invalid after jumping out of the Function
Int a, I = 0;
While (! =-1)
{
Cin>;
If (L. length = Maxsize)
{
Cout <"the sequence table is full !"
Return false;
}
L. elem [I ++] =;
L. length ++;
}
Return true;
}
3. Values of the sequence table
Any element in the sequence table can be found immediately, which is called a random access method. For example, if we take the I element, as long as the I value is reasonable (1 ≤ I ≤ L. length), then the element L can be found immediately. elem [I-1]:
Bool GetElem (SqList L, int I, int & e)
{
If (I <1 | I> L. length) return false;
// Judge whether the I value is reasonable. If it is unreasonable, false is returned.
E = L. elem [I-1]; // The unit of the I-1 stores the I data
Return true;
}
4. Sequential table search
To search for an element e in an ordered table, you must start from the first element and compare the values of each element in sequence. If the values are equal, the return element position (the first element) is returned ), if the entire sequence table is not found,-1 is returned:
Int LocateELem (SqList L, int e)
{
For (I = 0; I <L. length; I ++)
If (L. elem [I] = e) return I + 1; // The first few elements, such as the first 5th elements, the subscript is actually 4
Return-1;
}
Time Complexity Analysis:
Best case: If the element is in the first position, it is successful at a time. The time complexity isO(1 );
Worst case: If the element is in the last position, you need to compareNSuccessful, time complexity isO(N);
Average condition: assuming that each element has an equal probability of finding, the first position needs to be compared once, and the second position needs to be compared twice ,..., The last position, which needs to be comparedNTimesNAnd the average time complexity is alsoO(N):
5. Sequence Table insertion
In the sequence tableIInsert an element before positionE, You need to move back from the last element ,..., UntilIAnd thenEPlaceILocation.
(1) determine the insert positionIIs it legal (1 ≤I≤ L. length + 1), which can be inN+ Insert one element.
(2) Determine whether the storage space of the sequence table is full.
(3) ChangeNToIThe elements of the bits move backward one position in sequence, leavingILocation.
(4) new elements to be insertedEPlaceILocation.
(5) If the table length is increased by 1, true is returned after successful insertion.
Bool ListInsert_Sq (SqList & L, int I, int e)
{
If (I <1 | I> L. length + 1) return false; // The I value is invalid.
If (L. length = Maxsize) return false; // the bucket is full.
For (j = L. length-1; j >=i-1; j --)
L. elem [j + 1] = L. elem [j]; // move back from the last element until the I element is removed
L. elem [I-1] = e; // place the new element e in position I
L. length ++; // The table length increases by 1.
Return true;
}
Time Complexity Analysis:
Assuming that the insertion probability at each position is equal, the insertion can be performed before the first position, and before the second position ,..., Before the last positionN+ A totalN+ In each case, the number of moving elements isN-I+ 1. Add all the situations to the average time. The average time complexity isO(N):
6. delete an ordered table
DeleteIElements, need to save the element to the variableEAnd thenI+ One element starts to move forward ,..., UntilNAnd thenEPlaceILocation.
(1) determine the insert positionIIs it legal (1 ≤I≤ L. length ).
(2) Retain the elements to be deleted inE.
(3) ChangeI+ 1NThe elements of BITs move forward one position in sequence.
(4) If the table length is reduced by 1, true is returned if the deletion is successful.
Bool ListDelete_Sq (SqList & L, int I, int & e)
{
If (I <1) | (I> L. length) return false; // The I value is invalid.
E = L. elem [I-1]; // keep the element to be deleted inEMedium
For (j = I; j <= L. length-1; j ++)
L. elem [J-1] = L. elem [j]; // move the element forward after the deleted Element
L. length --; // The table length minus 1.
Return true;
}
Time Complexity Analysis:
Assume that the probability of deleting each element is equal.NIn each case, the number of moving elements isN-I, Adds all the situations to the average, and the average time complexity isO(N):
Complete code
1 # include <iostream> 2 using namespace std; 3 # define Maxsize 100 // maximum space 4 typedef struct {5 int * elem; 6 int length; // sequence table length 7} SqList; 8 9 bool InitList (SqList & L) // construct an empty sequence table L 10 {// L plus & indicates the reference type parameter, internal Function Change exit function is still valid 11 // No & indicates internal change, it is invalid 12 L after function exit. elem = new int [Maxsize]; // assign Maxsize 13 if (! L. elem) return false; // storage allocation failed 14 L. length = 0; 15 return true; 16} 17 18 bool CreateList (SqList & L) // create an ordered table L 19 {20 int a, I = 0; 21 cin> a; 22 while (! =-1) {23 if (L. length = Maxsize) {24 cout <"the sequence table is full! "; 25 return false; 26} 27 L. elem [I ++] = a; 28 L. length ++; 29 cin> a; 30} 31 return true; 32} 33 34 bool GetElem (SqList L, int I, int & e) {35 if (I <1 | I> L. length) return false; 36 // determines whether the I value is reasonable. If not, false 37 e = L is returned. elem [I-1]; // The unit of the I-1 stores the I data 38 return true; 39} 40 41 int LocateElem (SqList L, int x) {42 for (int I = 0; I <L. length; I ++) 43 if (L. elem [I] = x) return I + 1; // The first element, for example, the fifth element. The subscript is actually 4 44 return-1; 45} 46 47 bool ListInsert_Sq (SqList & L, int I, int e) 48 {49 if (I <1 | I> L. length + 1) return false; // The I value is invalid. 50 if (L. length = Maxsize) return false; // the storage space is full 51 for (int j = L. length-1; j >= I-1; j --) 52 L. elem [j + 1] = L. elem [j]; // move backward from the last element until 53 L is removed after the I element. elem [I-1] = e; // place the new element e in position 54 L. length ++; // table length plus 1 55 return true; 56} 57 58 bool ListDelete_Sq (SqList & L, int I, int & e) 59 {60 if (I <1) | (I> L. length) return false; // The I value is invalid. 61 e = L. elem [I-1]; // keep the element to be deleted in e 62 for (int j = I; j <= L. length-1; j ++) 63 L. elem [J-1] = L. elem [j]; // The elements after the deleted element are removed 64 L. length --; // reduce the table length by 1 65 return true; 66} 67 68 void print (SqList L) 69 {70 cout <"output sequence table" <endl; 71 for (int j = 0; j <= L. length-1; j ++) 72 cout <L. elem [j] <"; 73 cout <endl; 74} 75 76 void DestroyList (SqList & L) {77 if (L. elem) delete [] L. elem; // release bucket 78} 79 80 int main () 81 {82 SQL Ist myL; 83 int I, e, x; 84 cout <"1. initialize \ n "; 85 cout <" 2. create \ n "; 86 cout <" 3. value: \ n "; 87 cout <" 4. find \ n "; 88 cout <" 5. insert \ n "; 89 cout <" 6. delete \ n "; 90 cout <" 7. output \ n "; 91 cout <" 8. destroy \ n "; 92 cout <" 0. exit \ n "; 93 94 int choose =-1; 95 while (choose! = 0) {96 cout <"select:"; 97 cin> choose; 98 switch (choose) {99 case 1: // initialize the sequence table 100 cout <"sequence table initialization ·" <endl; 101 if (InitList (myL )) 102 cout <"sequence table initialization successful! "<Endl; 103 else104 cout <" An error occurred while initializing the sequence table! "<Endl; 105 break; 106 case 2: // create Sequence Table 107 cout <" sequence table creation... "<endl; 108 cout <"input integer number, input-1 End" <endl; 109 if (CreateList (myL) 110 cout <"sequence table created successfully! "<Endl; 111 else112 cout <" An error occurred while creating the sequence table! "<Endl; 113 break; 114 case 3: // value: 115 cout <" input integer I, take the I-th element for output "<endl; 116 cin> I; 117 if (GetElem (myL, I, e) 118 cout <"The I element is:" <e <endl; 119 failed to set the value of else120 cout <"sequence table! "<Endl; 121 cout <" the I-th element is: "<e <endl; 122 break; 123 case 4: // search for 124 cout <"Enter the number of search x:"; 125 cin> x; 126 if (LocateElem (myL, x) =-1) 127 cout <"search failed! "<Endl; 128 else 129 cout <" search successful! "<Endl; 130 break; 131 case 5: // insert 132 cout <" Enter the location of the data to be inserted and the Data Element e :"; 133 cin> I> e; 134 if (ListInsert_Sq (myL, I, e) 135 cout <"inserted successfully! "<Endl; 136 else 137 cout <" insertion failed! "<Endl; 138 break; 139 case 6: // Delete 140 cout <" Enter the location I: "; 141 cin> I; 142 if (ListDelete_Sq (myL, I, e) 143 cout <"deleted successfully! "<Endl; 144 else145 cout <" deletion failed! "<Endl; 146 break; 147 case 7: // output 148 print (myL); 149 break; 150 case 8: // destroy 151 cout <"sequence table destruction... <endl; 152 DestroyList (myL); 153 break; 154} 155} 156 return 0; 157} 158