When implementing Baglnterface where the data is stored in a
When implementing Baglnterface, where the data is stored in an array would it be more effective to beginning or the end of the array? Why? When implementing BagInterface, were the data is stored in a linked list, would it be more effective the beginning or the end of the linked list? Why? Implement add method in linked list that adds a given T data into the bag. Return false if the add is not s the header of the method is as follows: public boolean add (T new entry)
Solution
6)
SO, removal from end will be more efficient
Time : O(1)
remova from begining from an array means
you have to shift all other elements from second eleemnt
to till end, one by left (because first slot becomes empty after
removal of first one)
So, it takes O(n) time
7)
Removal from Begining is more efficient
Yes, because removing head node from a linked list is a
constant operation. You have to just change the head pointer to deconf element of list
Time: O(1)
Removal from end takes O(n) time
