c - 将单词插入堆栈并检查其是否存在回文

标签 c while-loop stack palindrome getchar

我们的老师给我们布置了作业,使用数据结构“Stack”检查单词的回文。

下面是我为以下问题编写的代码:-

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>

struct Stack
{
    int top;
    int capacity;
    char *array;
};

void push(struct Stack stack, char a) //Push function.
{
    stack.array[++stack.top] = a; //Helps to push charater to a stack.
}

char pop(struct Stack stack) //Pop function.
{
    return stack.array[stack.top--]; //Helps to pop character from a stack.
}

int main(void)
{
    struct Stack original; //Original stack where the "Original" word will be pushed.
    original.top = -1;
    original.capacity = 10;
    original.array = calloc(original.capacity, sizeof(char));

    struct Stack checker; //Another stack that "Checks" whether the word is palindrome or not.
    checker.top = -1;
    checker.capacity = 10;
    checker.array = calloc(checker.capacity, sizeof(char));

    while(getchar()!='\0') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
    {
        push(original, getchar());
    }

    while(original.top != -1)
    {
        push(checker,pop(original)); //Popping from "Original" stack and pushing it to "Checker" stack.
    }

    while(checker.top != -1)
    {
        original.top = checker.top;
        if(original.array[original.top] != checker.array[checker.top]) //Checking every character in the stack if it is excatly same or not.
        {
            printf("It is not a palindrome.\n");
            return EXIT_SUCCESS;
        }
        else
        {
            checker.top = checker.top - 1;
        }
    }

    if(checker.top == -1)
    {
        printf("It is a palindrome.\n");
    }

    return 0;
}

无论我在以下行中遇到什么问题:-

while(getchar()!='\0') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
    push(original, getchar());
}

以下循环无限运行。我添加以下行的目的是我想从 stdin buffer 添加单个字符并将其pushoriginal 堆栈中,直到遇到 '\0'.

我做错了什么?这样做违法吗?

Addendum: -

Sample Input 1: - civic

Expected Output: - It is a palindrome.

Sample Input 2: - madama

Expected Output: - It is not a palindrome.

附言

以下代码:-

while(getchar()!='\0') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
    push(original, getchar());
}

现在已替换为:-

int c;
int i = 0;

while ( i < original.capacity && ( c = getchar() ) != EOF && c != '\n' )
{
    push(original, c );
    ++i;
}

现在工作完美,无论如何,对于每个单词,我的代码都给出了输出:-

It is a palindrome.

我在哪里错误地应用了堆栈的概念?

最佳答案

这个循环

while(getchar()!='\0') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
    push(original, getchar());
}

在任何情况下都是错误的,因为它读取字符两次:在循环条件和循环体内。

并且您必须使用例如键盘明确输入 0。

你需要的是以下内容

int c;
int i = 0;

while ( i < original.capacity && ( c = getchar() ) != EOF && c != '\n' )
{
    push(original, c );
    ++i;
} 

还有一个问题。这些函数处理传递参数的副本。

void push(struct Stack stack, char a) //Push function.
{
    stack.array[++stack.top] = a; //Helps to push charater to a stack.
}

char pop(struct Stack stack) //Pop function.
{
    return stack.array[stack.top--]; //Helps to pop character from a stack.
}

你必须像这样声明它们

void push(struct Stack *stack, char a) //Push function.
{
    stack-?array[++stack->top] = a; //Helps to push charater to a stack.
}

char pop(struct Stack *stack) //Pop function.
{
    return stack->array[stack->top--]; //Helps to pop character from a stack.
}

即通过指针按引用传递原始堆栈。

并调用这些函数作为例子

push( &original, c );

否则数据成员top不会改变。

这是你更新的程序

# include <stdio.h>
# include <stdlib.h>

struct Stack
{
    int top;
    int capacity;
    char *array;
};

void push(struct Stack *stack, char a) //Push function.
{
    stack->array[++stack->top] = a; //Helps to push charater to a stack.
}

char pop(struct Stack *stack) //Pop function.
{
    return stack->array[stack->top--]; //Helps to pop character from a stack.
}

int main(void)
{
    struct Stack original; //Original stack where the "Original" word will be pushed.
    original.top = -1;
    original.capacity = 10;
    original.array = calloc(original.capacity, sizeof(char));

    struct Stack checker; //Another stack that "Checks" whether the word is palindrome or not.
    checker.top = -1;
    checker.capacity = 10;
    checker.array = calloc(checker.capacity, sizeof(char));

    int c;
    int i = 0;

    while ( i < original.capacity && ( c = getchar() ) != EOF && c != '\n' )
    {
        push( &original, c );
        ++i;
    }

    while(original.top != -1)
    {
        push(&checker,pop(&original)); //Popping from "Original" stack and pushing it to "Checker" stack.
    }

    while(checker.top != -1)
    {
        original.top = checker.top;
        if(original.array[original.top] != checker.array[checker.top]) //Checking every character in the stack if it is excatly same or not.
        {
            printf("It is not a palindrome.\n");
            return EXIT_SUCCESS;
        }
        else
        {
            checker.top = checker.top - 1;
        }
    }

    if(checker.top == -1)
    {
        printf("It is a palindrome.\n");
    }

    return 0;
}

考虑到这些 header

#include <string.h>
#include <stdbool.h>

是多余的。

关于c - 将单词插入堆栈并检查其是否存在回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137540/

相关文章:

存储来自用户的未知数量、未知大小的字符串的 C 程序 - 00558068 处的堆 block 在 00558096 处修改,过去请求的大小为 26

c - 在对其他数组进行类型转换后获取结构中的第一个字符数组

python - 如何使用函数的返回值作为 while 的条件,在 python 中返回元组

java - 以下 Java 库代码如何退出 while(true) 循环?

java - 如何使用 while 循环填充 List 而无需每次都声明一个新对象?

java - 为什么我的堆栈代码中的 pop 方法不执行?

c - 在函数签名中将枚举声明为返回状态

c - 释放一个等于 malloc 指针的指针

java - 节点链表的堆栈

python - 如何获取 Python 解释器堆栈的当前深度?