Prashnikaप्रश्निका
Code
About

Stack and queue visualiser

Pick a stack, queue, circular queue or dequeue, give the size of its array and the operations to run, then press Play. You see the array, the pointers (top, or front and rear) moving after every operation, the cell that changed, overflow and underflow in red, and the Java line that did it. For the table ISC asks you to draw, use the stack and queue simulator.

How to use it

  1. 1Choose a stack, queue, circular queue or dequeue and the size of its array, then type the operations, or tap an example.
  2. 2Press Show me, then Play. After each operation the array and the pointers are redrawn and the Java line that did it is lit.
  3. 3Watch top, or front and rear, move; overflow and underflow turn the sentence red and change nothing.
What to run

Separate with commas. Stack: push 10, pop, peek. Queue and circular queue: insert 10 (or enqueue 10), delete (or dequeue), peek. Dequeue: addfront 10, addrear 10, removefront, removerear.

The animation needs JavaScript. Every step is written out below.

Result

Result
from the front: 3 4 5
Every step in words (8 steps)
  1. An empty queue with 5 cells: front = -1, rear = -1.
  2. insert 1: 1 is inserted at the rear: rear becomes 0.
  3. insert 2: 2 is inserted at the rear: rear becomes 1.
  4. insert 3: 3 is inserted at the rear: rear becomes 2.
  5. insert 4: 4 is inserted at the rear: rear becomes 3.
  6. delete: 1 is deleted from the front: front becomes 1.
  7. delete: 2 is deleted from the front: front becomes 2.
  8. insert 5: 5 is inserted at the rear: rear becomes 4.
Start again

About the Stack and queue visualiser

What it does

Runs push, pop, insert and delete on an array-based stack, linear queue, circular queue or dequeue and plays them back one operation at a time: the cell that was just filled or emptied is marked, top (or front and rear) is shown under the cell it points at, and a sentence says what happened. The Java method that did it is shown with the running line lit. It follows exactly the same rules as the stack and queue simulator, which gives the table that ISC questions ask you to draw.

The conventions it follows

  • Stack: top starts at -1; push is OVERFLOW when top is the last cell, pop is UNDERFLOW when top is -1.
  • Queue: front and rear start at -1; a linear queue never reuses the cells before front, so it can be full while cells at the start are free; when the last value leaves, front and rear go back to -1.
  • Circular queue: rear and front wrap round from the last cell to cell 0, so it is full only when every cell is filled.
  • Dequeue: a circular array with addFront, addRear, removeFront and removeRear.

The limits

An array of 1 to 12 cells and up to 40 operations. Separate the operations with commas, e.g. push 10, push 20, pop.

Common questions

What is overflow and underflow?

Overflow is trying to add to a structure that is full, underflow is trying to remove from one that is empty. Nothing changes in either case, and the visualiser shows the sentence in red.

Why does my linear queue say it is full when there are free cells at the front?

In a linear queue rear only moves forward, so once rear reaches the last cell nothing more can be inserted, even after deletions have freed the cells before front. Use a circular queue, where rear wraps round to cell 0 (tap the circular queue example).

How does a circular queue wrap round?

When rear (or front) is at the last cell, the next position is cell 0, calculated as (rear + 1) % size. Watch rear jump from the last cell back to 0 in the circular queue example.

Can I see the code in Python, JavaScript or C++?

Yes. Choose the language in the menu above the code. Only the listing changes: the steps are always worked out from the Java program that ISC uses, and the highlighted line moves to the matching line of the language you chose.

Practise on real ISC questions

More Computer Science tools

All study tools →