First off, I would like to mention the question is a homework question. I have been pondering about the implementation for long enough.
I have to think and implement a library software that has the following functionalities:
- add/remove a new subscriber.
- borrow/return a book.
- what books the following subscriber have?
- which subscriber holds the following book?
- list of the subscribers with most books.
I thought of implementing a heap and 2 red black trees, the problem is that the space complexity is high. So I was wondering if I am missing something.
The subscribers are stored by I.Ds, the books have code names. One Red Black tree is for the subscribers and the other is for the borrowed books. The heap is a max heap, in order to implement the last requirement开发者_如何学Go.
I cannot use anything else but data structures.
Thanks for any insights and answers.
I guess you can also use containers, like structs? Use:
- One hash set/hash table for the books:
Additionally store a flag which determines whether the book is borrowed and the subscriber it's borrowed to. - One hash map from subscriber->linked list of books, to store not only all the subscribers but also which books they have borrowed.
This allows you to perform all listed tasks in O(1), except sorting the subscribers by the count of the books they have borrowed.
精彩评论