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
insert 11····001 is inserted at the rear: rear becomes 0
insert 212···012 is inserted at the rear: rear becomes 1
insert 3123··023 is inserted at the rear: rear becomes 2
insert 41234·034 is inserted at the rear: rear becomes 3
delete·234·131 is deleted from the front: front becomes 1
delete··34·232 is deleted from the front: front becomes 2
insert 5··345245 is inserted at the rear: rear becomes 4

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

3 elements, front to rear: 3, 4, 5

The rules followed

  • front and rear start at -1. Insert: if rear is the last cell it is OVERFLOW (cells before front are not reused); otherwise rear goes up by 1 (the first insert sets front and rear to 0).
  • Delete: if front is -1 it is UNDERFLOW; otherwise the value at front is removed and front goes up by 1. When the last value leaves, front and rear go back to -1.
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 →