If you really want to do that, set a flag in your entry. When you want to remove an entry from your file, simply invalidate that flag(logical removal) w/o deleting it physically. The next time you add an entry, just go through the file, look for the first invalidated entry, and overwrite it. If all are validated, append it to the end. This takes O(1)
time to remove an entry and O(n)
to add a new entry, assuming that reading/writing a single entry from/to disk is the basic operation.
You can even optimize it further. At the beginning of the file, store a bit map(1
for invalidated). E.g., 0001000...
represents that the 4th entry in your file is invalidated. When you add an entry, search for the first 1
in the bit map and use Random file I/O (in contrast with sequential file I/O) to redirect the file pointer to that entry-to-overwrite directly. Adding in this way only takes O(1)
time.
Oh, I notice your comment. If you want to do it efficiently with entry removed physically, a simple way is to swap the entry-to-remove with the very last one in your file and remove the last one, assuming your entries are not sorted. The time is also good, which is O(1)
for both adding and removing.
Edit: Just as Joe mentioned, this requires that all of your entries have the same size
. You can implement one with variable length of entries, but that'll be more complicated than the one in discussion here.