Linear Data Structures

The Linked List

An alternative to the array

Motivation

Structure Array Dynamic
Array
Prepend O(n) O(n)
Insert O(n) O(n)
Append O(n) O(1)
Remove First O(n) O(n)
Remove Index O(n) O(n)
Remove Last O(n) O(1)
Random Access O(1) O(1)

How

What if we stored memory non-contiguously?

Unlike arrays, in linked lists, data is stored
non-contiguously in these nodes

Singly Linked List

Where nodes only contain data and *next

Circular Singly Linked List

Where nodes only contain data and *next, but no node points to null

Doubly Linked List

Where nodes only contain data and *next, but no node points to null

Circular Doubly Linked List

Where nodes only contain data and *next, but no node points to null

Stacks

Last In First Out

Queues

First in First Out

Queues: Required Methods

  • Enqueue (push)
  • Dequeue (pop)
  • Is Empty

What should the underlying structure be?

Queues: SLL Backend Visual

Deque

Double Ended Queues

They allow fast pushes and pops from either end.

  • pushleft
  • pushright
  • popleft
  • popright