A simple basic sequence table operation example
Recently, I learned the basics of computer software and linear table. The following is a simple example of the simplest sequence table in a linear table for your convenience. (In addition, this is the key point for a small analysis of parameter passing through the function)
1 // Requirement Analysis 2 // 1. The linear table is incrementally ordered, and the repeated elements are deleted 3 // 2. The linear table is reversed 4 // 3. The maximum value is 5 6 # include <stdio. h> 7 8 typedef int ElementType; 9 typedef struct _ struct 10 {11 ElementType SequenceList [100]; 12 ElementType num; 13} SequenceList; 14 15 void InitSeq (SequenceList *** L); // initialize a linear table. It mainly returns a SequenceList instance and uses malloc to dynamically open up the memory mode. 16 int Add (SequenceList * L, ElementType value); // Add project 17 void Insert (SequenceList * L, ElementType pos, ElementType value) to the list ); // insert an element 18 void Delete (SequenceList * L, ElementType value); // Delete an element 19 ElementType Search (SequenceList * L, ElementType value ); // search for an element 20 void RemoveRepetiton (SequenceList * L); // remove all repeated elements 21 void Transpose (SequenceList * L ); // put all element positions upside down 22 ElementType MaxValue (SequenceList * L); // obtain the maximum value of 23 void Travel (SequenceList * L) in the element; // traverse the entire list (output each element once) 24 25 int main () 26 {27 SequenceList * L; 28 InitSeq (& L); 29 while (1) 30 {31 int n = 0; 32 int pos; 33 ElementType value; 34 printf ("Select Operation :( 1.Add 2. insert 3. delete 4. search 5. transpose 6. maxium 7. travel 8. removeReptitons) \ n "); 35 scanf (" % d ", & n); 36 switch (n) 37 {38 // add 39 case 1: 40 41 printf ("Add new item:"); 42 scanf ("% d", & value); 43 Add (L, value); 44 break; 45 // insert 46 case 2: 47 48 printf ("insert a position: (position value) \ n "); 49 scanf ("% d", & pos, & value); 50 Insert (L, pos, value); 51 break; 52 // delete: 53 case 3: 54 printf ("input Delete content:"); 55 scanf ("% d", & value); 56 Delete (L, value); 57 break; 58 // Search 59 case 4: 60 printf ("Search content:"); 61 scanf ("% d", & value); 62 pos = Search (L, value ); 63 if (pos) 64 printf ("exists in item % d. \ N ", pos); 65 else 66 printf (" cannot be found! \ N "); 67 break; 68 // transpose: 69 case 5: 70 Transpose (L); 71 Travel (L); 72 break; 73 // max 74 case 6: 75 printf ("the maximum value is % d. \ N ", MaxValue (L); 76 break; 77 case 7: 78 Travel (L); 79 break; 80 case 8: 81 RemoveRepetiton (L); 82 break; 83 default: 84 break; 85} 86} 87 free (L); 88 return 0; 89} 90 void InitSeq (SequenceList *** L) 91 {92 * L = (SequenceList *) malloc (sizeof (SequenceList); 93 (* L)-> num =-1; 94} 95 96 int Add (SequenceList * L, ElementType value) 97 {98 L-> num ++; 99 if (L-> num >=100) 100 {101 L-> num --; 102 pri Ntf ("the space is full! "); 103 return 0; 104} 105 L-> SequenceList [L-> num] = value; 106 return 1; 107} 108 109 void Insert (SequenceList * L, elementType pos, ElementType value) 110 {111 if (L-> num <0 | L-> num> = 99) 112 {113 printf ("the space is full or insufficient! "); 114 return 0; 115} 116 if (pos-1 <0 | pos-1> L-> num) 117 {118 printf (" Insertion Location Error! "); 119 return; 120} 121 int I; 122 for (I = L-> num; I> = pos-1; I --) 123 {124 L-> SequenceList [I + 1] = L-> SequenceList [I]; 125} 126 L-> SequenceList [pos-1] = value; 127 L-> num ++; 128} 129 130 void Delete (SequenceList * L, ElementType value) 131 {132 if (L-> num <0) 133 {134 printf ("the table is empty! "); 135 return; 136} 137 int I; 138 for (I = 0; I <= L-> num; I ++) 139 {140 if (value = L-> SequenceList [I]) 141 {142 int j; 143 for (j = I; j <L-> num; j ++) 144 {145 L-> SequenceList [j] = L-> SequenceList [j + 1]; 146} 147 L-> num --; 148} 149} 150} 151 152 ElementType Search (SequenceList * L, ElementType value) 153 {154 if (L-> num <0) 155 {156 printf ("the table is empty! "); 157 return-1; 158 159} 160 else161 {162 int I; 163 for (I = 0; I <= L-> num; I ++) 164 {165 if (value = L-> SequenceList [I]) 166 return I + 1; 167} 168} 169 170 return 0; 171} 172 173 void RemoveRepetiton (SequenceList * L) 174 {175 int count = 0; 176 int I; 177 for (I = 0; I <= L-> num; I ++) 178 {179 if (Search (L, L-> SequenceList [I]) 180 {181 Delete (L, L-> SequenceList [I]); 182 count ++; 183} 184} 185 printf ("Total deleted % D. ", Count); 186} 187 188 void Transpose (SequenceList * L) 189 {190 ElementType * temp = (ElementType *) malloc (sizeof (ElementType) * (L-> num + 1); 191 int I, j; 192 for (I = L-> num, j = 0; I> = 0; I --, j ++) 193 {194 temp [j] = L-> SequenceList [I]; 195} 196 for (I = 0; I <= L-> num; I ++) 197 {198 L-> SequenceList [I] = temp [I]; 199} 200 free (temp); 201} 202 ElementType MaxValue (SequenceList * L) 203 {204 int I = 0; 205 El EmentType max = L-> SequenceList [0]; 206 for (I = 1; I <= L-> num; I ++) 207 {208 if (max <L-> SequenceList [I]) 209 {210 max = L-> SequenceList [I]; 211} 212} 213 return max; 214} 215 216 void Travel (SequenceList * L) 217 {218 if (L-> num <0) 219 printf ("table empty! "); 220 else221 {222 int I; 223 for (I = 0; I <= L-> num; I ++) 224 {225 printf (" % d ", l-> SequenceList [I]); 226 if (I> 0 & (I % 10 = 0) 227 printf ("\ n "); 228} 229 printf ("\ n"); 230} 231}Click to view code
The code is written by hand instead of online copy ..
There are no algorithms in the code, and the simplest method is used for searching and deleting.
The only thing worth noting in the Code is the initialization list function InitList (SequenceList ** L). The second-level pointer is passed as the parameter. Why?
The following explains:
Our purpose is to malloc a piece of memory space in the InitList function to store our data.
Now let's take a look at a common incorrect writing method.
// See the following error code: void InitList (SequenceList * L) {L = (SequenceList *) malloc (sizeof (SequenceList )); // syntax focus .. L-> num =-1; // This is part of the program function}
Many beginners think that we pass the pointer variable to the InitList function. In InitList, we can assign values to the pointer variable to modify the variable in the main function, in this case, the first address of the space opened by malloc is assigned to L. Then, when I operate on L in the main function, I find errors such as garbled characters or inaccessible, why?
The reason is very simple, that is, the transfer method of function parameters is not clear. It should be noted that the parameter passing through the function always transmits the copy of the copied variable, not the variable itself.
To solve this problem, we will perform the following operations in the main function:
// Sample code for error. Remember
Void main () {SequenceList * L; InitList (L );......}
We pass a SequenceList pointer variable L for the function InitList. Assume that L points to the memory 0x10000 at this time, when actually passing parameters, the system will perform a copy operation on the L variable. If the copied variable is P, the P point to the address 0x10000.
Now the point is that we pass the parameter L for InitList, but the actual parameter passed by the system is P (P is a copy of L, they point to the same memory area, but their respective addresses are different.) In the InitList function, the P variable is actually operated, and the malloc address (assuming 0x30000) is assigned to P,
So P points to the address 0x30000, but L in the main function still points to 0x10000, which leads to the error we encountered, because we didn't change the address indicated by L to the address of malloc at all;
Next, let's take a look at the correct operations.
// The following code is the correct code void main () {SequenceList * L; InitList (& L);} void InitSeq (SequenceList ** L) {* L = (SequenceList *) malloc (sizeof (SequenceList); (* L)-> num =-1 ;}
When we pass the parameter, we pass the address of variable L. Then the system actually transmits the Variable P, which has the following relationship with L: P = & L (P points to the address of variable L ). in the InitList Function
*L = (SequenceList *)malloc(sizeof(SequenceList));
L is P, * L is the L in the main function. Now, assign values to the L in the main function using the malloc address * L.
Do you understand? This is my personal understanding. Maybe the system is different from what I said in some details, but I should be right.