A Fifo queue with fast insertion , deletion and get front element java -
which data structure should use in java implement queue fast insertion @ end , deletion front , , operation first element?
wouldn't linkedlist
fit bill?
its implementation
public e remove() { return removefirst(); } public boolean add(e e) { linklast(e); return true; }
it has both first
, last
nodes, insertion fast @ end. can delete front method remove()
. can first element well, ie. peek()
returns first
node. that's o(1)
.
source
void linklast(e e) { final node<e> l = last; final node<e> newnode = new node<>(l, e, null); last = newnode; if (l == null) first = newnode; else l.next = newnode; size++; modcount++; }
Comments
Post a Comment