MCPcopy Create free account
hub / github.com/careercup/ctci / Stack

Class Stack

java/Chapter 3/Question3_3/Stack.java:3–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1package Question3_3;
2
3public class Stack {
4 private int capacity;
5 public Node top;
6 public Node bottom;
7 public int size = 0;
8
9 public Stack(int capacity) {
10 this.capacity = capacity;
11 }
12
13 public boolean isFull() {
14 return capacity == size;
15 }
16
17 public void join(Node above, Node below) {
18 if (below != null) below.above = above;
19 if (above != null) above.below = below;
20 }
21
22 public boolean push(int v) {
23 if (size >= capacity) return false;
24 size++;
25 Node n = new Node(v);
26 if (size == 1) bottom = n;
27 join(n, top);
28 top = n;
29 return true;
30 }
31
32 public int pop() {
33 Node t = top;
34 top = top.below;
35 size--;
36 return t.value;
37 }
38
39 public boolean isEmpty() {
40 return size == 0;
41 }
42
43 public int removeBottom() {
44 Node b = bottom;
45 bottom = bottom.above;
46 if (bottom != null) bottom.below = null;
47 size--;
48 return b.value;
49 }
50}
51

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected