Ds Report
Ds Report
Report
1. Arrays Implementation
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void createArray(int arr[], int *size);
void displayArray(int arr[], int size);
void insertElement(int arr[], int *size, int element, int posi
void deleteElement(int arr[], int *size, int position);
void addArrays(int arr1[], int arr2[], int result[], int size
void multiplyArrays(int arr1[], int arr2[], int result[], int
void sparseMatrix();
int main() {
int arr[MAX], size = 0, choice, element, position;
int arr1[MAX], arr2[MAX], result[MAX];
while (1) {
printf("\nArray Operations:\n");
printf("1. Create Array\n");
printf("2. Display Array\n");
printf("3. Insert Element\n");
printf("4. Delete Element\n");
printf("5. Add Two Arrays\n");
printf("6. Multiply Two Arrays\n");
printf("7. Sparse Matrix Representation\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
void sparseMatrix() {
int rows, cols, nonZero = 0, value;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] != 0) nonZero++;
}
}
int sparse[nonZero][3], k = 0;
printf("Sparse Matrix Representation:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
Output:
int main() {
struct Node* singlyHead = NULL;
struct DNode* doublyHead = NULL;
struct CNode* circularHead = NULL;
return 0;
}
Output:
3. Stack Implementation
#include <stdio.h>
#include <stdlib.h>
// Stack operations
push(stack, 10);
push(stack, 20);
push(stack, 30);
displayStack(stack);
printf("Popped: %d\n", pop(stack));
displayStack(stack);
return 0;
}
// Stack functions
struct Stack* createStack(int capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int)
return stack;
}
Output:
Stack:
30
20
10
Popped: 30
Stack:
20
10
4. Queue Implementation
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Queue* queue = createQueue(5);
// Queue operations
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
displayQueue(queue);
printf("Dequeued: %d\n", dequeue(queue));
displayQueue(queue);
return 0;
}
// Queue functions
struct Queue* createQueue(int capacity) {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct
queue->capacity = capacity;
queue->front = queue->rear = -1;
queue->array = (int*)malloc(queue->capacity * sizeof(int)
return queue;
}
Output:
Queue:
10 20 30
Dequeued: 10
int main() {
// Binary Search Tree Operations
TreeNode* root = NULL;
root = insertBST(root, 50);
root = insertBST(root, 30);
root = insertBST(root, 70);
root = insertBST(root, 20);
root = insertBST(root, 40);
root = insertBST(root, 60);
root = insertBST(root, 80);
printf("In-order traversal of BST: ");
inorderBST(root);
printf("\n");
Output:
6. Graph Implementation
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Graph* graph = createGraph(5);
// Adding edges
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
return 0;
}
return graph;
}
Output:
Adjacency Matrix:
0 1 0 0 1
1 0 1 1 1
0 1 0 1 0
0 1 1 0 1
1 1 0 1 0
6. Searching
#include <stdio.h>
// Function prototypes
int sequentialSearch(int arr[], int size, int key);
int binarySearch(int arr[], int left, int right, int key);
void sortArray(int arr[], int size);
int main() {
int arr[] = {23, 12, 56, 34, 78, 90, 11};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 34;
// Sequential Search
int seqIndex = sequentialSearch(arr, size, key);
if (seqIndex != -1)
printf("Sequential Search: Element %d found at inde
x %d\n", key, seqIndex);
else
printf("Sequential Search: Element %d not found\n",
key);
// Binary Search
int binIndex = binarySearch(arr, 0, size - 1, key);
if (binIndex != -1)
printf("Binary Search: Element %d found at index %d
\n", key, binIndex);
else
printf("Binary Search: Element %d not found\n", ke
y);
return 0;
}
Output:
Sequential Search: Element 34 found at index 3
Binary Search: Element 34 found at index 3
7. Sorting
#include <stdio.h>
#include <stdlib.h>
radixSort(arr, n);
return 0;
}