About this lab
StudyFi Lab · Computer science

Data Structures

Data Structures is a free interactive simulation from StudyFi Lab. It runs right in your browser, with no sign-up and nothing to install.

Stacks, queues, lists, trees and graphs

Experiments

Stack

A stack works like a pile of plates: you add (push) and remove (pop) only at the top, so the last item in is the first one out (LIFO). The top pointer marks the top; push and pop both take O(1). Undo in an editor, the browser’s back button and function calls in a program (the call stack) all use one.

Try this: Push 1, 2, 3, then pop three times — the numbers come out in reverse order. Then try a ninth push.

Queue

A queue works like a line at a till: new items join at the back (enqueue, the rear pointer) and leave from the front (dequeue, the front pointer), so first in, first out (FIFO). In an array it is stored as a circular buffer: both pointers wrap from the last slot back to slot 0, so enqueue and dequeue take O(1). Queues run printer jobs, server requests and video buffering.

Try this: Turn on the circular view, enqueue six numbers, dequeue four and enqueue four more — watch rear wrap from slot 7 to slot 0.

Linked list

Each node holds a value and a next link to the following node; the last one points to null and the list starts at the head pointer. Inserting at the head costs O(1), but reaching position i or a searched value means walking node by node — O(n). Unlike an array nothing is shifted, you only relink; playlists, step histories and free-memory lists work this way.

Try this: Search for the last value and count the pointer’s steps, then insert at the head — that is always a single step.

Binary search tree

At every node, smaller values go left and larger ones go right. Searching and inserting therefore follow a single path from the root and take O(h), where h is the tree height — for a balanced tree h ≈ log₂ n. Deleting has three cases: a leaf just disappears, a node with one child is replaced by that child, and a node with two children takes the value of its in-order successor. An in-order traversal lists the values sorted; similar trees power database indexes.

Try this: Delete the root — it has two children, so you will see the successor search. Then try “Insert sorted values”: the tree degenerates into a list and the height grows to n.

Graph: BFS vs DFS

Breadth-first search (BFS) takes nodes from a queue, so it visits all neighbours of the start first, then their neighbours — layer by layer, which finds the shortest path in number of edges. Depth-first search (DFS) takes nodes from a stack and goes as deep as it can before backing up. Both visit every node and edge once: O(V + E). BFS powers route finding and “friends of friends”, DFS solves mazes and finds cycles.

Try this: Step from node 0 and compare the queue with the stack. Then change the start to node 7 — the visit orders change completely.

Frequently asked questions

What is Data Structures?

Data Structures is a free interactive simulation from StudyFi Lab. It runs right in your browser, with no sign-up and nothing to install. Stacks, queues, lists, trees and graphs

Is Data Structures free?

Yes. Data Structures is completely free. It works in the browser on computers, tablets and phones, and you do not need an account.

What can I explore in Data Structures?

Stack, Queue, Linked list, Binary search tree, Graph: BFS vs DFS.

What does the “Stack” experiment show?

A stack works like a pile of plates: you add (push) and remove (pop) only at the top, so the last item in is the first one out (LIFO). The top pointer marks the top; push and pop both take O(1). Undo in an editor, the browser’s back button and function calls in a program (the call stack) all use one.

What does the “Queue” experiment show?

A queue works like a line at a till: new items join at the back (enqueue, the rear pointer) and leave from the front (dequeue, the front pointer), so first in, first out (FIFO). In an array it is stored as a circular buffer: both pointers wrap from the last slot back to slot 0, so enqueue and dequeue take O(1). Queues run printer jobs, server requests and video buffering.

Studying for a test?

Upload your notes and StudyFi turns them into flashcards, tests and podcasts.

Try StudyFi free