Study Atlas › Computer Science › Data structures

Data structures: arrays, linked lists, stacks, queues

Ways to organise data so operations are efficient.

Edited by Alex C, EduAlx · Sources cited below

Explanation

Arrays give O(1) indexed access; linked lists give O(1) insertion at a known node. Stacks are last-in-first-out (function calls, undo); queues are first-in-first-out (scheduling, BFS). Choosing the right structure often matters more than micro-optimising code.

Example

Checking balanced brackets '({[]})' with a stack: push openers, pop and match closers.

Code

def balanced(s):
    pairs = {')':'(', ']':'[', '}':'{'}
    st = []
    for ch in s:
        if ch in '([{': st.append(ch)
        elif ch in pairs:
            if not st or st.pop() != pairs[ch]: return False
    return not st

How it works, step by step

  1. Identify required operations
  2. Compare costs per structure
  3. Choose the structure
  4. Implement and test edge cases

Open the interactive page with diagrams →

People behind it

References

  1. N. Wirth, Algorithms + Data Structures = Programs, Prentice-Hall, 1976.

Textbooks

Related topics

Hash tablesHeaps and priority queuesTrees and binary search treesSorting algorithms (quicksort, mergesort, heapsort)Algorithms and Big-O complexityGraph algorithms: BFS, DFS and Dijkstra's shortest pathError-detecting and error-correcting codesDynamic programmingHuman–computer interaction and usabilityTuring machines and computability