Data Structure
Data Structure
1. Data Type
A data type is a classification that specifies what type of data a variable can
hold. It helps the compiler understand how much memory to allocate and
what operations can be performed on the data.
1. Tree – Hierarchical data structure (e.g., Binary Tree, Binary Search Tree).
2. Graph – Collection of nodes (vertices) connected by edges.
C. Other Types
Types of Arrays
Example in C:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Array
declaration
int size = sizeof(arr) / sizeof(arr[0]); //
Finding size
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print each element
}
return 0;
}
Output:
10 20 30 40 50
#include <stdio.h>
void insertElement(int arr[], int *size, int
position, int value) {
for (int i = *size; i > position; i--) {
arr[i] = arr[i - 1]; // Shift elements
}
arr[position] = value; // Insert new value
(*size)++; // Increase size
}
int main() {
int arr[10] = {10, 20, 30, 40, 50}; // Initial
array
int size = 5, pos = 2, val = 25; // Position and
value to insert
insertElement(arr, &size, pos, val);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print updated array
}
return 0;
}
Output:
10 20 25 30 40 50
#include <stdio.h>
void deleteElement(int arr[], int *size, int
position) {
for (int i = position; i < *size - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements left
}
(*size)--; // Decrease size
}
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5, pos = 2; // Position to delete
deleteElement(arr, &size, pos);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print updated array
}
return 0;
}
Output:
10 20 40 50
Summary
Operati
Explanation
on
Travers
Visit and display all elements.
al
A Linked List is a linear data structure where elements (called nodes) are linked
using pointers. Unlike arrays, linked lists don’t have a fixed size and can grow
or shrink dynamically.
Each node contains:
• Data (value stored in the node)
• Pointer (next) (address of the next node)
Example of a simple Singly Linked List:
[10 | * ] → [20 | * ] → [30 | NULL]
Here, each node points to the next node, and the last node points to NULL.
Insertion
Deletion