Data Structures#
A data structure is a specialized format for organizing, processing, retrieving, and storing data in a computer system so it can be used efficiently. It provides a physical implementation for abstract concepts, ensuring that code can access and modify information quickly and accurately.
Core Classifications#
Data structures are generally divided into two main categories based on how the elements are arranged:
Primitive Data Structures#
Primitive data structures are the most basic and fundamental data types built directly into a programming language. They are atomic, meaning they hold a single value at a specific memory location and cannot be broken down into simpler data subtypes. Because they operate directly according to low-level machine instructions, they are highly optimized for memory speed.
Key Characteristics
Predefined: Built straight into the programming language compiler or interpreter.
Direct Operation: Handled by CPU machine instructions.
Fixed Size: Occupy a predetermined, fixed amount of memory (e.g. 4 bytes for an integer).
Single-Value: Designed to hold exactly one data point at a time.
Examples
Integer: Stores positive or negative whole numbers without decimal points.
Floating-point: Represents real numbers containing a fractional decimal component.
Character: Holds a single letter, number, punctuation mark, or space symbol.
Boolean: Represents a logical state consisting of only two possible values: true or false.
Pointer: Stores the exact memory address location of another variable.
Non-Primitive Data Structures#
A non-primitive data structure is a complex, user-defined or language-derived data organization format built by combining one or more primitive data structures (such as integers, characters, booleans). Instead of storing a single basic value, non-primitive data structures organize, store, and manipulate collections of related data items. They provide advanced features like dynamic sizing, relationship tracking between entries, and reference-based memory management.
Non-primitive data structures are broadly split into two structural categories:
Linear Data Structures#
A linear data structure is a data organization method where elements are arranged sequentially or linearly, one after another, so that each element connects to its unique predecessor and successor. Elements are ordered on a single level, allowing you to traverse the entire structure from start to finish in a single run.
Key Characteristics
Sequential Order: Elements form a clear line or sequence.
Single-Level Traversing: You can visit every element sequentially without splitting into different branches like trees or graphs.
Memory Management: They can use continuous memory allocations or scattered memory nodes linked by pointers.
An array is a fundamental, linear data structure that stores a collection of elements of the same data type in contiguous (adjacent) memory locations.
A linked list is a linear data structure where elements are not stored in contiguous memory locations. Instead, they are represented as individual objects called nodes, which are chained together using pointers or references.
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, meaning the last element added is the first one to be removed. Think of it like a physical stack of plates—you can only add a new plate to the top, and you can only remove the plate that is currently on top.
A queue is a fundamental, linear data structure that operates on the First In, First Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed, much like a real-world line of people waiting at a supermarket or a ticket counter.
Non-Linear Data Structures#
A non-linear data structure is an organization method where data items are not arranged in a sequential, one-by-one order. Instead, elements connect in hierarchical or network-like patterns. One node can link to multiple other nodes, meaning you cannot visit every piece of data in a single straightforward pass.
Key Characteristics
Multiple Paths: Elements connect across multiple levels rather than a single flat line.
Many-to-many links: One item can have relationships with several other items.
Complex Traversal: Visiting all data requires special paths like depth-first or breadth-first search.
Flexible Memory: Memory is allocated dynamically as nodes branch out.
A hash map (also known as a hash table, dictionary, or associative array) is a fundamental data structure designed to store key-value pairs, enabling ultra-fast data retrieval, insertion, and deletion.
A tree is a non-linear, hierarchical data structure composed of nodes connected by directed edges. Unlike linear structures like arrays or linked lists, trees organize data in a parent-child relationship, making them highly efficient for sorting, searching, and managing hierarchical information.
Common Operations#
Regardless of the type, software programs use data structures to perform six fundamental operations:
Searching: Finding a specific element within the structure.
Sorting: Arranging elements in a certain order (e.g. alphabetical, ascending numeric).
Insertion: Adding a new item to the structure.
Deletion: Removing an existing item from the structure.
Updating: Modifying an existing data element.
Traversal: Visiting every element in the structure exactly once to process it.