I need to create a data structure based on Linked list, Array and read-only memory.
As input, I get two numbers: N and M.
N represents the maximum disk capacity per key, M represents the maximum capacity of the computerโs hard drive, so M> N.
Therefore, I need to create a program that "transfers" information from the hard disk to disk-to-key. This program should implement the following methods:
- Insert (data) - Inserts data into a disk-on-key, if it is full, it deletes the data of least importance (*): the worst execution time is O (1).
- delete (data) - deletes data from disk to key - O (1)
(*) user can change the value of the file.
Maximum Memory Usage - O (M)
What i have done so far:
I created an array [1 ... M] that will "hold" the computer data, I created a doubly linked list in which data will be stored on disk. [The idea is this: every time the data is added to disk-by-key, they will be added to the linked list, and I can go directly to the data using the array as an index (/ key) storage.]
My computer data fields:
node firstNode; node currentNode; node[] dataCollection;
So, I wanted to create a method that replaces the least important data with the data I want to add [so that I can use it in Insert], My code to replace:
public void replace(int leastImportantdata, int insertData){ node leastImportant = dataCollection[leastImportantdata]; if (leastImportant.prev!=null) leastImportant.prev.next=dataCollection[insertData-1]; if (leastImportant.next!=null) leastImportant.next.prev=dataCollection[insertData-1]; numOfReplacements++;
So, my main problem is finding the least important data considering these two โcombinedโ data structures and still preserving the O (1) runtime, especially when the user decides to change the importance of the files.
- Suppose we start with {4,3,2,1} (numbers are important) the least important data will be 1. Suddenly, the user decided to change the value of the last file to 5, we get {4,3, 2,5}, and the least important data - 2.
Any idea?