将单词推入堆栈并检查其是否存在回文
Pushing a word to a stack and checking it for Palindrome
我们老师给我们布置的作业是使用数据结构检查单词的回文 "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()!='[=10=]') //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()!='[=11=]') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
push(original, getchar());
}
下面的循环是运行无限循环。我添加以下行的目的是我想在 original
堆栈中添加 stdin buffer
和 push
中的单个字符,直到遇到 '[=17=]'
.
我做错了什么?这样做违法吗?
Addendum: -
Sample Input 1: -
civic
Expected Output: -
It is a palindrome.
Sample Input 2: -
madama
Expected Output: -
It is not a palindrome.
P.S.
以下代码:-
while(getchar()!='[=11=]') //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()!='[=10=]') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
push(original, getchar());
}
在任何情况下都是错误的,因为它读取字符两次:在循环的条件下和在循环的 body 内。
并且您已经明确地使用例如键盘输入 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;
}
考虑到这些 headers
#include <string.h>
#include <stdbool.h>
多余。
我们老师给我们布置的作业是使用数据结构检查单词的回文 "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()!='[=10=]') //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()!='[=11=]') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
push(original, getchar());
}
下面的循环是运行无限循环。我添加以下行的目的是我想在 original
堆栈中添加 stdin buffer
和 push
中的单个字符,直到遇到 '[=17=]'
.
我做错了什么?这样做违法吗?
Addendum: -
Sample Input 1: - civic
Expected Output: - It is a palindrome.
Sample Input 2: - madama
Expected Output: - It is not a palindrome.
P.S.
以下代码:-
while(getchar()!='[=11=]') //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()!='[=10=]') //Getting all the characters from the stdin buffer and pushing it into "Original" stack.
{
push(original, getchar());
}
在任何情况下都是错误的,因为它读取字符两次:在循环的条件下和在循环的 body 内。
并且您已经明确地使用例如键盘输入 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;
}
考虑到这些 headers
#include <string.h>
#include <stdbool.h>
多余。