List of Programs Data Structure Unit 1
List of Programs for Data Structure in C and Python
Theory & Explanation
List of Programs - Unit 1: Data Structure
1. Built-in Data Types and Their Sizes
C Program
#include <stdio.h>
int main() {
int i = 10;
float f = 3.14;
double d = 9.81234;
char c = 'A';
printf("int: %d, size: %zu bytes\n", i, sizeof(i));
printf("float: %.2f, size: %zu bytes\n", f, sizeof(f));
printf("double: %.5lf, size: %zu bytes\n", d, sizeof(d));
printf("char: %c, size: %zu bytes\n", c, sizeof(c));
return 0;
}
Expected Output
int: 10, size: 4 bytes
float: 3.14, size: 4 bytes
double: 9.81234, size: 8 bytes
char: A, size: 1 bytes
Python Program
import sys
i = 10
f = 3.14
s = 'A'
b = True
print(f"int: {i}, size: {sys.getsizeof(i)} bytes")
print(f"float: {f}, size: {sys.getsizeof(f)} bytes")
print(f"str: {s}, size: {sys.getsizeof(s)} bytes")
print(f"bool: {b}, size: {sys.getsizeof(b)} bytes")
Expected Output
int: 10, size: 28 bytes
float: 3.14, size: 24 bytes
str: A, size: 42 bytes
bool: True, size: 28 bytes
2. Elementary Data Organization (Record / Structure)
C Program
#include <stdio.h>
struct Student {
int id;
char name[20];
float marks;
};
int main() {
struct Student s1 = {101, "Aarav", 88.5};
printf("ID: %d\nName: %s\nMarks: %.1f\n", s1.id, s1.name, s1.marks);
return 0;
}
Expected Output
ID: 101
Name: Aarav
Marks: 88.5
Python Program
class Student:
def __init__(self, student_id, name, marks):
self.id = student_id
self.name = name
self.marks = marks
s1 = Student(101, "Aarav", 88.5)
print(f"ID: {s1.id}\nName: {s1.name}\nMarks: {s1.marks}")
Expected Output
ID: 101
Name: Aarav
Marks: 88.5
3. Efficiency of an Algorithm (O(1) vs O(n) Operation Count)
C Program
#include <stdio.h>
int sumConstant(int n) {
return n (n + 1) / 2;
}
int sumLinear(int n, int steps) {
int sum = 0;
steps = 0;
for (int i = 1; i <= n; i++) {
sum += i;
(steps)++;
}
return sum;
}
int main() {
int n = 100, steps = 0;
printf("O(1) Sum: %d, Steps: 1\n", sumConstant(n));
int res = sumLinear(n, &steps);
printf("O(n) Sum: %d, Steps: %d\n", res, steps);
return 0;
}
Expected Output
O(1) Sum: 5050, Steps: 1
O(n) Sum: 5050, Steps: 100
Python Program
def sum_constant(n):
return n (n + 1) // 2, 1
def sum_linear(n):
total = 0
steps = 0
for i in range(1, n + 1):
total += i
steps += 1
return total, steps
n = 100
res1, steps1 = sum_constant(n)
res2, steps2 = sum_linear(n)
print(f"O(1) Sum: {res1}, Steps: {steps1}")
print(f"O(n) Sum: {res2}, Steps: {steps2}")
Expected Output
O(1) Sum: 5050, Steps: 1
O(n) Sum: 5050, Steps: 100
4. Asymptotic Notations Demonstration (Best, Average, Worst Case in Linear Search)
C Program
#include <stdio.h>
int linearSearch(int arr[], int n, int key, int comparisons) {
comparisons = 0;
for (int i = 0; i < n; i++) {
(comparisons)++;
if (arr[i] == key) return i;
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, comp;
linearSearch(arr, n, 10, &comp);
printf("Best Case Omega(1) Comparisons: %d\n", comp);
linearSearch(arr, n, 30, &comp);
printf("Average Case Theta(n) Comparisons: %d\n", comp);
linearSearch(arr, n, 99, &comp);
printf("Worst Case O(n) Comparisons: %d\n", comp);
return 0;
}
Expected Output
Best Case Omega(1) Comparisons: 1
Average Case Theta(n) Comparisons: 3
Worst Case O(n) Comparisons: 5
Python Program
def linear_search(arr, key):
comparisons = 0
for i, val in enumerate(arr):
comparisons += 1
if val == key:
return i, comparisons
return -1, comparisons
arr = [10, 20, 30, 40, 50]
_, best = linear_search(arr, 10)
_, avg = linear_search(arr, 30)
_, worst = linear_search(arr, 99)
print(f"Best Case Omega(1) Comparisons: {best}")
print(f"Average Case Theta(n) Comparisons: {avg}")
print(f"Worst Case O(n) Comparisons: {worst}")
Expected Output
Best Case Omega(1) Comparisons: 1
Average Case Theta(n) Comparisons: 3
Worst Case O(n) Comparisons: 5
5. Abstract Data Type (ADT) Implementation (List ADT)
C Program
#include <stdio.h>
#define MAX 5
struct ListADT {
int data[MAX];
int size;
};
void insert(struct ListADT list, int val) {
if (list->size < MAX) {
list->data[list->size++] = val;
}
}
void display(struct ListADT list) {
for (int i = 0; i < list->size; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
struct ListADT myList = {{0}, 0};
insert(&myList, 15);
insert(&myList, 25);
insert(&myList, 35);
display(&myList);
return 0;
}
Expected Output
15 25 35
Python Program
class ListADT:
def __init__(self, capacity=5):
self.capacity = capacity
self.data = []
def insert(self, val):
if len(self.data) < self.capacity:
self.data.append(val)
def display(self):
print(" ".join(map(str, self.data)))
my_list = ListADT()
my_list.insert(15)
my_list.insert(25)
my_list.insert(35)
my_list.display()
Expected Output
15 25 35
6. Single Dimensional Array (Traversal, Insertion, Deletion)
C Program
#include <stdio.h>
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int n = 5, pos = 2, val = 25;
for (int i = n; i > pos; i--) arr[i] = arr[i - 1];
arr[pos] = val;
n++;
printf("After Insertion: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
for (int i = pos; i < n - 1; i++) arr[i] = arr[i + 1];
n--;
printf("\nAfter Deletion: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
Expected Output
After Insertion: 10 20 25 30 40 50
After Deletion: 10 20 30 40 50
Python Program
arr = [10, 20, 30, 40, 50]
pos, val = 2, 25
arr.insert(pos, val)
print("After Insertion:", arr)
arr.pop(pos)
print("After Deletion:", arr)
Expected Output
After Insertion: 10 20 25 30 40 50
After Deletion: 10 20 30 40 50
7. Multidimensional Array (2D Matrix Input and Display)
C Program
#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Expected Output
1 2 3
4 5 6
Python Program
matrix = [
[1, 2, 3],
[4, 5, 6]
]
for row in matrix:
print(row)
Expected Output
1 2 3
4 5 6
8. Representation of Arrays: Row Major Order
C Program
#include <stdio.h>
int main() {
int rows = 3, cols = 4;
int baseAddress = 1000, size = 4;
int i = 1, j = 2;
int address = baseAddress + ((i cols) + j) size;
printf("Row Major Address of A[%d][%d]: %d\n", i, j, address);
return 0;
}
Expected Output
Row Major Address of A[1][2]: 1024
Python Program
rows, cols = 3, 4
base_address, size = 1000, 4
i, j = 1, 2
address = base_address + ((i cols) + j) size
print(f"Row Major Address of A[{i}][{j}]: {address}")
Expected Output
Row Major Address of A[1][2]: 1024
9. Representation of Arrays: Column Major Order
C Program
#include <stdio.h>
int main() {
int rows = 3, cols = 4;
int baseAddress = 1000, size = 4;
int i = 1, j = 2;
int address = baseAddress + ((j rows) + i) size;
printf("Column Major Address of A[%d][%d]: %d\n", i, j, address);
return 0;
}
Expected Output
Column Major Address of A[1][2]: 1028
Python Program
rows, cols = 3, 4
base_address, size = 1000, 4
i, j = 1, 2
address = base_address + ((j rows) + i) * size
print(f"Column Major Address of A[{i}][{j}]: {address}")
Expected Output
Column Major Address of A[1][2]: 1028
Advertisement