将堆栈插入队列

问题描述 投票:0回答:1

我用 c 编码语言创建了堆栈,并将用户想要从“dictionary.txt”中获取的单词添加到这些堆栈中 ” 从字典中提取文件并将其添加到堆栈中。在这个字典文件中,我找到了与用户输入的单词长度相同但单个字母不同的单词,并将它们添加到用户想要的单词中。对于每个找到的单词,我使用您想要从用户那里获得的单词创建单独的堆栈,并将它们放入队列中。在这里,我尝试用 C 编程语言编写一个程序,该程序从用户处获取起始词和目标词,创建堆栈直到到达该目标词,将它们添加到队列中,将堆栈从队列中出列,无论目标词是是否达到,如果堆栈顶部的单词等于目标单词,则从堆栈中弹出单词并打印堆栈中的每个单词。但我写的程序不能运行。由于我是这个行业的新手,我找不到我做错的地方。谢谢。这是我的代码:`

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#define MAX_WORD_LENGTH 50
#define MAX_DICTIONARY_SIZE 1000

// Stack yapısı
typedef struct {
    char words[MAX_DICTIONARY_SIZE][MAX_WORD_LENGTH];
    int top;
} Stack;

// Queue yapısı
typedef struct {
    Stack stacks[MAX_DICTIONARY_SIZE];
    int front, rear;
} Queue;

// Stack işlemleri
void initStack(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!\n");
    }
}

char* pop(Stack *stack) {
    if (!isStackEmpty(stack)) {
        return stack->words[stack->top--];
    } else {
        return NULL;
    }
}

// Queue işlemleri
void initQueue(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!\n");
    }
}

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!\n");
        initStack(&stack); // Boş bir stack döndür
        return stack;
    }
}

// Sözlük dosyasından kelime okuma işlemi
void readDictionary(Stack *stack, char *filename, char *userWord) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Dictionary file not found!\n");
        exit(1);
    }

    char word[MAX_WORD_LENGTH];
    while (fscanf(file, "%s", word) != EOF) {
        if (strlen(word) == strlen(userWord)) {
            int diff = 0;
            for (int 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;
    initQueue(&queue);

    Stack startStack;
    initStack(&startStack);
    push(&startStack, startWord);
    enqueue(&queue, startStack);

    while (!isQueueEmpty(&queue)) {
        Stack currentStack = dequeue(&queue);
        char *currentWord = pop(&currentStack);

        if (strcmp(currentWord, targetWord) == 0) {
            printf("Path found!\n");
            printf("Transformation path: ");
            while (!isStackEmpty(&currentStack)) {
                char *word = pop(&currentStack);
                printf("%s -> ", word);
            }
            printf("%s\n", startWord);
            break;
        }

        readDictionary(&currentStack, "dictionary.txt", currentWord);

        for (int i = 0; i <= currentStack.top; i++) {
            Stack newStack = currentStack;
            enqueue(&queue, newStack);
        }
    }

    return 0;
}

我想确保字典阅读逻辑正确识别彼此相差一个字母的单词,并正确使用它们来创建新路径。

c data-structures queue stack
1个回答
0
投票

除了Gerhardh提到的

queue
的错误存储类别之外,程序中的主要错误是
currentStack
(要收集Transformation路径的地方)也被用在
readDictionary
中,从而被覆盖。另一个错误是
currentWord
已从
currentStack
中删除,尽管转换可能需要该词。最后一个是相同的
newStack
被重复排队,而不是多次从字典中添加单词。因此,循环的核心可以更改为:

        if (strcmp(currentWord, targetWord) == 0) {
            printf("Path found!\n");
            printf("Transformation path: ");
            printf("%s", currentWord);
            while (!isStackEmpty(&currentStack)) {
                char *word = pop(&currentStack);
                printf(" <- %s", word);
            }
            printf("\n");
            break;
        }

        push(&currentStack, currentWord);   // we still need the word
        // and we still need the stack - don't abuse it for the dictionary
        readDictionary(&startStack, "dictionary.txt", currentWord);
        while (currentWord = pop(&startStack))
        {
            Stack newStack = currentStack;
            push(&newStack, currentWord);   // only one new word on each stack
            enqueue(&queue, newStack);
        }
© www.soinside.com 2019 - 2024. All rights reserved.