I have to implement a dictionary in c language without using pointers and with just help of the array. It has 2 functionalities of adding words and deleting words. It also has two arrays, one having the string of the elements of the dictionary and one(next[]) for saving the index of the next element. After adding words, the words need to be ordered according to their english name which works fine. My problem is in the second case which an element needs to be deleted. The functionality that we are looking for is to delete the element by swapping its position with the last element, deleting the last element and find the right position of the element which has been swapped with the deleted one. My codes works fine when the element is in the middle of the dictionary but when the element needs to be deleted is the first element, it deletes all the other elements except the first one(we expect it to do the same steps and also change the renew the starting index accordingly) and for the last element, it goes on a loop instead of just deleting that element. Can you help me fix this problem?
This is the code:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 10
#define WORD_SIZE 20
struct dictionary {
char english[WORD_SIZE];
char turkish[WORD_SIZE];
};
int main() {
struct dictionary dict[MAX_SIZE];
int next[MAX_SIZE];
int size = 0;
int start = -1;
char input[WORD_SIZE];
int choice = 0;
// Initialize next array
for (int i = 0; i < MAX_SIZE; i++) {
next[i] = i + 1;
}
next[MAX_SIZE - 1] = -1;
// Menu loop
while (choice != 4) {
printf("\n\nDictionary Menu:\n");
printf("1. Add a word\n");
printf("2. Delete a word\n");
printf("3. View the dictionary\n");
printf("4. Quit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (size >= MAX_SIZE) {
printf("The dictionary is full.\n");
} else {
// Add a new word
printf("Enter the English word: ");
scanf("%s", input);
// Convert to lowercase
for (int i = 0; i < strlen(input); i++) {
input[i] = tolower(input[i]);
}
strcpy(dict[size].english, input);
printf("Enter the Turkish word: ");
scanf("%s", input);
strcpy(dict[size].turkish, input);
// Find the correct position for the new word
int curr = start;
int prev = -1;
while (curr != -1 && strcmp(dict[curr].english, dict[size].english) < 0) {
prev = curr;
curr = next[curr];
}
// Insert the new word at the correct position
if (prev == -1) {
next[size] = start;
start = size;
} else {
next[prev] = size;
next[size] = curr;
}
size++;
printf("Word added successfully.\n");
}
break;
case 2:
if (size == 0) {
printf("The dictionary is empty.\n");
} else {
// Delete a word
printf("Enter the English word to delete: ");
scanf("%s", input);
// Convert to lowercase
for (int i = 0; i < strlen(input); i++) {
input[i] = tolower(input[i]);
}
int curr = start;
int prev = -1;
int delete_index = -1;
// Find the index of the word to delete
while (curr != -1) {
if (strcmp(dict[curr].english, input) == 0) {
delete_index = curr;
break;
}
prev = curr;
curr = next[curr];
}
if (delete_index == -1) {
printf("The word '%s' is not in the dictionary.\n", input);
} else {
// Replace the deleted word with the last word in the dictionary
size--;
if (delete_index == size) {
if (prev == -1) {
start = -1;
} else {
next[prev] = -1;
}
} else {
strcpy(dict[delete_index].english, dict[size].english);
strcpy(dict[delete_index].turkish, dict[size].turkish);
next[delete_index] = next[size];
if (prev == -1) {
start = delete_index;
} else {
next[prev] = delete_index;
}
}
printf("The word '%s' has been deleted from the dictionary.\n", input);
}
}
break;
case 3:
if (size == 0) {
printf("The dictionary is empty.\n");
} else {
// Print all the words in the dictionary
printf("English\tTurkish\n");
printf("-----------------\n");
int curr = start;
while (curr != -1) {
printf("%s\t%s\n", dict[curr].english, dict[curr].turkish);
curr = next[curr];
}
}
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}