使用堆栈回文
我们的教授要求我们使用堆栈来检查一个单词是否是回文。 每次运行它时,都会出现错误:未处理的异常。访问冲突
我做错了什么?我该如何改进我的代码?我的代码如下:
typedef struct stack{
char name;
struct stack * next;
}Stack;
void push(Stack**head, char value);
char pop(Stack**head);
int main(){
char word[11];
int i=0;
int lenght = 0;
Stack*head = NULL;
printf("Please type the word: ");
scanf("%s", word);
lenght = strlen(word);
while(word[i]!='\0'){
push(&head, word[i]);
i++;
}
i = 0;
while(pop(&head)==word[i]){
i++;
}
if(i==lenght) printf("The word is a palindrome");
else printf("The word is not a palindrome");
}
Our professor required us to check if a word is a palindrome by using stacks.
Every time I run it, there's an error: Unhandled Exception. Access violation
What am I doing wrong? How can i improve my code? My code is as follows:
typedef struct stack{
char name;
struct stack * next;
}Stack;
void push(Stack**head, char value);
char pop(Stack**head);
int main(){
char word[11];
int i=0;
int lenght = 0;
Stack*head = NULL;
printf("Please type the word: ");
scanf("%s", word);
lenght = strlen(word);
while(word[i]!='\0'){
push(&head, word[i]);
i++;
}
i = 0;
while(pop(&head)==word[i]){
i++;
}
if(i==lenght) printf("The word is a palindrome");
else printf("The word is not a palindrome");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的
push
函数应该获取因此,方法签名变为:
在函数主体中,您将
value
添加到堆栈顶部,如下所示:另外,您必须始终检查
malloc
的返回值。由于您要从函数
pop
返回弹出的值,因此它的返回类型不得为void
,因此请在声明和声明中将其更改为char
定义为:还有另一个逻辑错误:
首先将输入的所有字符推入堆栈。接下来你开始弹出字符。你的弹出没有终止条件。当您弹出所有字符(因此堆栈为空)时,下一次调用
pop
将导致崩溃,因为您将取消引用NULL
指针( < code>*head 将为NULL
)。要解决此问题,您只需弹出已推送的字符:
由于
&&
是短路的,因此一旦弹出所有字符,就不会调用pop
人物。或者(也是首选方法)是编写另一个名为
isEmpty
的函数,当堆栈为空时返回true
/1
并在您之前使用此方法调用pop
方法。Your
push
function should takeSo the method signature becomes:
and in the body of the function you add
value
to the top of stack as:Also you must always check the return value of
malloc
.Since you are returning the popped value from the function
pop
it's return type must not bevoid
, change it tochar
in both the declaration and the definition as:There is another logical error:
To start with you push all the characters of the input into the stack. Next you start popping the characters. There is no terminating condition for your popping. When you've popped all the characters (so your stack is empty) the next call to
pop
will lead to a crash as you'll be dereferencing aNULL
pointer (*head
will beNULL
).To fix this you pop only the characters you've pushed by doing:
Since the
&&
is short circuited,pop
will not be called once you've popped all the characters.Alternatively (and preferred approach) is to write another function called
isEmpty
which returntrue
/1
when the stack is empty and use this method before you call thepop
method.你应该检查你的代码是否丰富了堆栈末尾:
you should check you rich the end of stack or not in your code:
我认为你应该将 'void pop(Stack **head){' 更改为
并防止空堆栈:
I think you should change 'void pop(Stack **head){' into
and also guard against empty stack :
您还可以考虑使用“递归”,这在某种程度上类似于构造堆栈,只是它是为您的方法调用隐式完成的。
回文问题是学习递归威力的经典练习:)
You might also consider using "recursion" which is somehow similar to constructing a stack, just that it is done for your method calls implicitly.
The palindrome problem is a classical exercise for learning the power of recursion :)
这是您调用它的函数:
这是声明和定义的函数:
因此声明中的 arg 3 是一个字符数组,而调用部分中的 arg 3 是一个字符。更改您的
push()
以使用字符作为value
并省略i
:现在用以下方式调用它:
This is the function as you are calling it:
This is the function as declared and defined:
So arg 3 in the declaration is a character array, whereas arg 3 in the calling portion is a character. Change your
push()
to use a character forvalue
and just omiti
:Now call it with:
您的代码在
while-pop
部分出现问题。为了您的方便,我已为您附上修改后的工作代码:
your code got problem on
while-pop
portion.For your convinience, I have attached the modified working code for you: