Sobes.tech
Junior

What data structures exist?

sobes.tech AI

Answer from AI

Arrays, lists, stacks, queues, trees, graphs, hash tables.

Array: A collection of elements of the same type stored in contiguous memory cells. Access by index.

List: A sequential collection of elements. Can be singly or doubly linked. Adding/removing in the middle is more efficient than in an array.

Stack: A LIFO (Last-In, First-Out) structure. Operations: push (add), pop (remove from end), peek (view top element).

Queue: A FIFO (First-In, First-Out) structure. Operations: enqueue (add to end), dequeue (remove from front), peek (view first element).

Tree: A hierarchical data structure. Root, nodes, descendants. Example: binary search tree.

Graph: A set of vertices (nodes) connected by edges. Can be directed or undirected, weighted or unweighted.

Hash table: A collection of key-value pairs. Uses a hash function for fast access (on average). Collisions are possible.

Examples of usage in iOS:

  • Arrays: Array
  • Lists: no built-in class, implemented based on arrays or custom structures
  • Stacks/Queues: implemented based on arrays or lists
  • Trees: used in frameworks, e.g., in UI (view hierarchy)
  • Graphs: to represent connections, e.g., network graphs
  • Hash tables: Dictionary, Set
// Array example
let numbers = [1, 2, 3, 4, 5]
print(numbers[0]) // Access by index

// Dictionary (hash table) example
let grades = ["Alice": 95, "Bob": 88]
print(grades["Alice"] ?? 0) // Access by key