And my problem is that I do not understand how, there can be a fixed time of insertion and receipt.
In general, insert / remove an element in a constant time will not work. In the absence of collisions, it is possible to insert / remove an element beyond O(1) , but collisions (for an unlimited set of objects) are always possible. In the case of collisions, it is possible to insert / remove an element for O(n) (in the case of using a linked list) and for O(log n) (in the case of using a balanced tree).
After all, if I use an array, I still have to go over the keys to match
If you use an array with normal addressing, then yes, element retrieval by key will be possible in O(n) . But you can implement a hash table, in which, at best, the element will be retrieved in constant time.
And even if I manage to build a hash table based on the associated structure, it’s still time to get , for the last and first elements it will be different
Yes, it will vary. And it will be constant only if the desired item is first in the basket.
the option of using trees is excluded because have not yet passed
Even in this case, the time to retrieve an item is not always constant.
Please help with the idea how this should work in principle so that all the conditions of the task set before me are met
You need to implement a simple HashMap , but the 6th item will be executed only partially. Sometimes an element will be inserted / removed in a constant time, and sometimes linear or logarithmic (this depends on the data structure that will be used to resolve collisions).
The main idea of HashMap is that the hash code of the key is used to calculate the index of an element in the array (basket number). In the simplest case, the basket number can be obtained as:
int index = HASH_CODE % TABLE_LENGTH;
Where:
HASH_CODE - element hash key;TABLE_LENGTH - the size of the array that is used to store items (number of baskets).
To resolve collisions, you can use, for example, the chaining method (it is used in HashMap ), or open addressing (in this case, the element with the matching hash code is located in the next next basket).
Read how HashMap - you need to implement a similar data structure.
In general, your question is very general and voluminous. It is better to divide it into several questions, then you can give a specific answer.
O(1), since in the general case collisions are always possible. In the sameHashMapin the worst case (when all elements are in the same basket), the extraction will be forO(n)if the basket is implemented as a linked list and forO(log n)if the basket is implemented as a balanced tree. - post_zeew