Stack & Queue
Initialization
Stack<String> stack = new Stack<>(); Queue<String> queue = new LinkedList<>();Useful methods
element = stack.push(element); // push one in, O(1)
element = stack.pop(); // pop one out, O(1)
element = stack.peek(); // see the top element, O(1)
stack.isEmpty(); // return true or false
l = stack.length();q.offer(element);  // O(1), out of bound raise Exception
q.poll(); // O(1), out of bound raise Exception
l = q.size();
Last updated
Was this helpful?