Data Structures in C
Data Structures in C
Data structures are the building blocks for programming logic, used to organize, manage, and
store data efficiently. In C programming, data structures are categorized into Primitive and
Non-Primitive types.
Primitive data structures are basic data types directly supported by the system and act as the
foundation for more complex structures.
1.1 Integer
• Definition: Integer (int) represents whole numbers, both positive and negative, with
no fractional part.
• Size: Usually occupies 4 bytes in memory, allowing it to store values in the range -
2^31 to 2^31-1.
• Example:
c
Copy code
int age = 25;
printf("Age: %d", age); // Output: Age: 25
1.2 Float
• Definition: Float represents numbers with a fractional part, allowing storage of real
numbers with decimal points.
• Size: Typically occupies 4 bytes, offering up to 7 digits of precision.
• Example:
c
Copy code
float price = 10.99;
printf("Price: %.2f", price); // Output: Price: 10.99
1.3 Character
c
Copy code
char initial = 'A';
printf("Initial: %c", initial); // Output: Initial: A
1.4 Pointer
• Definition: A pointer is a variable that stores the memory address of another variable.
• Example:
c
Copy code
int num = 42;
int *ptr = #
printf("Address of num: %p", ptr); // Output: Address of num:
0x7ffee10c091c (example)
printf("Value at address: %d", *ptr); // Output: Value at address:
42
Non-primitive data structures are derived from primitive types and provide ways to store
multiple values. They are categorized as Static and Dynamic data structures.
These data structures have a fixed size, defined at compile-time. Common examples include
arrays and structures.
2.1.1 Arrays
c
Copy code
int numbers[5] = {1, 2, 3, 4, 5};
printf("Third element: %d", numbers[2]); // Output: Third element: 3
Multi-dimensional Arrays
• Definition: Arrays can have more than one dimension, such as a 2D array,
representing a matrix or grid.
• Example:
c
Copy code
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("Element at [1][2]: %d", matrix[1][2]); // Output: Element at
[1][2]: 6
2.1.2 Structures
Dynamic data structures can grow or shrink at runtime, depending on the needs of the
program. They are often managed using pointers and dynamic memory allocation functions
like malloc() and free().
Linear data structures have a sequential organization, where each element has a unique
predecessor and successor (except the first and last).
2.2.1.1 Linked Lists
• Definition: A linked list is a collection of nodes where each node contains data and a
pointer to the next node.
• Types:
o Singly Linked List: Each node points to the next node, with the last node
pointing to NULL.
• Example:
c
Copy code
struct Node {
int data;
struct Node* next;
};
2.2.1.2 Stacks
• Definition: A stack is a linear structure that follows the Last In First Out (LIFO)
principle, meaning the last element added is the first to be removed.
• Example (Array-based Stack):
c
Copy code
int stack[5];
int top = -1;
int pop() {
if(top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
2.2.1.3 Queues
• Definition: A queue is a linear structure that follows the First In First Out (FIFO)
principle, where the first element added is the first to be removed.
• Example (Simple Queue):
c
Copy code
int queue[5];
int front = 0, rear = -1;
int dequeue() {
if(front > rear) {
printf("Queue Underflow\n");
return -1;
} else {
return queue[front++];
}
}
enqueue(15);
printf("Dequeued: %d", dequeue()); // Output: Dequeued: 15
Non-linear data structures allow hierarchical relationships among elements, making them
suitable for representing real-world scenarios like trees and networks.
2.2.2.1 Trees
• Definition: A tree is a hierarchical structure where each node contains data and
references to child nodes. The top node is called the root, and nodes without children
are called leaves.
• Example (Binary Tree Node):
c
Copy code
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
2.2.2.2 Graphs
c
Copy code
int graph[3][3] = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
};
2.2.2.3 Heaps
c
Copy code
int heap[10], size = 0;
insert(10);
insert(5);
insert(20);
// Heap now stores values with the smallest at root
Conclusion
In C, data structures serve as the foundation for organizing and managing data efficiently.
Mastering both static and dynamic structures, such as arrays, linked lists, and trees, enables
optimal performance in terms of time and space complexity. By understanding how each
structure works and when to use them, you can write more efficient and effective C
programs.