1
Dhaka International University
Project On
“Data Structure Operations & Inserting a Word into a Digital
Dictionary”
Course Title: Data Structures Lab
Course No: 0613-104
Submitted to
Prof. Ms. Jahanara Akhtar
Department of CSE
Dhaka International University
Submitted By
Masudur Rahman
Semester: 2nd
Batch: D-86, Roll: 33
Registration No:
CS-D-86-22-123494
Department of CSE
Dhaka International University
Date of Submission: 3rd June, 2024
2
Project D-86
Problem 1: Implement data structure operations (insert(),
delete(), BinarySearch(), LinearSearch(), Bubble Sort())
using switch statement. Given list will be string type.
#include <stdio.h>
#include <string.h>
#define MAX 100
// Function Prototypes
void insert(char arr[][MAX], int *n, char word[]);
void delete(char arr[][MAX], int *n, char word[]);
int binarySearch(char arr[][MAX], int n, char word[]);
int linearSearch(char arr[][MAX], int n, char word[]);
void bubbleSort(char arr[][MAX], int n);
void switchCase(char operation[], char arr[][MAX], int *n, char
word[]);
int main() {
char arr[MAX][MAX]; // Array to hold list of words
int n = 0; // Number of words in the list
char operation[MAX];
char word[MAX];
3
while (1) {
printf("Enter operation (insert, delete, binary_search,
linear_search, bubble_sort, exit): ");
scanf("%s", operation);
if (strcmp(operation, "exit") == 0) {
break;
if (strcmp(operation, "bubble_sort") != 0) {
printf("Enter word: ");
scanf("%s", word);
switchCase(operation, arr, &n, word);
return 0;
// Insert a word into the list
void insert(char arr[][MAX], int *n, char word[]) {
strcpy(arr[*n], word);
(*n)++;
printf("Inserted %s\n", word);
}
4
// Delete a word from the list
void delete(char arr[][MAX], int *n, char word[]) {
int i, j;
for (i = 0; i < *n; i++) {
if (strcmp(arr[i], word) == 0) {
for (j = i; j < *n - 1; j++) {
strcpy(arr[j], arr[j + 1]);
(*n)--;
printf("Deleted %s\n", word);
return;
printf("%s not found\n", word);
// Binary search in a sorted list
int binarySearch(char arr[][MAX], int n, char word[]) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int res = strcmp(arr[mid], word);
if (res == 0)
return mid;
if (res < 0)
left = mid + 1;
5
else
right = mid - 1;
return -1;
// Linear search in the list
int linearSearch(char arr[][MAX], int n, char word[]) {
for (int i = 0; i < n; i++) {
if (strcmp(arr[i], word) == 0) {
return i;
return -1;
// Bubble sort the list
void bubbleSort(char arr[][MAX], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(arr[j], arr[j + 1]) > 0) {
char temp[MAX];
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
6
printf("List sorted\n");
// Perform operations based on the given operation name
void switchCase(char operation[], char arr[][MAX], int *n, char
word[]) {
printf("Operation: %s\n", operation);
if (strcmp(operation, "insert") == 0) {
insert(arr, n, word);
} else if (strcmp(operation, "delete") == 0) {
delete(arr, n, word);
} else if (strcmp(operation, "binary_search") == 0) {
bubbleSort(arr, *n); // Ensure the list is sorted before
binary search
int result = binarySearch(arr, *n, word);
if (result != -1)
printf("%s found at index %d\n", word, result);
else
printf("%s not found\n", word);
} else if (strcmp(operation, "linear_search") == 0) {
int result = linearSearch(arr, *n, word);
if (result != -1)
printf("%s found at index %d\n", word, result);
else
printf("%s not found\n", word);
} else if (strcmp(operation, "bubble_sort") == 0) {
7
bubbleSort(arr, *n);
} else {
printf("Invalid operation\n");
// Print the current state of the list
printf("Current List: ");
for (int i = 0; i < *n; i++) {
printf("%s ", arr[i]);
printf("\n");
}
8
Output:
9
Problem 2: Suppose some words are stored in a digital
dictionary. Insert a new word into appropriate location.
Location is not given by the user. Use linear search to find
appropriate location.
#include <stdio.h>
#include <string.h>
#define MAX 100
// Function Prototypes
void insertIntoSortedList(char wordList[][MAX], int *n, char word[]);
int main() {
char wordList[MAX][MAX] = {"apple", "banana", "cherry"}; // Pre-
sorted list of words
int n = 3; // Number of words in the list
char newWord[MAX];
printf("Enter a new word to insert into the sorted list: ");
scanf("%s", newWord);
printf("Before insertion:\n");
for (int i = 0; i < n; i++) {
printf("%s ", wordList[i]);
}
10
printf("\n");
insertIntoSortedList(wordList, &n, newWord);
printf("After insertion of %s:\n", newWord);
for (int i = 0; i < n; i++) {
printf("%s ", wordList[i]);
printf("\n");
return 0;
// Insert a new word into the appropriate position in a sorted list
void insertIntoSortedList(char wordList[][MAX], int *n, char word[]) {
int i;
for (i = *n - 1; (i >= 0 && strcmp(wordList[i], word) > 0); i--) {
strcpy(wordList[i + 1], wordList[i]);
strcpy(wordList[i + 1], word);
(*n)++;
printf("Inserted %s\n", word);
}
11
Output: