Skip to content
Computer ScienceISC Class 12 · Data Structures

Stack, Queue, Circular Queue and Dequeue in Java (ISC Class 12 Computer Science)

Updated 17 October 2026 · Draft, not yet fact-checked

A stack is last-in first-out (push, pop at the top); a queue is first-in first-out (add at the rear, remove from the front); a circular queue reuses freed space with rear = (rear + 1) % n; a dequeue allows insertion and deletion at both ends. Each is defined as an abstract data type with a clear interface and implemented in a class using an array.

Understand Stack, Queue, Circular Queue and Dequeue in Java

Abstract data type (ADT). A data structure is defined by its operations and their meaning (its interface) and not by how it is stored. In Java the operations can be written as an interface and implemented by one or more classes (for example an array version and a linked version).

Stack (LIFO). Elements are added and removed at one end, the top. Operations: push(x), pop(), peek(), isEmpty(), isFull(). Array implementation with top = -1 for empty:

class Stack {
    int[] a; int top;
    Stack(int n) { a = new int[n]; top = -1; }
    void push(int x) {
        if (top == a.length - 1) System.out.println("Overflow");
        else a[++top] = x;
    }
    int pop() {
        if (top == -1) { System.out.println("Underflow"); return -1; }
        return a[top--];
    }
}

Overflow is adding to a full structure; underflow is removing from an empty one. Applications: undo, reversing, checking brackets, converting and evaluating expressions, recursion.

Queue (FIFO). Elements enter at the rear and leave from the front. In a simple array queue, front and rear only move forward, so after some removals the array may report full although cells at the start are free.

Circular queue. The array is treated as a ring, with rear = (rear + 1) % n and front = (front + 1) % n. A counter count tells full (count == n) from empty (count == 0):

void enqueue(int x) {
    if (count == n) System.out.println("Overflow");
    else { rear = (rear + 1) % n; a[rear] = x; count++; }
}
int dequeue() {
    if (count == 0) { System.out.println("Underflow"); return -1; }
    int x = a[front]; front = (front + 1) % n; count--; return x;
}

(start with front = 0, rear = -1, count = 0).

Dequeue (double-ended queue). Insertion and deletion are allowed at both ends: addFront, addRear, removeFront, removeRear. It can act as a stack (use one end) or as a queue (add at the rear, remove from the front).

Expression conversion with a stack is in the next topic, and the linked list in the one after.

Stack, Queue, Circular Queue and Dequeue in Java: key facts

Stack operations
Emptytop = -1
Fulltop = n - 1
pusha[++top] = x
popreturn a[top--]
LIFO: the last pushed leaves first.
Queue ends
  • Add at the rear
  • Remove from the front
  • FIFO
  • Simple queue can waste freed space
The order of service is the order of arrival.
Circular queue
  • rear = (rear + 1) % n
  • front = (front + 1) % n
  • Full: count == n
  • Empty: count == 0
Indices wrap round to 0.
Dequeue
  • addFront and addRear
  • removeFront and removeRear
  • Both ends open
  • Works as a stack or a queue
Also called a double-ended queue.

How to answer Stack, Queue, Circular Queue and Dequeue in Java questions

Use this routine to trace stack and queue operations.

  1. 1Draw the array with indices, and write top (or front, rear and count) beside it.
  2. 2Apply the operations one by one: check overflow or underflow before every insertion or removal.
  3. 3For a circular queue, compute the new rear or front with the % n rule.
  4. 4Write the removed value for each pop or dequeue and the print line for each overflow or underflow.
  5. 5After the last operation, list the structure in order (bottom to top, or front to rear).
  6. 6State the final top, or front and rear.

Stack, Queue, Circular Queue and Dequeue in Java in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Stack of plates and a ticket queue

Suppose a canteen keeps clean plates in a pile and a ticket counter serves customers in the order they arrive.

Which structure does each follow?

Try it first, then show the solutionSolution
  1. The plate placed last is used first: stack (LIFO).
  2. The first customer is served first: queue (FIFO).

Answer: Plates: stack. Ticket counter: queue.

Memory hook: Stack up, queue in line.

Ring buffer of print jobs

Suppose a printer has room for 4 jobs in a ring, and new jobs are written after the last job, wrapping round to the start after the end.

Where does the next job go if the last job is at index 3 and index 0 is free?

Try it first, then show the solutionSolution
  1. rear = (3 + 1) % 4 = 0.
  2. The new job is stored at index 0.

Answer: Index 0.

Memory hook: After the last seat comes the first, like a roundabout.

Tricks to remember Stack, Queue, Circular Queue and Dequeue in Java

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • LIFO and FIFO

    Stack = Last In First Out, Queue = First In First Out.

    Use it when: Definitions.

  • Plus one, mod n

    Move the pointer by 1 and wrap with % n.

    Use it when: Circular queue indices.

  • Full or empty by count

    rear sits just behind front (modulo n) in both the full and the empty state, so front and rear alone cannot tell them apart; keep a count.

    Use it when: Circular queue conditions.

  • Check first, then act

    Test overflow before inserting and underflow before removing.

    Use it when: Writing push, pop, enqueue, dequeue.

Stack, Queue, Circular Queue and Dequeue in Java: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Name the principle · 1 mark · MCQBoard level

The principle followed by a stack is
(a) FIFO (b) LIFO (c) random access (d) priority

Try it first, then show the solutionSolution
  1. The last element pushed is the first removed.

Answer: (b) LIFO

Example 2: Stack trace with overflow · 4 marks · Short answerHarder board level

A Stack object s has capacity 3. Write the output and the final top:

s.push(5); s.push(8); s.push(2); s.push(9);
System.out.println(s.pop() + " " + s.pop());
s.push(7);
System.out.println(s.top);
Try it first, then show the solutionSolution
  1. Pushes 5, 8, 2 fill the stack (top = 2). push(9) finds top == 2 == n - 1 and prints Overflow. (1.5 marks)
  2. The two pops return 2 and then 8, so it prints 2 8; top is now 0. (1.5 marks)
  3. push(7) stores 7 at index 1, so top = 1 and the stack is 5, 7 from the bottom. Output: Overflow / 2 8 / 1. (1 mark)

Answer: Overflow
2 8
1

Example 3: Circular queue indices · 4 marks · Short answerHarder board level

A circular queue of size 4 starts empty (front = 0, rear = -1, count = 0). The operations are: enqueue A, B, C, D, E; dequeue twice; enqueue E; enqueue F. Show the outcome of each and the final array, front and rear.

Try it first, then show the solutionSolution
  1. A, B, C, D go to indices 0 to 3 (rear = 3, count = 4). The first E finds count == 4 and prints Overflow. (1 mark)
  2. Two dequeues return A and B: front = 2, count = 2. (1 mark)
  3. enqueue E: rear = (3 + 1) % 4 = 0, so a[0] = E; enqueue F: rear = 1, a[1] = F; count = 4. (1.5 marks)
  4. Array: E F C D with front = 2 and rear = 1; the service order is C, D, E, F. (0.5 mark)

Answer: Array [E, F, C, D], front = 2, rear = 1; order of removal C, D, E, F

Example 4: Dequeue operations · 3 marks · Short answerHarder board level

A dequeue is empty. Perform addRear(3), addRear(5), addFront(1), addFront(9), removeRear(), removeFront(). Give the values removed and the final content from front to rear.

Try it first, then show the solutionSolution
  1. After the adds the content from front to rear is 9, 1, 3, 5. (1.5 marks)
  2. removeRear gives 5, leaving 9, 1, 3; removeFront gives 9, leaving 1, 3. (1.5 marks)

Answer: Removed 5 and then 9; final content 1, 3

Example 5: Case: bracket check with a stack · 4 marks · Case-basedHOTS / competency

Suppose an editor checks brackets in (a+b)*(c using a stack: push for each (, pop for each ), and report unbalanced if a ) arrives on an empty stack or the stack is not empty at the end.
(i) Show the stack size after each bracket. (2 marks)
(ii) State the verdict. (1 mark)
(iii) Name the structure used and the principle. (1 mark)

Try it first, then show the solutionSolution
  1. (i) The brackets are ( ) (: sizes after each are 1, 0, 1.
  2. (ii) The stack is not empty at the end (size 1), so the expression is unbalanced.
  3. (iii) A stack, following LIFO: the most recent opening bracket is matched first.

Answer: (i) 1, 0, 1 (ii) unbalanced (iii) stack, LIFO

Where students lose marks in Stack, Queue, Circular Queue and Dequeue in Java

The slips examiners see most often, and what to write instead.

  • Mistake: No overflow or underflow test

    A push on a full array and a pop on an empty one both throw ArrayIndexOutOfBoundsException (a[-1] on pop).

    Wrong working

    void push(int x) { a[++top] = x; }

    Right working

    if (top == a.length - 1) overflow else a[++top] = x;

    Do this: Test before changing the pointer.

    Usually costs: 1 mark

  • Mistake: Forgetting % n

    The index moves past the end of the array instead of wrapping.

    Wrong working

    rear = rear + 1;

    Right working

    rear = (rear + 1) % n;

    Do this: Apply % n to both front and rear.

    Usually costs: 1 mark

  • Mistake: Mixing LIFO and FIFO

    The removal end decides the structure.

    Wrong working

    A queue removes the newest element

    Right working

    A queue removes the oldest element

    Do this: Say stack = last in first out, queue = first in first out.

    Usually costs: 1 mark

Exam tips

  • Draw the array with index numbers and the pointers after each operation.
  • Print Overflow or Underflow exactly as the question states.
  • Write the circular index rule explicitly: (rear + 1) % n.
  • Mention that a data structure is an abstract data type with a defined interface.
  • For a dequeue, name the end (front or rear) in every operation.

Practice questions on Stack, Queue, Circular Queue and Dequeue in Java

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which data structure removes the element that was inserted first?
    (a) stack (b) queue (c) tree (d) array

    Hint

    First in, first out.

    Answer

    (b) queue

  2. Q2 · 2 marks · Short answerBoard level

    Define overflow and underflow. Why is a circular queue better than a simple array queue?

    Hint

    Think of cells freed at the front.

    Answer

    Overflow is trying to insert into a full structure; underflow is trying to remove from an empty one. A circular queue wraps the indices round with % n, so cells freed by deletions are reused, which a simple array queue cannot do.

  3. Q3 · 3 marks · Short answerHarder board level

    An array queue of size 3 holds 1, 2, 3. Two items are removed and 4 is then added. Show why a simple queue reports Overflow but a circular queue accepts 4.

    Hint

    Follow front and rear.

    Answer

    After the two removals front = 2 and rear = 2, with two free cells at indices 0 and 1. In a simple queue rear == n - 1 = 2, so it reports Overflow. In a circular queue, count is 1, so there is space: rear = (2 + 1) % 3 = 0 and 4 is stored at index 0.

  4. Q4 · 3 marks · Short answerHarder board level

    Write a method of class Stack that displays the elements from the top to the bottom without removing them, and give its output for the stack 4, 7, 1 (1 on top).

    Hint

    Loop from top down to 0.

    Answer
    void display() {
        for (int i = top; i >= 0; i--)
            System.out.print(a[i] + " ");
    }

    With a = {4, 7, 1} and top = 2, the output is: 1 7 4

  5. Q5 · 4 marks · Case-basedHOTS / competency

    A queue holds 1, 2, 3 (1 at the front). Using one stack, reverse the queue so that 3 is at the front. Show the steps, the number of operations, and say which two principles are combined.

    Hint

    Empty the queue into the stack and then back.

    Answer

    Dequeue 1, 2, 3 and push each: the stack holds 1, 2, 3 with 3 on top. Pop 3, 2, 1 and enqueue each in turn, giving the queue 3, 2, 1 with 3 at the front. The operations are 3 dequeues, 3 pushes, 3 pops and 3 enqueues, 12 in all. FIFO of the queue and LIFO of the stack together reverse the order.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.What is the value of top for an empty array stack?Show answer

    -1.

  2. 2.Which end does a queue remove from?Show answer

    The front.

  3. 3.What is the advantage of a circular queue?Show answer

    It reuses freed space.

  4. 4.What condition shows a full circular queue with a counter?Show answer

    count == n.

  5. 5.How many ends can a dequeue use for insertion?Show answer

    Both ends.

  6. 6.What is underflow?Show answer

    Removing from an empty structure.

Stack, Queue, Circular Queue and Dequeue in Java: frequently asked questions

What is the difference between a stack and a queue?

A stack works on one end (last in, first out); a queue adds at one end and removes at the other (first in, first out).

What is a circular queue?

A queue implemented on an array treated as a ring, where front and rear wrap round to index 0 after the last cell, so freed cells are reused.

What is a dequeue?

A double-ended queue in which elements can be inserted and removed at both the front and the rear.

Why define a data structure as an abstract data type?

So its users depend only on the operations and not on the storage, which allows an array version to be replaced by a linked version without changing user code.