MCPcopy Create free account
hub / github.com/MolinDeng/Princeton-algs4 / dequeue

Method dequeue

02Lab-Queues/RandomizedQueue.java:57–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55
56 // remove and return a random item
57 public Item dequeue() {
58 if (isEmpty()) throw new NoSuchElementException();
59 int id = (first + StdRandom.uniformInt(n)) % v.length;
60 Item item = v[id];
61 v[id] = v[first];
62 v[first++] = null;
63 n--;
64 if (first == v.length) first = 0;
65 if (n > 0 && n == v.length / 4) resize(v.length / 2);
66 return item;
67 }
68
69 // return a random item (but do not remove it)
70 public Item sample() {

Callers 2

mainMethod · 0.95
findSAPMethod · 0.45

Calls 2

isEmptyMethod · 0.95
resizeMethod · 0.95

Tested by

no test coverage detected