STEP 7 Review: Data Structures, Sorting, and Searching

Review the key points with a cheat sheet, six code-reading questions, and a checklist of common mistakes.

① Key-point cheat sheet

Review the essentials of STEP 7 in these tables .

📚 Choosing a data structure

Structure Characteristic Typical use
Array Direct indexed access Fixed-length data
Linked list Fast insertion and deletion at a known position Data whose length changes
Stack Last in, first out (LIFO) Function calls and undo
Queue First in, first out (FIFO) Waiting lines and BFS
  • Which operations are fast or slow determines the choice

🔗 Linked-list nodes

struct Node {
    int value;
    struct Node *next; // Pointer to the next node 
};

// The last node has next == NULL 
  • Each node has a pointer to the next node .
  • Insertion and deletion use pointer relinking once the location is known
  • Indexed access is slow: traversal begins at the head

📥 Stack / 📤 Queue

// Stack (LIFO): push / pop 
push(1); push(2); push(3);
pop(); // → 3 (inserted last) 

// Queue (FIFO): enqueue / dequeue 
enqueue(1); enqueue(2); enqueue(3);
dequeue(); // → 1 (inserted first) 
  • A stack resembles a pile of plates: last in, first out
  • A queue resembles a checkout line: first in, first out

🔀 Common sorting algorithms

Name Approach Complexity
Bubble Compare and swap neighbors O(N²)
Selection Select the minimum and move it to the front O(N²)
Insertion Insert into the correct position in the sorted portion O(N²)
  • Their basic worst-case complexity is O(N²); time grows quickly as N increases
  • Merge sort guarantees O(N log N); quicksort typically achieves O(N log N) on average

🔍 Linear versus binary search

Method Requirement Complexity
Linear search None O(N)
Binary search Sorted input is required O(log N)
  • Binary search halves the search range each time by comparing against the midpoint
  • For N=1000: up to 1000 linear steps versus about 10 binary steps
  • On unsorted input, binary search cannot be applied correctly

② Six code-reading questions

Execute the code mentally before choosing an answer. Read the explanation even if you get it wrong.

Q1. Stack behavior

// Push onto a stack, then pop once: what is returned? 
push(10);
push(20);
push(30);
int v = pop();
printf("%d\n", v);

What is the output?

10
20
30
Indeterminate
Explanation: A stack is last in, first out. The last value pushed is 30, so pop returns 30. Think of taking the top plate off a pile.

Q2. Queue behavior

enqueue(10);
enqueue(20);
enqueue(30);
int v = dequeue();
printf("%d\n", v);

What is the output?

10
20
30
Indeterminate
Explanation: A queue is first in, first out. The first value enqueued is 10, so dequeue returns 10. Think of the first person in a checkout line being served first.

Q3. Bubble-sort iteration count

int a[] = {5, 2, 8, 1, 9}; // N=5
for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - 1 - i; j++) {
        if (a[j] > a[j+1]) /* swap */;
    }
}

How many inner-loop comparisons occur in total?

25
10
five iterations
15
Explanation: The inner loop makes 4, 3, 2, and 1 comparisons, totaling 10. In general this version makes N(N-1)/2 comparisons and has O(N²) complexity.

Q4. Binary-search comparison count

// Find an item among 1024 sorted values 
// Approximately how many comparisons does binary search need at worst? 

Approximately how many comparisons in the worst case?

About 1024
About 512
About 10
About 1
Explanation: Binary search repeatedly halves a sorted range. Ten halvings reduce 1024 candidates to one (2¹⁰=1024), so the approximate answer is 10. An implementation may need one final comparison. Complexity is O(log N), and sorted input is required.

Q5. The end of a linked list

struct Node *cur = head;
while (cur->next != NULL) {
    cur = cur->next;
}
// What does cur point to now? 

What does cur point to?

The first node
The second node
The last node (whose next is NULL)
NULL
Explanation: With cur->next != NULL, the loop stops on the last node, whose next is NULL. With cur != NULL, it would instead stop after cur itself becomes NULL.

Q6. First pass of selection sort

int a[] = {5, 2, 8, 1, 9};
// One selection-sort pass: find the minimum and move it to the front 

What is the array after the first pass?

{1, 2, 5, 8, 9} (fully sorted)
{1, 2, 8, 5, 9} (minimum first, other positions unchanged)
{1, 5, 8, 2, 9}
{2, 5, 8, 1, 9}
Explanation: The first pass finds 1 at index 3 and swaps it with the leading 5, producing {1, 2, 8, 5, 9}. Later passes repeat the process on the remaining unsorted portion.

③ Common mistakes and tricky points

If an item feels familiar, return to its original lesson and review it.
  1. 1
    Confused stack and queue order
    push/pop is LIFO; enqueue/dequeue is FIFO. Remember their everyday analogies : plates versus a checkout line.
  2. 2
    For the linked-list end condition, cur != NULLand cur->next != NULLwere confused
    The first traverses all nodes; the second stops on the last node. Choose for your goal.
  3. 3
    Forgot to free a linked-list node
    After finishing with a node, call free to avoid a memory leaks . Save next in a separate variable before freeing the current node.
  4. 4
    Used binary search on an unsorted array
    Binary search Sorted input is essential; unsorted input gives unreliable results.
  5. 5
    Incorrect inner-loop bounds in bubble, selection, or insertion sort
    j < N - 1 - ior j >= 1: an off-by-one boundary can cause Out-of-bounds access .
  6. 6
    Complexity O(N²)Underestimated the cost of
    N=10 means roughly 100 operations; N=10000 means 100 million. Large N requires a faster algorithm , such as one with O(N log N) complexity.
  7. 7
    During swap, a missing temporary variable caused a value to be lost
    a = b; b = a; makes both variables equal the old b. int t = a; a = b; b = t; preserves the value through a temporary variable.

④ Lessons to revisit when you get stuck

Use these links to return to the relevant explanation.
Data Structures Review linked lists, stacks, and queues Lesson 46: Sorting algorithms Review bubble, selection, insertion, and binary search Pointer basics For linked lists, review next when its meaning is unclear Dynamic memory Review allocating and releasing nodes Cheat sheet Review complexity and data structures on one page