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

Stack and queue simulator

Pick a stack, queue, circular queue or dequeue, give the size of its array and the operations to run, and see the array, the top (or front and rear) and what happened after every step - overflow and underflow included. The table ISC Computer Science data-structure questions ask you to draw.

How to use it

  1. 1Choose what to run: a stack, a queue, a circular queue or a dequeue, and the size of its array.
  2. 2Type the operations in order, e.g. push 10, push 20, pop, or tap an example.
  3. 3Press Run it and read the array, the pointers and what happened after every step.
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.

Step by step

Operation01234frontrearWhat happens
start·····-1-1an empty queue of 5 cells
addrear 1010····0010 is inserted at the rear: rear becomes 0
addrear 201020···0120 is inserted at the rear: rear becomes 1
addfront 51020··5415 is inserted at the front: front becomes 4
addfront 11020·15311 is inserted at the front: front becomes 3
removerear10··153020 is deleted from the rear: rear becomes 0
removefront10···5401 is deleted from the front: front becomes 4
removefront10····005 is deleted from the front: front becomes 0

The numbers above the cells are their positions, counted from 0. A red row is an operation that could not be done.

At the end

1 element, front to rear: 10

The rules followed

  • The dequeue is a circular array. addrear works like a circular queue's insert; addfront moves front back by 1 (from 0 to the last cell) and stores the value there.
  • removefront works like a circular queue's delete; removerear takes the value at rear and moves rear back by 1. A full dequeue is OVERFLOW and an empty one UNDERFLOW.
Start again

About the Stack and queue simulator

What it does

Runs push, pop, insert and delete on an array-based stack, linear queue, circular queue or dequeue, one operation at a time, and shows the array, the pointers (top, or front and rear) and the result after each one. Overflow and underflow are reported instead of changing anything. It is the table that ISC Computer Science data-structure questions ask you to draw.

The conventions it follows

  • Stack: top starts at -1. Push is OVERFLOW when top is the last cell, otherwise top goes up by 1. 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 from the last cell to cell 0, so every cell can be used; it is full only when every cell is filled.
  • Dequeue: a circular array that you can add to and remove from at both ends: addfront, addrear, removefront, removerear.

How to write the operations

Separate them 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.

Common questions

What is the difference between overflow and underflow?

Overflow is trying to add to a structure that is full; underflow is trying to remove from one that is empty. In both cases nothing changes.

Why does my linear queue say it is full when there is space at the front?

In a linear queue rear only moves forward, so once rear reaches the last cell no more values can be inserted, even after deletions have freed the cells before front. A circular queue fixes this by wrapping rear round to cell 0 (try the circular queue example).

How is a circular queue different from a dequeue?

A circular queue adds only at the rear and removes only at the front. A dequeue (double-ended queue) can add and remove at both ends.

Practise on real ISC questions

More Computer Science tools

All study tools →