DS Practical File P
DS Practical File P
1.Linear Search
#include <stdio.h>
int main() {
int n, i, key;
return 0;
}
2.Binary Search
#include <stdio.h>
int main() {
int n, i, key;
return 0;
}
.Insertion Sort
3
#include <stdio.h>
int main() {
int n, i;
return 0;
}
.Selection Sort
4
#include <stdio.h>
// Find the minimum element in the unsorted part of the array
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element of the
unsorted part
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int n, i;
return 0;
}
.Quick Sort
5
#include <stdio.h>
int main() {
int n;
return 0;
}
.Heap Sort
6
#include <stdio.h>
// Function to "heapify" a subtree rooted at node 'i' in an array of size 'n'
void heapify(int arr[], int n, int i) {
int largest = i; // Assume the root is the largest
int left = 2 * i + 1; // Left child
int right = 2 * i + 2; // Right child
// Check if the left child exists and is larger than the root
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
// Check if the right child exists and is larger than the current largest
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
// If the largest is not the root, swap and heapify the affected subtree
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
return 0;
}
v oid multiplyMatrices(int A[][10], int B[][10], int result[][10], int r1, int c1,
int r2, int c2) {
// Check if multiplication is possible
if (c1 != r2) {
rintf("Matrix multiplication is not possible. Number of columns of
p
first matrix must equal number of rows of second matrix.\n");
return;
}
// Initialize the result matrix with zeros
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
}
}
// Multiply the matrices
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}
void printMatrix(int matrix[][10], int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int r1, c1, r2, c2;
// Input: Rows and columns of first matrix
printf("Enter the number of rows and columns of the first matrix: ");
scanf("%d %d", &r1, &c1);
return 0;
}
rev->next = temp->next;
p
free(temp);
printf("Deleted node with value %d.\n", value);
}
if (position == 0) {
*head = temp->next;
free(temp);
printf("Deleted node at position %d.\n", position);
return;
}
do {
printf("\n--- Linked List Operations ---\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Insert after a node\n");
printf("4. Delete node by value\n");
printf("5. Delete node at position\n");
printf("6. Search for a value\n");
rintf("7. Traverse the list\n");
p
printf("8. Print the List\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter value of the node after which to insert: ");
scanf("%d", &position);
struct Node* temp = head;
while (temp != NULL && temp->data != position) {
temp = temp->next;
}
if (temp != NULL) {
insertAfterNode(temp, data);
} else {
printf("Node with value %d not found.\n", position);
}
break;
case 4:
printf("Enter value of the node to delete: ");
scanf("%d", &data);
deleteNodeByValue(&head, data);
break;
case 5:
printf("Enter position of the node to delete (starting from 0):
");
s canf("%d", &position);
deleteNodeAtPosition(&head, position);
break;
case 6:
printf("Enter value to search for: ");
scanf("%d", &data);
searchNode(head, data);
break;
case 7:
traverseList(head);
break;
case 8:
printf("Linked list: ");
printList(head);
break;
case 9:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 9);
return 0;
}
9.Implementation of Stack using Array with following options.
a.Push
b.Pop
c.Exit
#include <stdio.h>
#include <stdlib.h>
int main() {
int choice, value;
do {
printf("\n--- Stack Operations ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
1. Implementation of Queue using Array with following
1
options :
a. Insert an Item into Queue
b. Delete an Item from Queue
c. Exit
#include <stdio.h>
#include <stdlib.h>
int main() {
int choice, value;
do {
printf("\n--- Queue Operations ---\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
free(temp);
}
// Function to display the elements of the queue
void display() {
if (front == NULL) {
printf("Queue is empty.\n");
} else {
struct Node* temp = front;
printf("Queue elements are: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main() {
int choice, value;
do {
printf("\n--- Queue Operations ---\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
3. Factorial of a number using recursion
1
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}