I'm not sure you understand this, but the proposed implementation does a linear search on a linked list. If you push 2000 items on the stack with an average of two duplicates of each item value, then 2000 searches of a linked list averages 500-750 links (it depends on when, IE: what order, duplicates are presented in the search function. This requires 1 million + comparison. Not very.
More efficient duplicate detection in find_value () above can use a hash table with O (1) lookup time or a tree with O (log N) lookup time. The first, if you know how many values ββyou potentially push on the stack, and the second, if the number is unknown, for example, when receiving data from a socket in real time. (if you could implement your stack in an array instead of a much slower and more detailed linked list)
In any case, to properly support the hash table, the pop () function must be paired with the hashpop () function of the hash table, which removes the corresponding value from the hash table.
With Hashtable, your stack can simply point to the value of the element sitting in its hash location - it returns from find_value (). However, using a tree with a balanced location, the node location and therefore the value of the element will change all the time, so you will need to save the value of the element on the stack and the tree. If you are not writing in a very compressed memory environment, the performance that the second data structure could provide would be modest in memory.
source share