Data Structures & Algorithms - Midterm Summary

Data Foundations & ADTs

  • Data vs. Information:

    • Data: Raw facts without intrinsic meaning.

    • Process: Organize and interpret raw data.

    • Information: Meaningful result obtained after processing.

  • Data Structure: A method of organizing values so storage and operations can be performed efficiently.

  • Classification:

    • Linear: Array, Linked List, Stack, Queue.

    • Non-Linear: Tree, Graph.


Linear Data Structures


Non-Linear Data Structures
  • Abstract Data Type (ADT):

    • ADT specifies what operations can be performed (behavior).

    • Implementation specifies how data is stored (e.g., Stack ADT implemented via Array or Linked List).

Algorithms & Pseudocode

  • Valid Algorithm Criteria:

    • Definiteness: Clear, unambiguous steps.

    • Finiteness: Must terminate after a finite number of steps.

    • Input: Zero or more quantities entering.

    • Output: One or more quantities leaving.

    • Effectiveness: Operations are basic enough to be doable.

  • Flowchart Symbols: Start/End, Input/Output, Process, Decision, and Control Arrows.

  • Pseudocode: Expresses logic clearly using structured constructs (IF, FOR, WHILE, READ, PRINT) independent of exact language syntax.

Array Operations & Complexity

  • Array Rule: Elements share one declared type and occupy contiguous memory locations.

  • Indexing: Zero-based indexing; for nn elements, valid indices range from 00 to n−1n - 1.

  • Capacity vs. Logical Size:

    • Physical Capacity: Total memory cells allocated.

    • Logical Size: Number of elements currently used.

  • Time Complexities:

    • Index Access: Direct access, O(1)O(1).

    • Traversal & Linear Search: Scans elements sequentially, O(n)O(n).

    • Append: Add at logical end, O(1)O(1) (assuming available physical capacity).

    • Middle Insertion / Deletion: Requires element shifting, O(n)O(n).

    • Update: Replaces value at valid index without shifting, O(1)O(1).

  • Bubble Sort: After one complete ascending pass, the maximum value settles at the far-right index.

Dynamic Memory Management

  • Pointers: Store memory addresses rather than direct objects.

    • & (Address-of operator): Retrieves memory address of a variable.

    • * (Dereference operator): Accesses value stored at a pointer address.

  • Dynamic Allocation:

    • new int[n]: Allocates contiguous block on the Heap at runtime.

    • delete[] ptr: Releases dynamically allocated array memory. Matches new[] with delete[].

    • nullptr: Marks pointers explicitly as pointing to nothing post-release.

  • Memory Model:

    • Call Stack: Stores function call frames, parameters, local variables.

    • Heap / Free Store: Dynamic memory allocation (new, dynamic arrays, nodes).

Linked Lists

  • Singly Linked List Node: Contains DATA field and NEXT pointer.

  • Pointers:

    • head: Always points to the first node; losing head results in loss of list access.

    • current: Used for list traversal (current = current->next).

  • List Operations:

    • Insert at Beginning: new->next = head; head = new;

    • Insert at End: tail->next = new; new->next = nullptr;

    • Middle Insertion/Deletion: Adjust link pointers between prior and next nodes before deleting/inserting.

  • List Variations:

    • Doubly Linked List: Contains NEXT and PREV pointers for bidirectional traversal.

    • Circular Linked List: Final node points back to head; traversal stops upon returning to start node rather than encountering nullptr.

Stacks & Queues

  • Stack (LIFO):

    • Operations: Push (adds at TOP), Pop (removes from TOP), Peek (reads TOP without removing).

    • Array Stack Empty State: top = -1. After mm pushes with no pops, top = m - 1.

  • Queue (FIFO):

    • Operations: Enqueue (inserts at REAR), Dequeue (removes from FRONT).

    • Empty State: FRONT = -1, REAR = -1.

  • Circular Queue:

    • Solves linear queue space wastage by wrapping array indices.

    • Wrap-around formula: NEXT=(index+1) mod MAX\text{NEXT} = (\text{index} + 1) \bmod \text{MAX}.

  • Priority Queue:

    • Elements are processed by priority rather than arrival sequence.

    • High priority values are dequeued first when higher numbers represent higher priority.