Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 1.95 KB

array-vs-list-vs-queue-vs-stack.asc

File metadata and controls

41 lines (34 loc) · 1.95 KB

Array vs. Linked List & Queue vs. Stack

In this part of the book, we explored the most used linear data structures such as Arrays, Linked Lists, Stacks, and Queues. We implemented them and discussed the runtime of their operations.

Use Arrays when…
  • You need to access data in random order fast (using an index).

  • Your data is multi-dimensional (e.g., matrix, tensor).

Use Linked Lists when:
  • You will access your data sequentially.

  • You want to save memory and only allocate memory as you need it.

  • You want constant time to remove/add from extremes of the list.

Use a Queue when:
  • You need to access your data on a first-come, first-served basis (FIFO).

  • You need to implement a Breadth-First Search

Use a Stack when:
  • You need to access your data as last-in, first-out (LIFO).

  • You need to implement a Depth-First Search

Table 1. Time/Space Complexity of Linear Data Structures (Array, LinkedList, Stack & Queues)

Data Structure

Searching By

Inserting at the

Deleting from

Space

Index/Key

Value

beginning

middle

end

beginning

middle

end

[array-chap]

O(1)

O(n)

O(n)

O(n)

O(1)

O(n)

O(n)

O(1)

O(n)

part02-linear-data-structures.asc

O(n)

O(n)

O(1)

O(n)

O(n)

O(1)

O(n)

O(n)

O(n)

part02-linear-data-structures.asc

O(n)

O(n)

O(1)

O(n)

O(1)

O(1)

O(n)

O(1)

O(n)

part02-linear-data-structures.asc

-

-

-

-

O(1)

-

-

O(1)

O(n)

Queue (w/array)

-

-

-

-

O(1)

O(n)

-

-

O(n)

part02-linear-data-structures.asc (w/list)

-

-

-

-

O(1)

O(1)

-

-

O(n)