Question: Hello, I created stacks in c coding language and added to these stacks the word that the user wants from the dictionary.txt file from

Hello, I created stacks in c coding language and added to these stacks the word that the user wants from the "dictionary.txt" file from the dictionary and added it to the stacks. In this dictionary file, I found words with the same length as the word entered by the user but with a different single letter and added them to the word the user wanted. for each found word, I created separate stacks with the word you want from the user and enqueue them to the queue. Here, I tried to write a program in C programming language that takes a start word and a target word from the user, creates stacks until it reaches this target word, adds them to queues, dequeues the stacks from the queue whether the target word is reached or not, and if the word at the top of the stacks is equal to the target word, pops the words from the stacks and prints each word in the stack. But the program I wrote does not work. Since I am new in this business, I could not find where I am doing wrong. thank you. Here's the following my code:
#include
#include
#include
#define MAX_WORD_LENGTH 50
#define MAX_DICTIONARY_SIZE 1000
// Stack yaps
typedef struct {
char words[MAX_DICTIONARY_SIZE][MAX_WORD_LENGTH];
int top;
} Stack;
// Queue yaps
typedef struct {
Stack stacks[MAX_DICTIONARY_SIZE];
int front, rear;
} Queue;
// Stack ilemleri
void initialStack(Stack *stack){
stack->top =-1;
}
int isStackEmpty(Stack *stack){
return (stack->top ==-1);
}
void push(Stack *stack, char *word){
if (stack->top < MAX_DICTIONARY_SIZE -1){
stack->top++;
strcpy(stack->words[stack->top], word);
} else {
printf("Stack overflow!
");
}
}
char* pop(Stack *stack){
if (!isStackEmpty(stack)){
return stack->words[stack->top--];
} else {
return NULL;
}
}
// Queue ilemleri
void initialQueue(Queue *queue){
queue->front = queue->rear =-1;
}
int isQueueEmpty(Queue *queue){
return (queue->front ==-1);
}
int isQueueFull(Queue *queue){
return ((queue->rear +1)% MAX_DICTIONARY_SIZE == queue->front);
}
void enqueue(Queue *queue, Stack stack){
if (!isQueueFull(queue)){
if (isQueueEmpty(queue)){
queue->front = queue->rear =0;
} else {
queue->rear =(queue->rear +1)% MAX_DICTIONARY_SIZE;
}
queue->stacks[queue->rear]= stack;
} else {
printf("Queue is full!
");
}
}
Stack dequeue(Queue *queue){
Stack stack;
if (!isQueueEmpty(queue)){
stack = queue->stacks[queue->front];
if (queue->front == queue->rear){
queue->front = queue->rear =-1;
} else {
queue->front =(queue->front +1)% MAX_DICTIONARY_SIZE;
}
return stack;
} else {
printf("Queue is empty!
");
initialStack(&stack); // Bo bir stack dndr
return stack;
}
}
// Szlk dosyasndan kelime okuma ilemi
void readDictionary(Stack *stack, char *filename, char *userWord){
FILE *file = fopen(filename,"r");
if (file == NULL){
printf("Dictionary file not found!
");
exit(1);
}
char word[MAX_WORD_LENGTH];
while (fscanf(file,"%s", word)!= EOF){
if (strlen(word)== strlen(userWord)){
int diff =0;
int i;
for ( i =0; i < strlen(word); i++){
if (word[i]!= userWord[i]){
diff++;
}
}
if (diff ==1){
push(stack, word);
}
}
}
fclose(file);
}
int main(){
char startWord[MAX_WORD_LENGTH], targetWord[MAX_WORD_LENGTH];
printf("Enter the start word: ");
scanf("%s", startWord);
printf("Enter the target word: ");
scanf("%s", targetWord);
Queue queue;
initialQueue(&queue);
Stack startStack;
initialStack(&startStack);
push(&startStack, startWord);
enqueue(&queue, startStack);
while (!isQueueEmpty(&queue)){
Stack currentStack = dequeue(&queue);
char *currentWord = pop(tStack);
if (strcmp(currentWord, targetWord)==0){
printf("Path found!
");
printf("Transformation path: ");
while (!isStackEmpty(tStack)){
char *word = pop(tStack);
printf("%s ->", word);
}
printf("%s
", startWord);
break;
}
readDictionary(tStack, "dictionary.txt", currentWord);
int i;
for (i =0; i <= currentStack.top; i++){
Stack newStack = currentStack;
enqueue(&queue, newStack);
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!