Data Structure and Types
A data structure is a systematic way of organizing, storing, and managing data in a computer so that it can be accessed and modified efficiently. It defines the relationship between data elements and provides a framework for performing operations such as insertion, deletion, searching, and updating.
Data structures form the foundation of programming, as they help programmers handle data in an organized and efficient manner. The choice of a suitable data structure directly affects the performance of a program in terms of time and memory usage.
A data structure is a structured collection of data elements that are related to each other in a specific way, along with the operations that can be performed on them.
It not only stores data but also defines:
- How data is organized
- How data is accessed
- How data is processed efficiently
Classification of Data Structures
Data structures are broadly classified into two main categories:
- Primitive Data Structures
- Non-Primitive Data Structures
1. Primitive Data Structures
Primitive data structures are the basic built-in data types that are directly supported by the programming language. These data types are the simplest form of data and cannot be broken down further.
They are directly handled by machine-level instructions and form the foundation for more complex data structures.
Primitive data types store single values and are used for basic operations like arithmetic and logical processing.
Examples
- Integer (int)
- Float
- Character (char)
- Boolean
2. Non-Primitive Data Structures
Non-primitive data structures are derived from primitive data types and are used to store multiple values or complex relationships between data elements.
These structures are not directly operated by machine instructions; instead, they are implemented using primitive types.
They are used to handle:
- Large datasets
- Complex relationships
- Real-world problems
Non-primitive data structures can store:
- Homogeneous data (same type)
- Heterogeneous data (different types)
Classification of Non-Primitive Data Structures
Based on structure and arrangement, they are divided into:
- Linear Data Structures
- Non-Linear Data Structures
1. Linear Data Structures
A linear data structure is one in which data elements are arranged in a sequential order, where each element is connected to its previous and next element (except the first and last elements).
However, in memory, elements may not always be stored sequentially.
Types Based on Memory Allocation
A. Static Data Structures
Static data structures have a fixed size, which is defined at compile time. Once declared, their size cannot be changed during program execution.
Although the size is fixed, the values stored inside can be modified.
Static structures allocate memory in advance, making them faster but less flexible.
Example
- Arrays
B. Dynamic Data Structures
Dynamic data structures have a flexible size, which can change during program execution. Memory is allocated at runtime based on requirements.
They are more flexible because they grow or shrink as needed.
Examples
- Linked List
- Stack
- Queue
Types of Linear Data Structures
1. Arrays
An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is accessed using an index number.
Arrays allow efficient access to elements using their position, but their size is fixed.
Instead of creating multiple variables, arrays store multiple values under a single name, making data management easier.
Types of Arrays
- One-Dimensional Array → Single row of elements
- Two-Dimensional Array → Rows and columns (matrix form)
- Multidimensional Array → Array of arrays
2. Linked Lists
A linked list is a dynamic linear data structure where elements (nodes) are connected using pointers. Each node contains:
- Data
- Address of next node
The last node points to NULL.
Unlike arrays, linked lists do not use contiguous memory. Each element is linked using pointers, making insertion and deletion easier.
Types of Linked List
- Singly Linked List → One direction
- Doubly Linked List → Both directions
- Circular Linked List → Last node connects to first
3. Stacks
A stack is a linear data structure that follows the LIFO (Last In First Out) principle, meaning the last element inserted is the first one removed.
Operations are performed only at one end called the top.
Operations
- Push → Insert element
- Pop → Remove element
A stack works like a pile of plates where the top plate is removed first.
4. Queues
A queue is a linear data structure that follows the FIFO (First In First Out) principle, where the first element inserted is the first one removed.
Insertion occurs at the rear and deletion occurs at the front.
Operations
- Enqueue → Insert element
- Dequeue → Remove element
A queue works like a line of people waiting for service.
2. Non-Linear Data Structures
Non-linear data structures are those in which data elements are not arranged in sequential order. Instead, they form a hierarchical or network relationship between elements.
1. Trees
A tree is a hierarchical data structure consisting of nodes connected by edges. It has a root node, parent nodes, and child nodes.
A tree organizes data in levels, where each node can have multiple child nodes.
Types of Trees
- Binary Tree → Maximum two children
- Binary Search Tree → Sorted structure
- AVL Tree → Self-balancing tree
- B-Tree → Multi-child balanced tree
2. Graphs
A graph is a non-linear data structure consisting of vertices (nodes) and edges (connections between nodes). It is used to represent networks and relationships.
Graphs represent real-world systems like social networks, transport systems, and communication networks.
Mathematically:
G = (V, E)
Where:
- V = set of vertices
- E = set of edges
Types of Graphs
- Directed Graph
- Undirected Graph
- Cyclic Graph
- Acyclic Graph
- Connected Graph
- Disconnected Graph
- Complete Graph
- Bipartite Graph
Data structures play a crucial role in organizing and managing data efficiently. They are classified into primitive and non-primitive types, where non-primitive structures are further divided into linear and non-linear structures.
Each data structure has its own advantages and use cases, and selecting the right one is essential for designing efficient algorithms and solving computational problems effectively.