Question

In: Computer Science

What structure would you select if a customer came and said, I want to have a...

What structure would you select if a customer came and said, I want to have a program where I would be updating the data (I would be putting more data in) and later I want to retrieve in their initial order (I want to get them out in the order they were inserted). What would the most sufficient structure to do that? Explain in your own words.

Solutions

Expert Solution

Following are detailed operations.

insert(x)
1) Check if x is already present by doing a hash map lookup.
2) If not present, then insert it at the end of the array.
3) Add in the hash table also, x is added as key and last array index as the index.

remove(x)
1) Check if x is present by doing a hash map lookup.
2) If present, then find its index and remove it from a hash map.
3) Swap the last element with this element in an array and remove the last element.
Swapping is done because the last element can be removed in O(1) time.
4) Update index of the last element in a hash map.

getRandom()
1) Generate a random number from 0 to last index.
2) Return the array element at the randomly generated index.

search(x)
Do a lookup for x in hash map.

Below is the implementation of the data structure.

/* C++ program to design a DS that supports folloiwng operations 
in Theta(n) time 
a) Insert 
b) Delete 
c) Search 
d) getRandom */
  
#include<bits/stdc++.h> 
using namespace std; 
  
// class to represent the required data structure 
class myStructure 
{ 
    // A resizable array 
    vector <int> arr; 
      
    // A hash where keys are array elements and vlaues are 
    // indexes in arr[] 
    map <int, int> Map; 
  
    public: 
    // A Theta(1) function to add an element to MyDS 
    // data structure 
    void add(int x) 
    { 
        // If ekement is already present, then noting to do 
        if(Map.find(x) != Map.end()) 
            return; 
              
        // Else put element at the end of arr[] 
        int index = arr.size(); 
        arr.push_back(x); 
              
        // and hashmap also 
        Map.insert(std::pair<int,int>(x, index)); 
    } 
          
    // function to remove a number to DS in O(1) 
    void remove(int x) 
    { 
        // element not found then return 
        if(Map.find(x) == Map.end()) 
            return; 
              
        // remove element from map 
        int index = Map.at(x); 
        Map.erase(x); 
              
        // swap with last element in arr 
        // then remove element at back 
        int last = arr.size() - 1; 
        swap(arr[index], arr[last]); 
        arr.pop_back(); 
              
        // Update hash table for new index of last element 
        Map.at(arr[index]) = index; 
    } 
          
    // Returns index of element if element is present, otherwise null 
    int search(int x) 
    { 
        if(Map.find(x) != Map.end()) 
        return Map.at(x); 
        return -1; 
    } 
          
    // Returns a random element from myStructure 
    int getRandom() 
    { 
        // Find a random index from 0 to size - 1 
        srand (time(NULL)); 
        int random_index = rand() % arr.size(); 
              
        // Return element at randomly picked index 
        return arr.at(random_index); 
    }      
}; 
  
// Driver main 
int main() 
{ 
    myStructure ds; 
    ds.add(10); 
    ds.add(20); 
    ds.add(30); 
    ds.add(40); 
    cout << ds.search(30) << endl; 
    ds.remove(20); 
    ds.add(50); 
    cout << ds.search(50) << endl; 
    cout << ds.getRandom() << endl; 
} 

Related Solutions

.So I have a project, I just want a general answer on how would you start...
.So I have a project, I just want a general answer on how would you start it. I have to make the Backend read/write from the database. What does that mean and how you u do that general example, please. in php please EX - would i just make a script that just connects to the database , or is it more to it
If you have a computer, why would you want an IP address? Why would you want...
If you have a computer, why would you want an IP address? Why would you want a URL? Would you ever want both? Why? Detailed answer please.
Select one of the elements of the Customer Development Manifesto, select what you believe is particularly...
Select one of the elements of the Customer Development Manifesto, select what you believe is particularly critical for founders. Why is it critical? Could you be a successful founder without it? Why or why not?
I want to design an algorithm decides on the notes to give a customer at a...
I want to design an algorithm decides on the notes to give a customer at a cash dispensing machine. The machine only has $20 and $50 notes and I want my algorithm to dispense the minimum number of notes to the customer. This means that if a customer wants $120 then the machine will dispense two $50 notes and one $20 note rather than six $20 notes. I propose an algorithm to solve this problem. My algorithm is: input: amount...
Turkey is the country I want Select a foreign country that interests you. Perhaps someone you...
Turkey is the country I want Select a foreign country that interests you. Perhaps someone you work with in your organization is from another country. Perhaps you have family from another country or perhaps you would like to travel to this country. Identify the country and why you selected this country. Provide a description about life in this country. You are not limited to the following information, but you should include: A brief description of the overall culture of this...
The term divisional organization refers to the ___________ form of organization structure. Select one: a. customer...
The term divisional organization refers to the ___________ form of organization structure. Select one: a. customer b. product c. geographic d. functional
Write an essay on why I want to be an educator. What I believe I have...
Write an essay on why I want to be an educator. What I believe I have to offer the students of our future?
Hi I have an exercise and I want to you to solve it step by step,...
Hi I have an exercise and I want to you to solve it step by step, please.. I found the solution here but it did not have steps .. *Answer the following questions using the information below: Tiger Pride produces two product lines: T-shirts and Sweatshirts. Product profitability is analyzed as follows: T-SHIRTS SWEATSHIRTS Production and sales volume 60,000 units 35,000 units Selling price $16.00 $29.00 Direct material $ 2.00 $ 5.00 Direct labor $ 4.50 $ 7.20 Manufacturing overhead...
Select an organization of your choice. You may select a company you have worked for, would...
Select an organization of your choice. You may select a company you have worked for, would like to work for, or design your own organization. Another option is to select two of the most admired companies or two companies of your choice and determine the organizational design and structures they use. In an essay, address the following topics: ? Describe organizational functions as they relate to the organizational structure, culture, and design of the organization(s). ? Define and provide an...
If you want to conduct a study on female students only you would have to have...
If you want to conduct a study on female students only you would have to have evidence that there was a reason not to include male students. Do you expect that the result on male versus female monkeys extends to male and female students? Give evidence to support your conclusion (I won’t accuse you of political incorrectness.) Design a study to test your hypothesis.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT