DS Lab Assignment PDF
DS Lab Assignment PDF
Write a program in C to read n number of values in an array and display it in reverse order
#include <stdio.h>
int main()
{
int n, num;
printf("Enter the n-numbers: ");
scanf("%d", &n);
int array1[n];
return 0;
}
#include<stdio.h>
int main(){
int n, newValue;
int array1[n];
// Enter elements
printf("Enter the elements:\n");
for(int i = 0;i<=n-1;i++){
scanf("%d", &array1[i]);
}
array1[n] = newValue;
n++;
return 0;
}
#include <stdio.h>
int main()
{
int n, target;
int array1[n];
int found = 0;
for (int i = 0; i < n; i++)
{
if(array1[i] == target){
found = 1;
for(int j = i; j < n-1; j++){
array1[j] = array1[j+1];
}
n--;
i--;
}
}
if(found){
printf("\nThe array after deletion:\n");
for(int i=0; i<n;i++){
printf("%3d", array1[i]);
}
}
else{
printf("\nElement not FOUND!");
}
return 0;
}
#include<stdio.h>
int main(){
int even,odd,n;
printf("Enter the n-terms number: ");
scanf("%d", &n);
int num[n];
printf("Enter the elements of array: \n");
for(int i = 0; i < n;i++){
scanf("%d", &num[i]);
}
return 0;
}
#include<stdio.h>
int main(){
int n, num, large;
printf("Enter the n-terms Number: ");
scanf("%d",&n);
int arr[n];
large = arr[0];
for(int i = 0; i < n; i++){
if(arr[i] > large){
large = arr[i];
}
}
printf("Program Terminated");
return 0;
}
6.Write a C Program to Find the Transpose
#include <stdio.h>
int main()
{
int rows, cols;
printf("Enter the no. of rows: ");
scanf("%d", &rows);
printf("Enter the no. of columns: ");
scanf("%d", &cols);
// Insetion of elements
printf("Enter the elements: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &array[i][j]);
}
}
// Transposing of Array:
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
transpose[j][i] = array[i][j];
}
}
return 0;
}
#include<stdio.h>
int main(){
int n;
printf("Enter the element to make 2D Array: ");
scanf("%d", &n);
return 0;
}
int main() {
int rows1, cols1, rows2, cols2;
if (cols1 != rows2) {
printf("Matrix multiplication is not possible. Columns of the first matrix must be equal to rows of
the second matrix.\n");
return 1; // Exit with an error code
}
return 0;
}
#include <stdio.h>
int main() {
int n;
int matrix[n][n];
int primaryDiagonalSum = 0;
int secondaryDiagonalSum = 0;
return 0;
}
#include <stdio.h>
int main() {
int n, target;
int array[n];
int count = 0;
return 0;
}
#include <stdio.h>
int main() {
int number = 42; // Declare an integer variable
int *ptr; // Declare an integer pointer variable
return 0;
}
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
#include <stdio.h>
return 0;
}
14. Write a program that uses functions to perform the following operations on singly linked list:
i) Creation ii) Insertion iii) Deletion iv) Display. */
#include <stdio.h>
#include <stdlib.h>
// Function to delete a node with a given value from the linked list
void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
if (current == NULL) {
printf("Node with data %d not found.\n", key);
} else {
prev->next = current->next;
free(current);
printf("Deleted node with data %d\n", key);
}
}
int main() {
struct Node* head = NULL;
// Delete a node
deleteNode(&head, 20);
return 0;
}
15. Write a program that uses functions to perform the following operations on doubly linked list
i) Creation ii) Insertion iii) Deletion iv) Display. */
#include <stdio.h>
#include <stdlib.h>
// Function to delete a node with a given value from the doubly linked list
void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
if (current == NULL) {
printf("Node with data %d not found.\n", key);
} else {
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
printf("Deleted node with data %d\n", key);
}
}
current = head;
while (current != NULL && current->next != NULL) {
current = current->next;
}
printf("Backward: ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->prev;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
// Delete a node
deleteNode(&head, 20);
return 0;
}
16. Write a program that uses functions to perform the following operations on circular linked List
i) Creation ii) Insertion iii) Deletion iv) Display. */
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the circular linked list
struct Node {
int data;
struct Node* next;
};
// Function to delete a node with a given value from the circular linked list
void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
if (current->data != key) {
printf("Node with data %d not found.\n", key);
} else {
if (current == *head && current->next == *head) {
*head = NULL;
} else if (current == *head) {
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
}
*head = (*head)->next;
last->next = *head;
} else {
prev->next = current->next;
}
free(current);
printf("Deleted node with data %d\n", key);
}
}
int main() {
struct Node* head = NULL;
// Delete a node
deleteNode(&head, 20);
return 0;
}
17. Write a program that implement stack and its operations using
i) Arrays OR ii) Linked list (Pointers). */
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int items[MAX_SIZE];
int top;
};
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
display(&stack);
display(&stack);
return 0;
}
18. Write a program that implement Queue and its operations using
i) ArraysOR ii) Linked list (Pointers). */
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int items[MAX_SIZE];
int front, rear;
};
int main() {
struct Queue queue;
initialize(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
display(&queue);
display(&queue);
return 0;
}
19. Write a program that performs the searching operations for a Key value in a given list of integers
using Linear search Technique.*/
#include <stdio.h>
int main() {
int n, key;
int arr[n];
if (result != -1) {
printf("Key %d found at index %d.\n", key, result);
} else {
printf("Key %d not found in the list.\n", key);
}
return 0;
}
20. Write a program that performs the searching operations for a Key value in a given list of integers
using Binary search Technique. */
#include <stdio.h>
if (arr[mid] == key) {
return mid; // Key found at index mid
}
int main() {
int n, key;
printf("Enter the number of elements in the sorted list: ");
scanf("%d", &n);
int arr[n];
if (result != -1) {
printf("Key %d found at index %d.\n", key, result);
} else {
printf("Key %d not found in the list.\n", key);
}
return 0;
}
#include <stdio.h>
int main() {
int n, temp;
int array[n];
return 0;
}
#include <stdio.h>
int main() {
int n, temp;
int array[n];
// Swap the minimum element with the first element in the unsorted part
if (minIndex != i) {
temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
return 0;
}
#include <stdio.h>
arr[j + 1] = key;
}
}
int main() {
int n;
int arr[n];
insertionSort(arr, n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
return root;
}
int main() {
struct TreeNode* root = NULL;
int choice, key;
do {
printf("Binary Search Tree Operations:\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Search for a key\n");
printf("4. In-order Traversal\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the key to insert: ");
scanf("%d", &key);
root = insert(root, key);
break;
case 2:
printf("Enter the key to delete: ");
scanf("%d", &key);
root = delete(root, key);
break;
case 3:
printf("Enter the key to search for: ");
scanf("%d", &key);
if (search(root, key) != NULL) {
printf("%d is present in the tree.\n");
} else {
printf("%d is not found in the tree.\n");
}
break;
case 4:
printf("In-order Traversal of the Binary Search Tree: ");
inOrderTraversal(root);
printf("\n");
break;
case 5:
// Exit the program
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
struct TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
26. Write C programs for implementing the Depth first Search (DFS) graph traversal algorithm.
// Iterative DFS:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return graph;
}
int main() {
int V = 6; // Number of vertices
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 5);
DFSTraversal(graph, startVertex);
return 0;
}
// Recursive DFS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return graph;
}
int main() {
int V = 6; // Number of vertices
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 5);
DFSTraversal(graph, startVertex);
return 0;
}
27. Write C programs for implementing the Breadth first Search (BFS) graph traversal algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return graph;
}
visited[startVertex] = true;
enqueue(queue, startVertex);
free(visited);
free(queue);
}
int main() {
int V = 6; // Number of vertices
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 5);
BFS(graph, startVertex);
return 0;
}