Assignment Ds in C
Assignment Ds in C
PROGRAM 1
1. Perform following operations on 1-D Array:
a. Insert an element at specified position.
b. Delete an element of specified position.
c. Display all elements.
#include <stdio.h>
int main() {
int choice;
while (1) {
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
if (size == 100) {
} else {
scanf("%d", &position);
printf("Invalid position. Please enter a valid position between 0 and %d.\n", size);
} else {
scanf("%d", &element);
arr[position] = element;
size++;
break;
case 2:
if (size == 0) {
} else {
int position;
scanf("%d", &position);
printf("Invalid position. Please enter a valid position between 0 and %d.\n", size - 1);
} else {
size--;
}
}
break;
case 3:
if (size == 0) {
printf("Array is empty.\n");
} else {
printf("\n");
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
return 0;
OUTPUT
PROGRAM 2
2. Create a linked list with nodes having information about a student and perform
a. Insert a new node at specified position.
b. Delete of a node with the roll number of student specified.
c. Reversal of that linked list.
Coding
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (current == NULL) {
printf("Student with roll number %d not found.\n", rollNumber);
return;
}
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
printf("Student with roll number %d deleted.\n", rollNumber);
}
*head = prev;
}
int main() {
struct Student* head = NULL;
// Insert nodes
insertNode(&head, 0, 101, "Akash");
insertNode(&head, 1, 102, "Deepak");
insertNode(&head, 1, 103, "Ankur");
insertNode(&head, 2, 104, "Deepanshu");
// Delete a node
deleteNode(&head, 102);
printf("Linked List after deleting a student:\n");
displayList(head);
return 0;
}
SSS
PROGRAM 3
3. Create doubly linked list with nodes having information about an employee and perform Insertion at
front of doubly linked list and perform deletion at end of that doubly linked list.
CODING
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int employeeID;
char name[50];
};
newEmployee->employeeID = employeeID;
strcpy(newEmployee->name, name);
newEmployee->next = NULL;
newEmployee->prev = NULL;
return newEmployee;
// Function to insert an employee node at the front of the doubly linked list
*head = newEmployee;
} else {
newEmployee->next = *head;
(*head)->prev = newEmployee;
*head = newEmployee;
// Function to delete an employee node at the end of the doubly linked list
if (*head == NULL) {
return;
current = current->next;
if (current->prev != NULL) {
current->prev->next = NULL;
} else {
free(current);
current = current->next;
int main() {
displayFromFront(head);
deleteAtEnd(&head);
displayFromFront(head);
return 0;
OUTPUT
PROGRAM 4
Q4 Create a stack and perform Push, Pop, Peek and Traverse operations on the stack using Array or Linked
list.
CODING
#include <stdio.h>
int stack[MAX_SIZE];
if (top == MAX_SIZE - 1) {
return;
stack[++top] = value;
int pop() {
if (top == -1) {
int peek() {
if (top == -1) {
return stack[top];
void traverse() {
if (top == -1) {
printf("Stack is empty.\n");
return;
printf("\n");
int main() {
while (true) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
traverse();
break;
case 5:
return 0;
default:
}
OUTPUT
PROGRAM 5
4. Create a Linear Queue using Array or Linked List and implement different operations such as
Insert, Delete, and Display the queue elements.
CODING
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
q->front = -1;
q->rear = -1;
if (isFull(q)) {
} else {
if (isEmpty(q)) {
q->front = 0;
q->rear++;
q->items[q->rear] = value;
int item;
if (isEmpty(q)) {
return -1;
} else {
item = q->items[q->front];
q->front++;
return item;
}
int i;
if (isEmpty(q)) {
printf("Queue is empty.\n");
} else {
printf("\n");
int main() {
struct Queue q;
initQueue(&q);
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Quit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
return 0;
OUTPUT
PROGRAM 6
6. Implement sparse matrix using array. Description of program:
a. Read a 2-D array from the user.
CODING
#include <stdio.h>
struct SparseElement {
int row;
int col;
int value;
};
int k = 0;
if (arr[i][j] != 0) {
sparseArr[k].row = i;
sparseArr[k].col = j;
sparseArr[k].value = arr[i][j];
k++;
}
sparseArr[k].row = rows;
sparseArr[k].col = cols;
int k = sparseArr[0].value;
printf("Sparse Matrix:\n");
printf("Row\tColumn\tValue\n");
int main() {
scanf("%d", &rows);
scanf("%d", &cols);
int arr[MAX_ROWS][MAX_COLS];
printf("Enter the elements of the 2-D array:\n");
scanf("%d", &arr[i][j]);
printSparseMatrix(sparseArr);
return 0;
OUTPUT
PROGRAM 7
7 Implement insertion, deletion and traversals (inorder, preorder and postorder) on Binary Search Tree.
CODING
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
return newNode;
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
return root;
if (root != NULL) {
inorderTraversal(root->left);
inorderTraversal(root->right);
if (root != NULL) {
preorderTraversal(root->left);
preorderTraversal(root->right);
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
current = current->left;
return current;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
free(root);
return temp;
return temp;
root->data = temp->data;
return root;
if (root != NULL) {
freeBST(root->left);
freeBST(root->right);
free(root);
int main() {
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
printf("\n");
preorderTraversal(root);
printf("\n");
postorderTraversal(root);
printf("\n");
inorderTraversal(root);
printf("\n");
return 0;
OUTPUT
PROGRAM 8
8 Implement Selection Sort, Bubble Sort, Insertion sort, Merge sort, Quick sort, and Heap Sort using array as a
data structure.
CODING
#include <stdio.h>
// Selection Sort
// Implementation
// Bubble Sort
// Implementation
// Insertion Sort
// Implementation
// Merge Sort
// Implementation
// Implementation
}
// Quick Sort
// Implementation
// Implementation
// Heap Sort
// Implementation
// Implementation
int main() {
// Selection Sort
printf("Selection Sort:\n");
int selectionArr[n];
selectionArr[i] = arr[i];
selectionSort(selectionArr, n);
printf("\n");
// Bubble Sort
printf("Bubble Sort:\n");
int bubbleArr[n];
bubbleArr[i] = arr[i];
bubbleSort(bubbleArr, n);
printf("\n");
// Insertion Sort
printf("Insertion Sort:\n");
int insertionArr[n];
insertionArr[i] = arr[i];
insertionSort(insertionArr, n);
printf("\n");
// Merge Sort
printf("Merge Sort:\n");
int mergeArr[n];
mergeArr[i] = arr[i];
mergeSort(mergeArr, 0, n - 1);
printf("\n");
// Quick Sort
printf("Quick Sort:\n");
int quickArr[n];
quickArr[i] = arr[i];
quickSort(quickArr, 0, n - 1);
printf("\n");
// Heap Sort
printf("Heap Sort:\n");
int heapArr[n];
heapArr[i] = arr[i];
heapSort(heapArr, n);
printf("\n");
return 0;
OURPUT
PROGRAM 9
9 Company ABC maintains its daily sale amount in a one dimensional array. The sales manager of the
company wants to find a pair with the given total sale amount from the stored sales data if any. You are
required to write the code using suitable data structure(s) to solve his problem.
CODING
#include <stdio.h>
struct Node {
int key;
int value;
};
newNode->key = key;
newNode->value = value;
newNode->next = NULL;
return newNode;
if (hashTable[index] == NULL) {
} else {
temp = temp->next;
if (hashTable[index] != NULL) {
if (temp->value == complement) {
printf("Pair found: %d + %d = %d\n", sales[i], complement, target);
return;
temp = temp->next;
int main() {
int sales[] = {10, 20, 30, 40, 50}; // Replace with your daily sale data
int totalSaleAmount = 70; // Change this to the desired total sale amount
return 0;
OUTPUT
PROGRAM 10
10 Aman created two Binary Trees of the same data using two different algorithms. Now he wants
check that both the Binary Trees are identical or not. You are required to write the code using
suitable data structure(s) to solve his problem.
CODING
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
return true;
}
return false;
areIdentical(root1->right, root2->right);
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
int main() {
root1->left = createNode(2);
root1->right = createNode(3);
root1->left->left = createNode(4);
root1->left->right = createNode(5);
root2->left = createNode(2);
root2->right = createNode(3);
root2->left->left = createNode(4);
root2->left->right = createNode(5);
if (areIdentical(root1, root2)) {
printf("Both Binary Trees are identical.\n");
} else {
freeTree(root1);
freeTree(root2);
return 0;
OUTPUT