Introduction to Data Structures in C
What are Data Structures?
Data structures are ways of organizing and storing data so that they can be accessed and worked with efficiently. Different types of data structures are suited to different kinds of applications, and some are highly specialized for specific tasks.
Types of Data Structures
There are several types of data structures, broadly classified into two categories:
- Linear Data Structures: Data elements are arranged in a sequential manner. Examples include arrays, linked lists, stacks, and queues.
- Non-Linear Data Structures: Data elements are not in sequence. Examples include trees and graphs.
Arrays
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
Example: Declaring and initializing an array in C
Output: The array arr
will contain the elements 1, 2, 3, 4, and 5.
Linked Lists
A linked list is a linear data structure where elements are not stored at contiguous memory locations. The elements, or nodes, contain a data part and a reference to the next node in the sequence.
Example: Defining a linked list node in C
int data;
struct Node* next;
};
Output: This defines a node structure with an integer data field and a pointer to the next node.
Stacks
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. Elements can be added or removed only from one end, called the top of the stack.
Example: Basic stack operations in C
int stack[MAX];
int top = -1;
void push(int x) {
if (top >= MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = x;
}
}
int pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
Output: This code snippet demonstrates basic push and pop operations on a stack.
Queues
A queue is a linear data structure that follows the First In First Out (FIFO) principle. Elements are added at the rear end and removed from the front end.
Example: Basic queue operations in C
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int x) {
if (rear >= MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
queue[++rear] = x;
}
}
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
} else {
return queue[front++];
}
}
Output: This code snippet demonstrates basic enqueue and dequeue operations on a queue.
Trees
A tree is a non-linear data structure where data is organized in a hierarchical manner. The most common type of tree is a binary tree, where each node has at most two children.
Example: Defining a binary tree node in C
int data;
struct Node* left;
struct Node* right;
};
Output: This defines a node structure with integer data and pointers to the left and right child nodes.
Graphs
A graph is a non-linear data structure consisting of nodes (vertices) and edges. Graphs can be used to model many types of relationships and processes in physical, biological, social, and information systems.
Example: Representing a graph using an adjacency matrix in C
int graph[V][V] = {
{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 1, 0},
{0, 1, 1, 0, 1},
{1, 0, 0, 1, 0}
};
Output: This code snippet demonstrates how to represent a graph using an adjacency matrix.