简体   繁体   English

将单词压入堆栈并检查回文

[英]Pushing a word to a stack and checking it for Palindrome

Our teacher gave us homework to check for palindrome of a word using data structure "Stack".我们的老师给了我们作业,使用数据结构“堆栈”检查单词的回文。

Below is the code which I have written for the following problem: -以下是我为以下问题编写的代码: -

# 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;
}

Howsoever I am getting problem in the following line: -但是,我在以下行中遇到问题:-

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

The following loop is running infinitely.以下循环无限运行。 My purpose of adding the following line is that I want to add individual characters from stdin buffer and push it in the original stack until it encounters '\0' .我添加以下行的目的是我想从stdin buffer添加单个字符并将其push送到original堆栈中,直到它遇到'\0'

What have I done wrong here?我在这里做错了什么? Is it illegal to do it this way?这样做是否违法?

Addendum: -附录: -

Sample Input 1: - civic示例输入 1: -公民

Expected Output: - It is a palindrome.预期的 Output: -这是一个回文。

Sample Input 2: - madama示例输入 2:- madama

Expected Output: - It is not a palindrome.预期的 Output: -这不是回文。

PS附言

The following code: -以下代码: -

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

has now been replaced with: -现在已替换为:-

int c;
int i = 0;

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

And is now working perfectly, howsoever now, for every word, my code is giving the output: -现在工作正常,但是现在,对于每个单词,我的代码都给出了 output:-

It is a palindrome.这是一个回文。

Where have I applied the concept of stack incorrectly?我在哪里错误地应用了堆栈的概念?

This loop这个循环

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

is in any case wrong because it reads characters twice: in the condition of the loop and within the body of the loop.在任何情况下都是错误的,因为它两次读取字符:在循环的条件下和在循环的主体内。

And you have explicitly to enter 0 using for example keypad.并且您必须使用例如键盘明确输入 0。

What you need is the following您需要的是以下内容

int c;
int i = 0;

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

Also there is one more problem.还有一个问题。 These functions deal with a copy of the passed arguments.这些函数处理传递的 arguments 的副本。

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.
}

You have to declare them like你必须像这样声明它们

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.
}

That is to pass the original stack by reference through pointer.即通过指针通过引用传递原始堆栈。

And call these functions as for example并调用这些函数,例如

push( &original, c );

Otherwise the data member top will not be changed.否则数据成员top不会改变。

Here is your updated program这是您更新的程序

# 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;
}

Take into account that these headers考虑到这些标题

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

are redundant.是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM