Operations of Data Structures
Data structures are logical and mathematical ways of organizing data in memory so that it can be accessed and modified efficiently. To manipulate this data, a set of fundamental operations is performed.
These operations are independent of the programming language and are applicable to all major data structures such as arrays, linked lists, stacks, queues, trees, and graphs.
1. Traversal
Traversal is the systematic process of accessing and processing each element of a data structure exactly once, following a defined order specific to the structure. It ensures that no element is skipped or visited multiple times unless explicitly required.
Traversal is essential when we need to:
- Examine all elements
- Perform operations like printing, counting, or summing
- Apply a function to every element
The traversal technique depends on the type of data structure:
- Linear structures (arrays, lists): Sequential traversal from first to last element
- Non-linear structures (trees, graphs): Specialized traversal methods (e.g., depth-first, breadth-first)
Example
Traversing an array to compute sum:
Marks = [50, 60, 70]
Sum = 1802. Searching
Searching is the operation of locating a specific element or set of elements within a data structure based on a given key or condition, and determining its presence, position, or both.
- It may return the index, memory location, or simply a TRUE/FALSE result
- Efficiency depends on the structure and algorithm used
- Searching can be unsuccessful if the element does not exist
Types:
- Linear Search: Sequential checking (works on all data structures)
- Binary Search: Efficient method that works only on sorted data
Example
Searching for value 25 in:
[10, 20, 25, 30] → Found at index 23. Insertion
Insertion is the process of adding a new element into a data structure at a specified location while maintaining the structure’s properties and organization.
- The position of insertion affects complexity
- May require shifting elements (arrays) or pointer adjustments (linked lists)
- Must ensure no violation of constraints (e.g., overflow in fixed-size structures)
Types
- Beginning
- Middle
- End
Example:
Before: [10, 20, 30]
Insert 15 at index 1
After: [10, 15, 20, 30]4. Deletion
Deletion is the process of removing an existing element from a data structure and reorganizing the remaining elements to maintain continuity and structure integrity.
- Requires locating the element first
- May involve shifting or relinking
- Must handle underflow conditions in some structures
Example
Before: [10, 20, 30]
Delete 20
After: [10, 30]5. Sorting
Sorting is the process of arranging elements of a data structure in a specific order (typically ascending or descending) based on a comparison criterion.
- Improves efficiency of searching
- Helps in data organization and analysis
- Can be stable or unstable depending on algorithm
Common Algorithms
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
Example
Before: [30, 10, 20]
After: [10, 20, 30]6. Merging
Merging is the operation of combining two or more data structures of the same type into a single unified structure, typically while preserving or establishing a specific order.
- Often applied to sorted data
- Forms the basis of efficient algorithms like Merge Sort
- Requires comparison and placement logic
Example
List1: [10, 20]
List2: [15, 25]
Merged: [10, 15, 20, 25]7. Creation
Creation is the initial operation of allocating memory and defining the structure in which data elements will be stored, either at compile-time or run-time.
- Determines size, type, and structure
- Can be static (fixed size) or dynamic (flexible size)
- Impacts performance and memory usage
Types
- Compile-time allocation: Memory fixed before execution
- Run-time allocation: Memory allocated during execution
Example (C Language):
int arr[10]; // static allocation
int *arr = malloc(10 * sizeof(int)); // dynamic allocation8. Selection
Selection is the operation of identifying and extracting specific elements from a data structure that satisfy a given condition or criteria.
- Often used with loops and conditional statements
- Helps in filtering relevant data
- Can produce a subset of original data
Example
Select numbers greater than 50:
Input: [30, 60, 80]
Output: [60, 80]9. Updating
Updating is the process of modifying the value of an existing data element in a data structure without changing its position or overall structure.
- Requires locating the element first
- Ensures data remains current and accurate
- Common in dynamic applications like databases
Example
Before: [50, 60, 70]
Update index 1 → 65
After: [50, 65, 70]10. Splitting
Splitting is the process of dividing a data structure into two or more smaller structures based on specific criteria, conditions, or positions.
- Reduces complexity of large datasets
- Improves processing efficiency
- Often used in divide-and-conquer strategies
Example
Input: [1, 2, 3, 4]
Even: [2, 4]
Odd: [1, 3]