Stacks and Queues for app.codility.com/programmers Explained in Java
Category of Article : Technology | Computers and Software | Algorithms
By: Chrysanthus Date Published: 4 Jul 2025
Stack
The stack container adapter (or stack for short), is a data structure in which the insertion of a new element takes place at the top, and the deletion (removal) of an element, also takes place from the top. The idea of the stack can be illustrated by plates stacked on top of one another. Each new plate is placed on top of the stack of plates (operation push), and plates can only be taken off the top of the stack (operation pop). This is a Last-In-First-Out (LIFO) data structure.Use of push() and pop() methods for the Stack
The following program pushes the list of integers,4, 8, 6, 3
one-by-one onto a stack, and then pops them out one-by-one from the top of the stack. The stack begins with the number 4 and then ends with the number 3. So, in the popping operations, 3 will be sent out first. The program is:
import java.util.Stack;
public class temp {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(4); stk.push(8); stk.push(6); stk.push(3);
int int1 = stk.peek(); stk.pop();
int int2 = stk.peek(); stk.pop();
int int3 = stk.peek(); stk.pop();
int int4 = stk.peek(); stk.pop();
System.out.println(int1 + ", " + int2 + ", " + int3 + ", " + int4);
}
}
The output is: 3, 6, 8, 4
in reverse order, as expected, since it is last-in-first-out. The peek() method copies out the last (top) element, while the pop() method removes the last (top) element and throws it away (in this code). push() returns void. Note the use of the dot operator, between the name of the stack, stk and the method, push() or peek() or pop().
Note as well: The push() or peek() or pop() method takes O(1) time. The sequence of operations for the whole list takes O(n) time, with pushing or with popping.
Queue
The queue is a data structure in which a new element is inserted at the back, but an old element is removed from the front, in order. The idea of the queue can be illustrated by a line of customers at a grocery store. New people join the back of the queue (offer operation) and the next person to be served, is the one in front (remove operation), of the line (queue). After serving, he/she goes away. This is a First-In-First-Out (FIFO) data structure.Use of offer() and remove() methods for the Queue
The following program pushes the list of integers,4, 8, 6, 3
one-by-one into a queue from the back, and then removes them out one-by-one from the front (top) of the queue. The queue begins with the number 4 and then ends with the number 3. So, in the removing (fronting) operations, 4 will be sent out first. The program is:
import java.util.Queue;
import java.util.LinkedList;
public class temp {
public static void main(String[] args) {
Queue<Integer> qu = new LinkedList<>();
qu.offer(4); qu.offer(8); qu.offer(6); qu.offer(3);
int int1 = qu.peek(); qu.remove();
int int2 = qu.peek(); qu.remove();
int int3 = qu.peek(); qu.remove();
int int4 = qu.peek(); qu.remove();
System.out.println(int1 + ", " + int2 + ", " + int3 + ", " + int4);
}
}
The output is:4, 8, 6, 3
in same order, as expected, since it is first-in-first-out. The peek() method copies out the first (top) element, while the remove() method removes the first (top) element and throws it away (for this code). offer() returns false if the queue is full. Note the use of the dot operator, between the name of the queue, qu and the method, offer() or peek() or remove().
Note as well: The offer() or peek() or remove() method takes O(1) time. The sequence of operations for the whole list takes O(n) time, with offering or with removing.
The size() method for the Stack and the Queue
A stack or a queue object, has the size() method. This returns the length of the stack or queue object. The following program illustrates this:
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class temp {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(4); stk.push(8); stk.push(6); stk.push(3);
Queue<Integer> qu = new LinkedList<>();
qu.offer(4); qu.offer(8); qu.offer(6); qu.offer(3);
System.out.println("The size of the stack is: " + stk.size() + " and the size of the queue is: " + qu.size());
}
}
The output is:The size of the stack is: 4 and the size of the queue is: 4