I want to keep a sorted queue of items, where I want to be able to pop the item with the lowest value (a bit like `std::priority_queue`). The item values are contiguous and ever increasing. However, the items do not have less-than comparison defined, they only have `is-previous` and `is-next` predicates (where `A is-previous B` == `B is-next A`), they also support `++` or `--`.

I wonder if there is any algorithm to (partially) sort values, based on such predicates? Alternately, is there a better way to keep track of such a queue?

This problem occurs when working in modulo arithmetic - the items are designated by integers, but thanks to the modulo, less than and greater than lose their meaning, and cannot be used for sorting anymore.

The language of choice is C++, but I guess I can port most of the reasonable languages.

**EDIT**

Appologies to all the people trying to answer, while writing the example code, I realized the original question was ill-formed. It is really embarasing, but I would like to emphasize that your time was not wasted, as I was sweating over it for one hour, and then in 10 minutes I realized how it is wrong. Anyway, this is the behavior I want:

```
template <class T>
class OrderedQueue {
    std::vector<T> storage;
    T next;

public:
    OrderedQueue(T lowest_value = T())
        :next(lowest_value)
    {}

    void Push(T t)
    {
        storage.push_back(t);
    }

    T Pop()
    {
        std::vector<T>::iterator it = std::find(storage.begin(), storage.end(), next);
        if(it == storage.end())
            throw std::runtime_error("no such element");
        T value = *it;
        storage.erase(it);
        ++ next;
        return value;
    }
};

```

My problem is that `Pop()` takes `O(N)`. Is there a way to make it faster, using the is-prev and is-next predicates?