Data Structures Summary Notes
Data Structure Fundamentals
- Systematic method for organizing, storing, and managing data efficiently.
- Primitive types:
int, char, float, pointer. - Non-primitive types:
Array, Stack, Queue, Linked List, Tree, Graph. - Linear structures:
Array, Stack, Queue, Linked List. - Non-linear structures:
Tree, Graph. - Static allocation: Fixed size at allocation time (e.g.,
Array). - Dynamic allocation: Memory obtained or released during execution (e.g.,
Linked List). - Primary operations: Traversal, Insertion, Deletion, Searching, Sorting, and Merging.
Memory Allocation
- Static Allocation: Memory size is known and fixed before execution (e.g.,
int a[10];). - Automatic Allocation: Local variables live for function or block duration (e.g.,
int x;). - Dynamic Allocation Functions:
malloc(n): Allocates n bytes of uninitialized memory.calloc(k,n): Allocates space for k elements of n bytes each, initialized to zero.realloc(ptr,n): Resizes a previously allocated memory block.free(ptr): Releases dynamically allocated memory.
Arrays and Sparse Matrices
- 1-D Array Address: Address of A[i]=BA+(i−LB)×w
- 2-D Array Row-Major Address: LOC(A[i][j])=BA+w×((i−LR)×N+(j−LC))
- Common Complexities:
- Index Access: O(1)
- Unsorted Search: O(n)
- Binary Search (Sorted): O(log n)
- Insertion or Deletion in Middle: O(n)
- Insertion or Deletion at End: O(1)
- Sparse Matrix: Matrix in which most elements are zero.
- Triplet Representation: Stores non-zero elements as
(row, column, value) tuples. - Linked Representation: Dynamic nodes containing row, column, value, and a link to the next non-zero term.
Stacks and Expression Processing
- Stack: Linear LIFO (Last In, First Out) data structure with O(1) Push, Pop, and Peek operations.
- Exception Conditions:
- Overflow: Push attempted on a full stack.
- Underflow: Pop attempted on an empty stack.
- Multiple Stacks in One Array: Two stacks grow from opposite ends (
top1 = -1, top2 = MAX), sharing array space as long as top1+1<top2 holds true. - Notation Forms:
- Infix: Operator between operands (e.g., A+B).
- Prefix: Operator before operands (e.g., +AB).
- Postfix: Operator after operands (e.g., AB+).
- Expression Evaluation:
- Postfix Evaluation: Scans left to right using an operand stack.
- Prefix Evaluation: Scans right to left using an operand stack.
- Primary Applications: Function calls and recursion, expression conversion and evaluation, symbol balancing, undo/redo mechanisms, and depth-first search (DFS).