Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9278065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:16:07+00:00 2026-06-18T17:16:07+00:00

I am looking for a data structures that have the next features: – have

  • 0

I am looking for a data structures that have the next features:
– have key and value
– can find the value in O(n) > t > O(logn ) or O(1)
– can pull the first element and the last element I insert.

TreeMep is not good, because if I insert key (as string) “b” and then key “a” if I will pull the first one I will get “a” instead of “b”

ConcurrentSkipListMap is not good because I can’t rely on size func’

will appreciate any help

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:16:08+00:00Added an answer on June 18, 2026 at 5:16 pm

    You can use a deque (double ended queue) cross-referenced with a multimap (tipically a binary search tree), which allows duplicate keys.

    Every element of the queue would have a reference (an iterator) to the corresponding element of the map and vice-versa.

    This way, you can search in the map in O(log N) and you can push/pull at either ends of the sequence in O(log N).

    You can decide to store the strings in the map or in the queue.

    Here is an implementation in C++:

    #include <string>
    #include <map>
    #include <deque>
    
    typedef int         my_key_type;       // Key type
    typedef std::string my_value_type;     // Value type
    
    struct queueitem;                                   // Queue element
    
    typedef std::deque<queueitem> my_queue;             // Queue
    
    typedef std::multimap <my_key_type,               
                           my_queue::iterator> my_map;  // Map
    
    typedef std::pair <my_key_type,                  
                       my_queue::iterator> my_map_pair; // Map element
    
    struct queueitem
    {
        my_value_type value;
        my_map::iterator mapitem;
    
        queueitem (const my_value_type & val) : value(val) {}
    };
    
    class mapqueue
    {
        public:
    
            mapqueue () {}
    
            my_value_type find (my_key_type key) const;
            size_t index_of (my_key_type key) const;
    
            my_value_type front_value () const { return Q.front().value; }
            my_value_type back_value () const { return Q.back().value; }
    
            my_key_type front_key () const
            { return Q.front().mapitem->first; }
    
            my_key_type back_key () const
            { return Q.back().mapitem->first; }
    
            void push_front (my_key_type key,
                             const my_value_type & value);
    
            void push_back (my_key_type key,
                            const my_value_type & value);
    
            void pop_front ();
            void pop_back ();
    
        private:
    
            my_queue Q;
            my_map M;
    
            mapqueue (const mapqueue &) {}
            mapqueue & operator= (const mapqueue &) { return *this; }
    };
    
    using namespace std;
    
    my_value_type mapqueue::find (my_key_type key) const
    {
        my_map::const_iterator it = M.find (key);
    
        if (it==M.end())
            throw "Not found";
    
        return it->second->value;
    }
    
    size_t mapqueue::index_of (my_key_type key) const
    {
        my_map::const_iterator it = M.find (key);
    
        if (it==M.end())
            throw "Not found";
    
        return it->second - Q.begin();
    }
    
    void mapqueue::push_front (my_key_type key,
                               const my_value_type & value)
    {
        Q.push_front (queueitem(value));
        my_queue::iterator qit = Q.begin ();
        qit->mapitem = M.insert (my_map_pair(key,qit));
    }
    
    void mapqueue::push_back (my_key_type key,
                              const my_value_type & value)
    {
        Q.push_back (queueitem(value));
        my_queue::iterator qit = Q.end () - 1;
        qit->mapitem = M.insert (my_map_pair(key,qit));
    }
    
    void mapqueue::pop_front ()
    {
        M.erase (Q.front().mapitem);
        Q.pop_front ();
    }
    
    void mapqueue::pop_back ()
    {
        M.erase (Q.back().mapitem);
        Q.pop_back ();
    }
    

    This also supports finding the index in the queue of any given key in O(log N). If you don’t need this, you can simplify the design storing the strings in the map and removing the references from the map to the queue.

    Update: The key and value types are now specified via typedefs. This way it’s easy to change them. It would be interesting to convert the whole thing in a template parametrized by these two types. This way, the same code would be reusable for different pairs of key-value types even in the same program.

    Update: There is a tool for the general case: the Boost Multi-index Containers Library. See this example in the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for material on persistent data structures that can be used to implement
I'm looking for a data structure (or structures) that would allow me keep me
In C++ and Java, data structures can have private , public and protected regions.
I am looking for a data structure that holds the same values under two
I'm looking for a data structure that provides indexing for Rectangles. I need the
I'm looking for a Java built-in data structure that would be the best at
I'm looking for a sorted data structure, that will be similar to the STL
I'm looking for a tool that finds duplicate nodes in a tree data structure
I'm looking for an easy way of packing/unpacking data structures for sending over the
I'm looking for the proper data structure for this scenario. I have boost available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.