Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Container for a stack of unique elements

Tags:

c++

What I'm trying to do is to construct a stack which contains unique elements.

And if an element is pushed to which is already in stack the element is not pushed but the existing elemetn should be moved to the top of the stack, i.e. ABCD + B > ACDB

I would like to here from you which container will be the best choice to have this functionality.

I decided to user stack adapter over list, because

  1. list does provide constant time for element move
  2. list is one of the natively supported containers for the stack.

The drawback of my choice is that I have to manually check for the duplicate elements.

P.S. My compiler is not so recent so please don't suggest unordered_set.

like image 313
deimus Avatar asked Aug 23 '12 10:08

deimus


People also ask

What is stack container?

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only.

Which STL collection guarantees the uniqueness of the stored content?

set::begin() and set::end() in C++ STL Sets are a type of associative container in which each element has to be unique because the value of the element identifies it.

How do you find the elements of a stack?

top() is used to access the element at the top of the stack container. In a stack, the top element is the element that is inserted at the last or most recently inserted element.


2 Answers

Basically you have to chose between constant time moving + long search, or constant time search + long moving.

It's hard to say which would be more time-consuming, but consider this:

  • You will have to search for if the element exists every time you try to add an element
  • You will not have to move elements every time, since obviously at some times you will be adding elements that are "new" for the container.

I'd suggest you to store elements and their stack positions in different containers. Store elements in a way that provides fast search, store stack positions in a way that provides fast movement. Connect both with pointers (so you can know which element is on which position, and which position holds which element <-- messy phrase, I know it!), and you will perform stuff rather fast.

like image 130
SingerOfTheFall Avatar answered Oct 11 '22 17:10

SingerOfTheFall


From your requirements, it seems to me that the structure you want could be derived from a Max Heap.

If instead of storing just the item, you store a pair (generation, item), with generation coming from a monotonically increasing counter, then the "root" of the heap is always the last seen element (and the other elements do not really matter, do they ?).

  • Pop: typical pop operation on the heap (delete-max operation)
  • Push: modified operation, to account for uniqueness of "item" within the structure
    • look for "item", if found update its generation (increase-key operation)
    • if not, insert it (insert operation)

Given the number of elements (20), building the heap on a vector seems a natural choice.

like image 24
Matthieu M. Avatar answered Oct 11 '22 17:10

Matthieu M.