Skip to main content

LinkedList

LinkedLists are sequences of nodes where each node contains data and connects to the next node. They are perfect for visualizing dynamic data structures and pointer-based algorithms.

Source Code

1linkedlist chain = {
2 nodes: [head, node1, node2, tail]
3 value: [10, 20, 30, 40]
4 color: ["green", "blue", "blue", "red"]
5 arrow: ["start", null, null, "end"]
6}
7
8page
9show chain

Rendered Diagram

Properties

LinkedLists support the following properties:

PropertyTypeDescription
nodesid[]List of node identifiers
value(number | string | null)[]Values for each node
color(color | null)[]Colors for each node
arrow(number | string | null)[]Arrows or labels for each node
abovestring | text_object (see special features)Label positioned above the list
belowstring | text_objectLabel positioned below the list
leftstring | text_objectLabel positioned to the left
rightstring | text_objectLabel positioned to the right

Methods

LinkedLists support these methods for manipulation:

Text Positioning Methods

  • setText(text, position) - Set or remove text at specific positions ("above", "below", "left", "right")

Node Methods

  • addNode(name, value?) - Add new node with optional value
  • insertNode(index | id, name, value?) - Insert node at specific position or after node ID with optional value
  • removeNode(name) - Remove specific node
  • setValue(index | id, value) - Set value for node at index or node id
  • setColor(index | id, color) - Set color for node at index or node id
  • setArrow(index | id, arrow) - Set arrow for node at index or node id

Value Methods

  • addValue(value) - Add value to end of list
  • removeValue(value) - Remove first occurrence of value
  • removeAt(index) - Remove node at specific position

Multiple Element Methods

  • setValues([...]) - Set multiple node values
  • setColors([...]) - Set multiple node colors
  • setArrows([...]) - Set multiple node arrows

Examples

Basic LinkedList Example

LinkedList with Pointers

Source Code

1linkedlist pointers = {
2 nodes: [head, curr, next, end]
3 value: [1, 2, 3, null]
4 color: ["green", "blue", "yellow", "red"]
5 arrow: ["head", "current", "next", "null"]
6}
7
8page
9show pointers

Rendered Diagram

Dynamic LinkedList Operations

Source Code

1linkedlist list = {
2 nodes: [n1, n2]
3 value: [10, 20]
4}
5
6page
7show list
8
9page
10list.addNode(n3, 30)
11list.setColor(2, "green")
12list.setArrow(2, "new")
13
14page
15// insert by index
16list.insertNode(1, n1_5, 15)
17list.setColors([null, "blue", null, "green"])
18list.setArrows([null, "inserted", null, null])
19
20page
21// insert after node ID
22list.insertNode(n1_5, n1_75, 17.5)
23list.setColors([null, "blue", "yellow", null, "green"])
24list.setArrows([null, "inserted", "after n1_5", null, null])
25
26page
27list.removeNode(n2)
28list.setValues([10, 15, 30])
29list.setColors([null, null, "green"])
30list.setArrows([null, null, null])

Rendered Diagram

LinkedList Traversal

Source Code

1linkedlist traverse = {
2 nodes: [start, mid1, mid2, end]
3 value: ["first", "second", "third", "last"]
4 color: [null, null, null, null]
5}
6
7page
8show traverse
9
10page
11traverse.setColor(0, "blue")
12traverse.setArrow(0, "visiting")
13
14page
15traverse.setColor(0, "gray")
16traverse.setColor(1, "blue")
17traverse.setArrow(0, "visited")
18traverse.setArrow(1, "visiting")
19
20page
21traverse.setColor(1, "gray")
22traverse.setColor(2, "blue")
23traverse.setArrow(1, "visited")
24traverse.setArrow(2, "visiting")

Rendered Diagram

Sorted LinkedList Insertion

Source Code

1linkedlist sorted = {
2 nodes: [n10, n30, n50]
3 value: [10, 30, 50]
4 above: "Sorted LinkedList"
5 below: "Inserting 25"
6}
7
8page
9show sorted
10
11page
12// insert after n10 node
13sorted.insertNode(n10, n25, 25)
14sorted.setColor(1, "red")
15sorted.setArrow(1, "inserted")

Rendered Diagram

Use Cases

LinkedLists are ideal for:

  • Dynamic data structures - Variable-length lists, memory allocation
  • Insertion/deletion operations - Efficient mid-list modifications
  • Pointer manipulation - Understanding references and links
  • Queue implementation - FIFO using linked nodes
  • Stack implementation - Alternative to array-based stacks
  • Graph representations - Adjacency lists
  • Memory management - Free lists, garbage collection
  • Algorithm visualization - Merging, splitting, reversing lists
  • Methods Reference - Complete list of linkedlist methods
  • Array - Alternative linear structure
  • Stack - LIFO structure that can use linked nodes
  • Graph - Uses similar node-based concepts