Assignment DS
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 arr[100]; // Maximum array size is set to 100, change it as needed
int size = 0; // Current size of the array
int choice;
while (1) {
printf("\n1. Insert element at specified position\n");
printf("2. Delete element at specified position\n");
printf("3. Display all elements\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (size == 100) {
printf("Array is full. Cannot insert more elements.\n");
} else {
int position, element;
printf("Enter position to insert: ");
scanf("%d", &position);
if (position < 0 || position > size) {
printf("Invalid position. Please enter a valid position between 0 and %d.\n", size);
} else {
printf("Enter the element to insert: ");
scanf("%d", &element);
for (int i = size; i > position; i--) {
arr[i] = arr[i - 1];
arr[position] = element;
size++;
printf("Element inserted successfully.\n");
break;
case 2:
if (size == 0) {
printf("Array is empty. Nothing to delete.\n");
} else {
int position;
printf("Enter position to delete: ");
scanf("%d", &position);
if (position < 0 || position >= size) {
printf("Invalid position. Please enter a valid position between 0 and %d.\n", size - 1);
} else {
for (int i = position; i < size - 1; i++) {
arr[i] = arr[i + 1];
size--;
printf("Element deleted successfully.\n");
}
}
break;
case 3:
if (size == 0) {
printf("Array is empty.\n");
} else {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please enter a valid option.\n");
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>
// Define the structure for a student node
struct Student {
int rollNumber;
char name[50];
struct Student* next;
};
// Function to create a new student node
struct Student* createStudent(int rollNumber, const char* name) {
struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
newStudent->rollNumber = rollNumber;
strcpy(newStudent->name, name);
newStudent->next = NULL;
return newStudent;
}
// Function to insert a new student node at a specified position
void insertNode(struct Student** head, int position, int rollNumber, const char* name) {
struct Student* newStudent = createStudent(rollNumber, name);
if (position == 0) {
newStudent->next = *head;
*head = newStudent;
} else {
struct Student* current = *head;
int i = 0;
while (i < position - 1 && current != NULL) {
current = current->next;
i++;
}
if (current == NULL) {
printf("Invalid position. Insertion at the end of the list.\n");
}
newStudent->next = current->next;
current->next = newStudent;
}
}
// Function to delete a student node with a specific roll number
void deleteNode(struct Student** head, int rollNumber) {
struct Student* current = *head;
struct Student* prev = NULL;
while (current != NULL && current->rollNumber != rollNumber) {
prev = current;
current = current->next;
}
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);
}
// Function to reverse the linked list
void reverseList(struct Student** head) {
struct Student* prev = NULL;
struct Student* current = *head;
struct Student* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
// Function to display the linked list
void displayList(struct Student* head) {
struct Student* current = head;
while (current != NULL) {
printf("Roll Number: %d, Name: %s\n", current->rollNumber, current->name);
current = current->next;
}
}
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");
printf("Original Linked List:\n");
displayList(head);
// Delete a node
deleteNode(&head, 102);
printf("Linked List after deleting a student:\n");
displayList(head);
// Reverse the linked list
reverseList(&head);
printf("Reversed Linked List:\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>
// Define the structure for an employee node
struct Employee {
int employeeID;
char name[50];
struct Employee* next;
struct Employee* prev;
};
// Function to create a new employee node
struct Employee* createEmployee(int employeeID, const char* name) {
struct Employee* newEmployee = (struct Employee*)malloc(sizeof(struct Employee));
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
void insertAtFront(struct Employee** head, int employeeID, const char* name) {
struct Employee* newEmployee = createEmployee(employeeID, name);
if (*head == NULL) {
*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
void deleteAtEnd(struct Employee** head) {
if (*head == NULL) {
printf("The list is empty. Cannot delete from an empty list.\n");
return;
struct Employee* current = *head;
while (current->next != NULL) {
current = current->next;
if (current->prev != NULL) {
current->prev->next = NULL;
} else {
*head = NULL; // The list had only one element
free(current);
// Function to display the doubly linked list from the front
void displayFromFront(struct Employee* head) {
struct Employee* current = head;
while (current != NULL) {
printf("Employee ID: %d, Name: %s\n", current->employeeID, current->name);
current = current->next;
int main() {
struct Employee* head = NULL;
// Insert employees at the front of the list
insertAtFront(&head, 101, "Akash");
insertAtFront(&head, 102, "Ankur");
insertAtFront(&head, 103, "Deepak");
printf("Doubly Linked List (from front):\n");
displayFromFront(head);
// Delete an employee from the end
deleteAtEnd(&head);
printf("\nDoubly Linked List after deleting an employee from the end:\n");
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>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
// Function to push an element onto the stack
void push(int value) {
if (top == MAX_SIZE - 1) {
printf("Stack is full. Cannot push.\n");
return;
stack[++top] = value;
printf("Pushed: %d\n", value);
// Function to pop the top element from the stack
int pop() {
if (top == -1) {
printf("Stack is empty. Cannot pop.\n");
return -1; // Return an error value
int poppedValue = stack[top--];
printf("Popped: %d\n", poppedValue);
return poppedValue;
// Function to peek at the top element of the stack
int peek() {
if (top == -1) {
printf("Stack is empty. Cannot peek.\n");
return -1; // Return an error value
printf("Peeked: %d\n", stack[top]);
return stack[top];
// Function to traverse and display all elements of the stack
void traverse() {
if (top == -1) {
printf("Stack is empty.\n");
return;
printf("Stack elements: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
printf("\n");
int main() {
int choice, value;
while (true) {
printf("\n1. Push\n2. Pop\n3. Peek\n4. Traverse\n5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
traverse();
break;
case 5:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
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>
#define MAX_SIZE 100
// Structure to represent the queue
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
// Function to create an empty queue
void initQueue(struct Queue *q) {
q->front = -1;
q->rear = -1;
// Function to check if the queue is empty
int isEmpty(struct Queue *q) {
return (q->front == -1);
// Function to check if the queue is full
int isFull(struct Queue *q) {
return (q->rear == MAX_SIZE - 1);
// Function to insert an element into the queue
void enqueue(struct Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full. Cannot enqueue.\n");
} else {
if (isEmpty(q)) {
q->front = 0;
q->rear++;
q->items[q->rear] = value;
printf("Inserted: %d\n", value);
// Function to remove an element from the queue
int dequeue(struct Queue *q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
// Reset the queue when it becomes empty
q->front = q->rear = -1;
return item;
}
// Function to display the elements of the queue
void display(struct Queue *q) {
int i;
if (isEmpty(q)) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (i = q->front; i <= q->rear; i++) {
printf("%d ", q->items[i]);
printf("\n");
int main() {
struct Queue q;
initQueue(&q);
int choice, value;
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
return 0;
OUTPUT
PROGRAM 6
6. Implement sparse matrix using array. Description of program:
a. Read a 2-D array from the user.
b. Store it in the sparse matrix form, use array of structures.
C. Print the final array.
CODING
#include <stdio.h>
#define MAX_ROWS 100
#define MAX_COLS 100
// Structure to represent a sparse matrix element
struct SparseElement {
int row;
int col;
int value;
};
// Function to convert a 2-D array into a sparse matrix
void convertToSparse(int arr[MAX_ROWS][MAX_COLS], int rows, int cols, struct
SparseElement sparseArr[]) {
int k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
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;
sparseArr[k].value = k; // Number of non-zero elements
// Function to print the sparse matrix
void printSparseMatrix(struct SparseElement sparseArr[]) {
int k = sparseArr[0].value;
printf("Sparse Matrix:\n");
printf("Row\tColumn\tValue\n");
printf("%d\t%d\t%d\n", sparseArr[0].row, sparseArr[0].col, k);
for (int i = 1; i <= k; i++) {
printf("%d\t%d\t%d\n", sparseArr[i].row, sparseArr[i].col, sparseArr[i].value);
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int arr[MAX_ROWS][MAX_COLS];
printf("Enter the elements of the 2-D array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
struct SparseElement sparseArr[MAX_ROWS * MAX_COLS];
// Convert the 2-D array to sparse matrix
convertToSparse(arr, rows, cols, sparseArr);
// Print the sparse matrix
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>
// Define the structure for the node of the BST
struct Node {
int data;
struct Node *left;
struct Node *right;
};
// Function to create a new node
struct Node *createNode(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
// Function to insert a new node into the BST
struct Node *insertNode(struct Node *root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insertNode(root->left, value);
} else if (value > root->data) {
root->right = insertNode(root->right, value);
return root;
// Function to perform inorder traversal (left-root-right)
void inorderTraversal(struct Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
// Function to perform preorder traversal (root-left-right)
void preorderTraversal(struct Node *root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
// Function to perform postorder traversal (left-right-root)
void postorderTraversal(struct Node *root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
// Function to find the minimum value node in a BST
struct Node *minValueNode(struct Node *node) {
struct Node *current = node;
while (current && current->left != NULL) {
current = current->left;
return current;
// Function to delete a node from the BST
struct Node *deleteNode(struct Node *root, int value) {
if (root == NULL) {
return root;
if (value < root->data) {
root->left = deleteNode(root->left, value);
} else if (value > root->data) {
root->right = deleteNode(root->right, value);
} else {
if (root->left == NULL) {
struct Node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node *temp = root->left;
free(root);
return temp;
struct Node *temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
return root;
// Function to free memory allocated for the BST
void freeBST(struct Node *root) {
if (root != NULL) {
freeBST(root->left);
freeBST(root->right);
free(root);
int main() {
struct Node *root = NULL;
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
printf("Inorder traversal: ");
inorderTraversal(root);
printf("\n");
printf("Preorder traversal: ");
preorderTraversal(root);
printf("\n");
printf("Postorder traversal: ");
postorderTraversal(root);
printf("\n");
root = deleteNode(root, 20);
printf("Inorder traversal after deleting 20: ");
inorderTraversal(root);
printf("\n");
freeBST(root); // Free the allocated memory for BST
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
void selectionSort(int arr[], int n) {
// Implementation
// Bubble Sort
void bubbleSort(int arr[], int n) {
// Implementation
// Insertion Sort
void insertionSort(int arr[], int n) {
// Implementation
// Merge Sort
void merge(int arr[], int l, int m, int r) {
// Implementation
void mergeSort(int arr[], int l, int r) {
// Implementation
}
// Quick Sort
int partition(int arr[], int low, int high) {
// Implementation
void quickSort(int arr[], int low, int high) {
// Implementation
// Heap Sort
void heapify(int arr[], int n, int i) {
// Implementation
void heapSort(int arr[], int n) {
// Implementation
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
// Selection Sort
printf("Selection Sort:\n");
int selectionArr[n];
for (int i = 0; i < n; i++) {
selectionArr[i] = arr[i];
selectionSort(selectionArr, n);
for (int i = 0; i < n; i++) {
printf("%d ", selectionArr[i]);
printf("\n");
// Bubble Sort
printf("Bubble Sort:\n");
int bubbleArr[n];
for (int i = 0; i < n; i++) {
bubbleArr[i] = arr[i];
bubbleSort(bubbleArr, n);
for (int i = 0; i < n; i++) {
printf("%d ", bubbleArr[i]);
printf("\n");
// Insertion Sort
printf("Insertion Sort:\n");
int insertionArr[n];
for (int i = 0; i < n; i++) {
insertionArr[i] = arr[i];
insertionSort(insertionArr, n);
for (int i = 0; i < n; i++) {
printf("%d ", insertionArr[i]);
printf("\n");
// Merge Sort
printf("Merge Sort:\n");
int mergeArr[n];
for (int i = 0; i < n; i++) {
mergeArr[i] = arr[i];
mergeSort(mergeArr, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", mergeArr[i]);
printf("\n");
// Quick Sort
printf("Quick Sort:\n");
int quickArr[n];
for (int i = 0; i < n; i++) {
quickArr[i] = arr[i];
quickSort(quickArr, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", quickArr[i]);
printf("\n");
// Heap Sort
printf("Heap Sort:\n");
int heapArr[n];
for (int i = 0; i < n; i++) {
heapArr[i] = arr[i];
heapSort(heapArr, n);
for (int i = 0; i < n; i++) {
printf("%d ", heapArr[i]);
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>
#define TABLE_SIZE 10000 // Size of the hash table
// Structure to store elements in the hash table
struct Node {
int key;
int value;
struct Node *next;
};
// Function to create a new node
struct Node *createNode(int key, int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->key = key;
newNode->value = value;
newNode->next = NULL;
return newNode;
// Function to insert a key-value pair into the hash table
void insert(struct Node *hashTable[], int key, int value) {
int index = key % TABLE_SIZE;
if (hashTable[index] == NULL) {
hashTable[index] = createNode(key, value);
} else {
struct Node *temp = hashTable[index];
while (temp->next != NULL) {
temp = temp->next;
temp->next = createNode(key, value);
// Function to search for a pair with given total sale amount
void findPair(int sales[], int size, int target) {
struct Node *hashTable[TABLE_SIZE] = {NULL};
for (int i = 0; i < size; i++) {
int complement = target - sales[i];
int index = complement % TABLE_SIZE;
if (hashTable[index] != NULL) {
struct Node *temp = hashTable[index];
while (temp != NULL) {
if (temp->value == complement) {
printf("Pair found: %d + %d = %d\n", sales[i], complement, target);
return;
temp = temp->next;
insert(hashTable, sales[i], sales[i]);
printf("No pair found with the given total sale amount.\n");
int main() {
int sales[] = {10, 20, 30, 40, 50}; // Replace with your daily sale data
int size = sizeof(sales) / sizeof(sales[0]);
int totalSaleAmount = 70; // Change this to the desired total sale amount
findPair(sales, size, totalSaleAmount);
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>
// Structure for a Node in Binary Tree
struct Node {
int data;
struct Node *left;
struct Node *right;
};
// Function to create a new Node
struct Node *createNode(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
// Function to check if two binary trees are identical
bool areIdentical(struct Node *root1, struct Node *root2) {
if (root1 == NULL && root2 == NULL) {
return true;
}
if (root1 == NULL || root2 == NULL) {
return false;
return (root1->data == root2->data) &&
areIdentical(root1->left, root2->left) &&
areIdentical(root1->right, root2->right);
// Function to deallocate memory used by the tree
void freeTree(struct Node *root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
int main() {
struct Node *root1 = createNode(1);
root1->left = createNode(2);
root1->right = createNode(3);
root1->left->left = createNode(4);
root1->left->right = createNode(5);
struct Node *root2 = createNode(1);
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 {
printf("Binary Trees are not identical.\n");
// Deallocate memory used by the trees
freeTree(root1);
freeTree(root2);
return 0;
OUTPUT