Linear table-sequential Storage Structure and linear Storage Structure
1) Statement
Newbie. If something is wrong, unreasonable, encoding style, and algorithm ideas need to be improved, please give me some advice.
Ii) Introduction
This article uses the method of dynamically opening up the memory to create a linear table, to achieve the basic operations of the sequence table.
This code is simple. After all, it is just a simple principle and has no specific application. It is only used as an accumulation of getting started learning.
Iii. Implementation Analysis:
3.1) the header file is defined as follows:
1 # ifndef LinearList_SqList_h 2 # define LinearList_SqList_h 3 4 # include <stdio. h> 5 # include <stdlib. h> 6 7 # define OK 1 8 # define ERROR 0 9 10 # define LIST_INIT_SIZE 10011 # define SIZE_INCREMENT 1012 13 typedef int Status; 14 typedef int ElemType; 15 16 typedef struct {17 ElemType * pElem; 18 int nLength; 19 int nListSize; 20} SqList; 21 22 23 void InitSqList (SqList * L ); 24 Status InsertSqList (SqList * L, int nIndex, ElemType eValue); 25 Status AssignSqList (SqList * L); 26 Status DeleteSqList (SqList * L, int nIndex, ElemType * pValue ); 27 Status ClearSqList (SqList * L); 28 Status DestroySqList (SqList * L); 29 Status LocateElem (SqList L, ElemType nValue); 30 Status PrintSqList (SqList L ); 31 32 # endifSqList. h
3.2) specific implementation:
3.2.1) Basic Principles:
A) In function parameters, as long as the pointer is passed, the function must first check whether the pointer is null.
B) If you want to modify the sequence table, you must pass the address instead of the value.
C) The Memory applied for by malloc must be released with free. After the memory is released, the pointer is set to null to prevent the occurrence of pointer.
3.2.2) specific code ideas:
A. InitSqList
Dynamically apply for space. assign values to the members of the sequence table.
B. InsertSqList (null tables can be inserted here)
Train of Thought: Verify pointer parameters -- check insertion point -- check whether the sequence table has been initialized -- check whether the sequence table is full -- traverse the sequence table, locate the insertion point -- move element -- insert -- and increase the table length;
Analysis: in fact, the real insertion is a line of code, but to ensure that the insertion does not affect the use of other elements, the elements must be moved; To ensure the robustness of the program, a lot of validation processes must be added;
The focus is on the calculation of the lower mark.
2.1 parameter verification: whether the pointer is null or whether the insertion point is invalid
2.2 sequence table check: whether the sequence table has been initialized; whether the sequence table is full for insertion;
2.3 After all the verifications are completed, find the correct insertion point, move the element (traverse from the end of the table to the destination point), insert the element, and increase the table length;
(2.4) question about whether empty tables can be inserted: set according to different requirements and functions. If you do not allow insertion of empty tables, you can add a verification function. (insert of empty tables is allowed here)
C. DeleteSqList (basic principle is similar to Insert)
Train of Thought: Check pointer parameters -- check Deletion Point -- check whether the sequence table is initialized -- check whether the sequence table is empty -- check the sequence table to traverse the sequence table, search for the delete point -- move the element -- delete the table -- and reduce the table length;
Analysis: deleting an element is a constant moving element. You can use the overwrite principle (traverse from the deletion point to the end of the table ). The key is the calculation of the lower mark.
D. DestroySqList
Idea: Check pointer parameters -- check whether sequence tables are initialized -- release applied memory -- and set parameters related to sequence tables.
Analysis: the focus is that the pointer after the parameter checksum and free should be null.
3.3) the code is as follows:
1 # include "SqList. h "2 3 void InitSqList (SqList * L) {4 if (NULL = L) {5 printf (" Error parament. "); 6 return; 7} 8 9 L-> pElem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (SqList); 10 if (NULL = L-> pElem) {11 printf ("Error: Out of memory. "); 12 return; 13} 14 15 L-> nLength = 0; 16 L-> nListSize = LIST_INIT_SIZE; 17} 18 19 20 // Insert element to sqlist 21 Status InsertSqList (SqList * L, int nIndex, ElemType eValue) {22 if (NULL = L) {23 printf ("Error parament. "); 24 return ERROR; 25} 26 27 if (NULL = L-> pElem) {28 printf (" Error: The list is not initiated. "); 29 return ERROR; 30} 31 32 if (nIndex <1 | nIndex> L-> nLength + 1) {33 printf (" Error: Invalid insert point. "); 34 return ERROR; 35} 36 37 ElemType * pNewBase; 38 if (L-> nLength> = L-> nListSize) {39 pNewBase = (ElemType *) realloc (L-> pElem, (LIST_INIT_SIZE + SIZE_INCREMENT) * sizeof (ElemType); 40 if (NULL = pNewBase) {41 printf ("Error: Out of memory. "); 42 return OK; 43} 44 // here can also write with 'else '. 45 // Logically no problem. but it takes effort to write the else and '{}'. efficiently not good choice 46 L-> pElem = pNewBase; 47 L-> nListSize + = SIZE_INCREMENT; 48} 49 50 ElemType * pInsert, * pLast; 51 pInsert = L-> pElem + nIndex-1; 52 pLast = L-> pElem + L-> nLength-1; 53 54 55 while (pLast> = pInsert) {56 * (pLast + 1) = * pLast; 57 pLast --; 58} 59 60 * pInsert = eValue; 61 + L-> nLength; 62 63 return OK; 64} 65 66 // Assign sqlist 67 Status AssignSqList (SqList * L) {68 if (NULL = L) {69 printf ("Error parament. "); 70 return ERROR; 71} 72 73 if (NULL = L-> pElem) {74 printf (" Error: The list is not initiated. "); 75 return ERROR; 76} 77 78 int nLength, nValue; 79 printf (" Input the length: "); 80 scanf (" % d ", & nLength ); 81 82 for (int I = 1; I <= nLength; I ++) {83 printf ("Input the value:"); 84 scanf ("% d ", & nValue); 85 86 InsertSqList (L, I, nValue); 87} 88 89 return OK; 90} 91 92 // delete element from sqlist 93 Status DeleteSqList (SqList * L, int nIndex, ElemType * pValue) {94 if (NULL = L | NULL = pValue) {95 printf ("Error parament. "); 96 return ERROR; 97} 98 99 if (NULL = L-> pElem) {100 printf (" Error: The list is not initiated. "); 101 return ERROR; 102} 103 104 if (L-> nLength <= 0) {105 printf (" Error: The list is empty. can't delete. "); 106 return ERROR; 107} 108 109 if (nIndex <1 | nIndex> L-> nLength) {110 printf (" Error: Invalid delete index. "); 111 return ERROR; 112} 113 ElemType * pDelete, * pLast; 114 pDelete = L-> pElem + nIndex-1; 116 pLast = L-> pElem + L-> nLength-1; 117 118 * pValue = * pDelete; 119 for (pDelete ++; pDelete <= pLast; pDelete ++) {120 * (pDelete-1) = * pDelete; 121} 122 123 -- L-> nLength; 124 125 return OK; 126} 127 128 // clear sqlist129 Status ClearSqList (SqList * L) {130 if (NULL = L) {131 printf ("Error parament. "); 132 return ERROR; 133} 134 135 if (NULL = L-> pElem) {136 printf (" Error: The list is not initiated. "); 137 return ERROR; 138} 139 140 L-> nLength = 0; 141 142 return OK; 143} 144 145 // destroy sqlist146 Status DestroySqList (SqList * L) {147 if (NULL = L) {148 printf ("Error parament. "); 149 return ERROR; 150} 151 152 if (NULL = L-> pElem) {153 printf (" Error: The list is not initiated. "); 154 return ERROR; 155} 156 free (L-> pElem); 157 L-> pElem = NULL; 158 L-> nLength = 0; 160 L-> nListSize = 0; 161 162 return OK; 163} 164 165 // locate element from sqlist166 Status LocateElem (SqList L, ElemType nValue) {167 if (NULL = L. pElem) {168 printf ("Error: The list is not initiated. "); 169 return ERROR; 170} 171 172 if (L. nLength <= 0) {173 printf ("Error: The list is empty. "); 174 return ERROR; 175} 176 177 int nIndex; 178 ElemType * pLocate = L. pElem; 179 180 for (nIndex = 1; nIndex <= L. nLength; nIndex ++) {181 if (nValue = * pLocate) {182 printf ("Located succeeded. "); 183 return nIndex; 184} 185 pLocate ++; 186} 187 188 return ERROR; 189} 190 191 192 // Print sqlist193 Status PrintSqList (SqList L) {194 if (NULL = L. pElem) {195 printf ("Error: The list is not initiated. "); 196 return ERROR; 197} 198 199 if (L. nLength <= 0) {200 printf ("The list is empty. "); 201 return ERROR; 202} 203 printf (" The list is as follows: "); 204 for (int I = 1; I <= L. nLength; I ++) {206 printf ("% d", * L. pElem); 207 L. pElem ++; 208} 209 210 printf ("\ n"); 211 return OK; 212}SqList. c