Data structures are ways to organize and store data so it can be used efficiently. They are essential in computer science for managing and processing information in programs. Common types of data structures include arrays, linked lists, stacks, queues, trees, and graphs. Each structure is designed for specific tasks, such as searching, sorting, or managing hierarchical data. Understanding data structures helps in solving problems faster and writing better algorithms.
Data structures are of two types:
1. Linear Data Structures: In linear data structures, elements are arranged in a sequential order. Each element is connected to its previous and next element, making traversal straightforward.
2. Non-Linear Data Structures: In non-linear data structures, elements are not arranged sequentially. They are connected in a hierarchical or network-like structure.
Arrays
An array is a data structure used to store multiple elements of the same type in contiguous memory locations. Arrays are simple and widely used for organizing and managing data.
Declaration:
In C, we can declare an array by specifying its and size or by initializing it or by both.
// Array declaration by specifying size
int arr[10];
// Array declaration by initializing elements
int arr[] = {10, 20, 30, 40};
// Array declaration by specifying size and
// initializing elements
int arr[6] = {10, 20, 30, 40}
Initialization:
int arr[5] = {1, 2, 3, 4, 5};
Accessing Elements:
arr[0] = 10; // Assigns 10 to the first element
printf("%d", arr[2]); // Prints the third element
Formulas:
Length of Array = UB - LB + 1
Types of Arrays
1. One-Dimensional Array: Stores elements in a single row.
Example:
int arr[5] = {10, 20, 30, 40, 50};
2. Multi-Dimensional Array: Represents a matrix or table of data.
Example:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Given the address of first element, address of any other element is calculated using the formula:-
Loc (arr [k]) = base (arr) + w * k
w = number of bytes per storage location
of for one element
k = index of array whose address we want
to calculate
Elements of two-dimensional arrays (mXn) are stored in two ways:-
- Column major order: Elements are stored column by column, i.e. all elements of first column are stored, and then all elements of second column stored and so on.
Loc(arr[i][j]) = base(arr) + w (m *j + i)
- Row major order: Elements are stored row by row, i.e. all elements of first row are stored, and then all elements of second row stored and so on.
Loc(arr[i][j]) = base(arr) + w (n*i + j)
Common Operations on Arrays
- Traversal: Visiting all elements in the array.
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
- Insertion: Adding an element at a specific position.
Requires shifting elements to make space.
- Deletion: Removing an element from a specific position.
Requires shifting elements to fill the gap.
- Searching: Finding an element in the array.
Linear Search: Sequentially check each element.
Time Complexity:O(n)
Binary Search: Requires the array to be sorted.
Time Complexity:O(log n)
- Sorting: Arranging elements in ascending or descending order.
Common algorithms: Bubble Sort, Selection Sort, Quick Sort.
read more about - Arrays
Stacks
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, meaning the element added last is removed first. It is widely used in programming for tasks such as expression evaluation, backtracking, and function call management.
Basic operations :
1. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. (Top=Top+1). Time Complexity: O(1).
2. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.(Top=Top-1). Time Complexity: O(1).
3. Peek: Retrieves the top element without removing it. Time Complexity: O(1).
Infix, prefix, Postfix notations
Infix notation: X + Y - Operators are written in-between their operands. This is the usual way we write expressions. An expression such as
A * ( B + C ) / D
Postfix notation (also known as "Reverse Polish notation"): X Y + Operators are written after their operands. The infix expression given above is equivalent to
A B C + * D/
Prefix notation (also known as "Polish notation"): + X Y Operators are written before their operands. The expressions given above are equivalent to
/ * A + B C D
Conversion between these notations: Click here
Implementation of Stack
Stacks can be implemented in C using:
Tower of Hanoi
It is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
- No disk may be placed on top of a smaller disk.
For n disks, total 2n – 1 moves are required
Time complexity : O(2n) [exponential time]
read more about - Stacks
Queues
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means the element added first is removed first. Queues are widely used in scenarios like scheduling, buffering, and real-time systems.
Types of Queues
- Simple Queue: Follows the standard FIFO principle. Elements are added at the rear and removed from the front.
- Circular Queue: The last position connects back to the first position, making the queue circular. Efficient use of memory compared to a simple queue.
- Priority Queue: Each element is associated with a priority, and elements with higher priority are dequeued before others.
- Deque (Double-Ended Queue): Elements can be added or removed from both ends (front and rear).
Common Operations on Queue
- Enqueue: Adds an item to the queue. If t he queue is full, then it is said to be an Overflow condition.
- Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.
- Peek:Retrieve the front element without removing it. Time Complexity:
O(1). - Traversal: Iterating through all elements in the queue. Time Complexity:
O(n).
Front: Get the front item from queue.
Rear: Get the last item from queue.
Implementation of Queue
Queues can be implemented in C using:
read more about - Queues
Linked Lists
A linked list is a linear data structure where elements (called nodes) are connected using pointers. Unlike arrays, linked lists do not store elements in contiguous memory locations, making them dynamic and flexible for insertion and deletion operations.
Types of Linked Lists
- Singly Linked List: Each node points to the next node. Traversal is unidirectional. Last node points to
NULL.