Safal Dsa
Safal Dsa
E1. Write a program to find the mean and the median of the numbers stored
in an array
Syntax:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
float calculate_mean(int arr[], int size) {
float sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum / size;
}
float calculate_median(int arr[], int size) {
float median;
int temp;
// Sort the array first
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Calculate median
if (size % 2 == 0)
median = (arr[size / 2 - 1] + arr[size / 2]) / 2.0;
else
median = arr[size / 2];
return median;
}
int main() {
int numbers[SIZE] = {5, 3, 1, 9, 7, 2, 8, 4, 6, 0};
float mean, median;
mean = calculate_mean(numbers, SIZE);
median = calculate_median(numbers, SIZE);
printf("Mean = %.2f\n", mean);
printf("Median = %.2f\n", median);
return 0;
}
Output:
Mean = 4.50
Median = 4.50
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
2|Page Data Structure And Algorithm using C
int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
3|Page Data Structure And Algorithm using C
Output:
Array: 1 2 10 3 4 5
Array: 1 2 10 4 5
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
4|Page Data Structure And Algorithm using C
E3. Write a program to Linear & Binary search for a number in an array.
Syntax:
#include <stdio.h>
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return the index where the key is found
}
}
return -1; // Return -1 if key is not found
}
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
return mid; // Return the index where the key is found
} else if (arr[mid] < key) {
low = mid + 1; // Search the right half
} else {
high = mid - 1; // Search the left half
}
}
return -1; // Return -1 if key is not found
}
int main() {
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 13;
int linearIndex = linearSearch(arr, size, key);
if (linearIndex != -1) {
printf("Linear search: Key %d found at index %d\n", key,
linearIndex);
} else {
printf("Linear search: Key %d not found\n", key);
}
int binaryIndex = binarySearch(arr, size, key);
if (binaryIndex != -1) {
printf("Binary search: Key %d found at index %d\n", key,
binaryIndex);
} else {
printf("Binary search: Key %d not found\n", key);
}
return 0;
}
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
5|Page Data Structure And Algorithm using C
Output:
Linear search: Key 13 found at index 6
Binary search: Key 13 found at index 6
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
6|Page Data Structure And Algorithm using C
#include <stdio.h>
#define STUDENTS 10
#define COURSES 5
int main() {
int marks[STUDENTS][COURSES];
scanf("%d", &marks[i][j]);
printf("\n");
return 0;
}Output:
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
7|Page Data Structure And Algorithm using C
...
Student 1: 80 75 85 90 88
Student 2: ...
...
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
8|Page Data Structure And Algorithm using C
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to insert a new node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int newData) {
// Allocate memory for new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// Check if memory allocation was successful
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
// Assign data to the new node
newNode->data = newData;
// Link the new node to the current head
newNode->next = *head;
// Update the head to point to the new node
*head = newNode;
}
// Function to delete a node with given key from the linked list
void deleteNode(struct Node** head, int key) {
// Initialize pointers for traversal and previous node
struct Node* temp = *head;
struct Node* prev = NULL;
// If the key is found at the head
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
9|Page Data Structure And Algorithm using C
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
10 | P a g e Data Structure And Algorithm using C
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
// Display the list
printf("Initial linked list: ");
displayList(head);
// Delete a node with key 20 from the list
deleteNode(&head, 20);
// Display the updated list
printf("Updated linked list: ");
displayList(head);
return 0;
}
Output:
Ini al linked list: 30 -> 20 -> 10 -> NULL
Updated linked list: 30 -> 10 -> NULL
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
11 | P a g e Data Structure And Algorithm using C
E6. Write a program to print the elements of a linked list in reverse order without
disturbing the linked list.
Syntax:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to insert a new node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int newData) {
// Allocate memory for new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// Check if memory allocation was successful
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
// Assign data to the new node
newNode->data = newData;
// Link the new node to the current head
newNode->next = *head;
// Update the head to point to the new node
*head = newNode;
}
// Function to print elements of the linked list in reverse order
void printReverse(struct Node* head) {
if (head == NULL) // Base case: empty list
return;
printReverse(head->next); // Recursively call printReverse for the
next node
printf("%d -> ", head->data); // Print the data of the current node
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
12 | P a g e Data Structure And Algorithm using C
}
int main() {
// Initialize an empty linked list
struct Node* head = NULL;
// Insert some elements at the beginning of the list
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
// Display the original list
printf("Original linked list: ");
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
// Print the list in reverse order
printf("Reversed linked list: ");
printReverse(head);
printf("NULL\n");
return 0;
}
Output:
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
13 | P a g e Data Structure And Algorithm using C
Syntax:
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
bubbleSort(arr, size);
printf("Sorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
14 | P a g e Data Structure And Algorithm using C
Output:-
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
15 | P a g e Data Structure And Algorithm using C
#include <stdlib.h>
struct Node {
int coefficient;
int exponent;
};
if (newNode == NULL) {
return;
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
if (*poly == NULL) {
*poly = newNode;
} else {
temp = temp->next;
temp->next = newNode;
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
16 | P a g e Data Structure And Algorithm using C
temp1 = temp1->next;
temp2 = temp2->next;
} else {
temp1 = temp1->next;
temp2 = temp2->next;
temp1 = temp1->next;
temp2 = temp2->next;
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
17 | P a g e Data Structure And Algorithm using C
return result;
if (poly->next != NULL) {
printf("+ ");
poly = poly->next;
printf("\n");
int main() {
insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 2, 1);
insertTerm(&poly1, 1, 0);
printf("Polynomial 1: ");
displayPolynomial(poly1);
insertTerm(&poly2, 4, 3);
insertTerm(&poly2, 2, 1);
insertTerm(&poly2, 1, 0);
printf("Polynomial 2: ");
displayPolynomial(poly2);
// Add polynomials
displayPolynomial(result)
return 0;
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
18 | P a g e Data Structure And Algorithm using C
Output:
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
19 | P a g e Data Structure And Algorithm using C
#include <stdio.h>
#include <stdlib.h>
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
20 | P a g e Data Structure And Algorithm using C
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
return;
}
// Traverse the list to find the key
while (temp != NULL && temp->data != key) {
temp = temp->next;
}
if (temp == NULL) {
printf("Key not found in the list\n");
return;
}
// If key is found, unlink the node from the list
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
}
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
21 | P a g e Data Structure And Algorithm using C
int main() {
struct Node* head = NULL;
return 0;
}
Output:
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
22 | P a g e Data Structure And Algorithm using C
E10. Write a program to implement a stack opera on Push, Pop using an array and linked
list
Syntax:
#include <stdio.h>
#include <stdlib.h>
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
23 | P a g e Data Structure And Algorithm using C
int main() {
struct ArrayStack* stack = createArrayStack(MAX_SIZE);
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
24 | P a g e Data Structure And Algorithm using C
return 0;
}
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
40 pushed to stack
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
25 | P a g e Data Structure And Algorithm using C
Syntax:
#include <stdio.h>
// Function to partition the array and return the index of the pivot
element
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Select the last element as the pivot
int i = low - 1; // Index of the smaller element
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
26 | P a g e Data Structure And Algorithm using C
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
Output:
Original array: 10 7 8 9 1 5
Sorted array: 1 5 7 8 9 10
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
27 | P a g e Data Structure And Algorithm using C
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
28 | P a g e Data Structure And Algorithm using C
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
29 | P a g e Data Structure And Algorithm using C
exit(EXIT_FAILURE);
}
int main() {
struct CircularQueue* queue = createCircularQueue();
return 0;
}
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
30 | P a g e Data Structure And Algorithm using C
Output:
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
31 | P a g e Data Structure And Algorithm using C
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
32 | P a g e Data Structure And Algorithm using C
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas
33 | P a g e Data Structure And Algorithm using C
switch (exp[i]) {
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2 / val1); break;
}
}
}
return pop(stack);
}
int main() {
char exp[] = "231*+9-"; // Postfix expression: 2 + (3 * 1) - 9
printf("Postfix expression: %s\n", exp);
printf("Result: %d\n", evaluatePostfix(exp));
return 0;
}
Output:
Result: 2
Submi ed by:- Safal Malhotra(3rd Sem) Submi ed to:- Rishi Raj Vyas