Home > Back-end >  Insert remove rewrite data structure
Insert remove rewrite data structure

Time:10-11

Can write the detailed point, thank you

CodePudding user response:

I=1
P=L.e lem.
While (I<=L.l ength & amp; & ! Compare (*) (* p++, e)) I++;

E here is to find the element
Compare is a function pointer, the function prototype should be
Compare int (*) (ElementType ElementType e1, e2); Used to compare whether the two elements of the same
This is in order to cope with the complexity of the structure, and replace with a function pointer to directly compare
If the linear table is of type int, also known as ElementType int type, so the above code can be changed to
While (I<=L.l ength & amp; & ! (* p++==e) ) I++;
* p++==e this expression, is to use pointer to the linear table elements, compared each and e, to find a will continue to find the next, found while dropped out

An array of writing has nothing to explain

CodePudding user response:

Linear table which stores may be any type allows a struct structure, may be a float, maybe is a pointer to string, the comparison between these elements cannot be simple array notation on the direct judging==, such as string pointer==, STRCMP will use to replace; Float==, want to use fabs (f1, f2) & lt; EPSOLN way, the comparison of structure is more complex,

To find the method is general, so we provide a single comparison function, for different types of linear table, complete the elements with different comparison function to the comparison between

CodePudding user response:

I=1
P=L.e lem.
While (I<=L.l ength & amp; & ! Compare (*) (* p++, e)) I++;
This kind of writing, when from the exit of the while, and do a judgment
If (I & gt; L.l ength)
The output is not found
The else
I was to find the index

CodePudding user response:

This just understand the most basic grammar should be able to understand the ah,
First, compare is p, pointing to the elements and e comparison, returns 0 if equal, before it returns non-zero, so as long as the returns non-zero, means, so while will continue to cycle, until I reach the end of the list (that is, I position is greater than the length of chain table)
Into a for loop to illustrate
For (I=1, p=L.e lem. iCompare the if (* (* p, e)==0) break;//if the find the exit loop
P++;//or pointer to the next element
}

Second, as an array, pass an array subscript is cycle, if the same is returned directly subscript subscript elements and e, didn't find if cycle until the last, it returns 0
Into a for loop to illustrate
for(i=1; iIf [I]==e (L) return the I;//if found it returns the subscript I
}
return 0;//can't find it returns 0
  • Related