使用堆栈回文

发布于 2024-10-07 10:47:25 字数 666 浏览 1 评论 0原文

我们的教授要求我们使用堆栈来检查一个单词是否是回文。 每次运行它时,都会出现错误:未处理的异常。访问冲突 我做错了什么?我该如何改进我的代码?我的代码如下:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

薄凉少年不暖心 2024-10-14 10:47:25

您的 push 函数应该获取

  • 堆栈头的地址(您已经正确)和
  • 需要推入的字符(这需要修复)。

因此,方法签名变为:

void push(Stack**head, char value);

在函数主体中,您将 value 添加到堆栈顶部,如下所示:

temp->name = value;

另外,您必须始终检查 malloc 的返回值。

由于您要从函数 pop 返回弹出的值,因此它的返回类型不得为 void,因此请在声明和声明中将其更改为 char定义为:

char pop(Stack**head)

还有另一个逻辑错误:

首先将输入的所有字符推入堆栈。接下来你开始弹出字符。你的弹出没有终止条件。当您弹出所有字符(因此堆栈为空)时,下一次调用 pop 将导致崩溃,因为您将取消引用 NULL 指针( < code>*head 将为 NULL)。

要解决此问题,您只需弹出已推送的字符:

while(i<lenght && pop(&head)==word[i]){

由于 && 是短路的,因此一旦弹出所有字符,就不会调用 pop人物。

或者(也是首选方法)是编写另一个名为 isEmpty 的函数,当堆栈为空时返回 true/1 并在您之前使用此方法调用 pop 方法。

Your push function should take

  • the address of the stack head (you've it correct) and
  • the character that needs to be pushed in (this needs fixing).

So the method signature becomes:

void push(Stack**head, char value);

and in the body of the function you add value to the top of stack as:

temp->name = value;

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 be void, change it to char in both the declaration and the definition as:

char pop(Stack**head)

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 a NULL pointer ( *head will be NULL).

To fix this you pop only the characters you've pushed by doing:

while(i<lenght && pop(&head)==word[i]){

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 return true/1 when the stack is empty and use this method before you call the pop method.

メ斷腸人バ 2024-10-14 10:47:25

你应该检查你的代码是否丰富了堆栈末尾:

while(i < length && pop(&head)==word[i]){
       i++;
   }

you should check you rich the end of stack or not in your code:

while(i < length && pop(&head)==word[i]){
       i++;
   }
你的心境我的脸 2024-10-14 10:47:25

我认为你应该将 'void pop(Stack **head){' 更改为

char pop(Stack **head) {

并防止空堆栈:

char pop(Stack**head){
Stack* temp;
char val;
temp = *head;
if (!temp) return 0;
val = temp->name;
*head = (*head)->next;
free(temp);
return val;
}

I think you should change 'void pop(Stack **head){' into

char pop(Stack **head) {

and also guard against empty stack :

char pop(Stack**head){
Stack* temp;
char val;
temp = *head;
if (!temp) return 0;
val = temp->name;
*head = (*head)->next;
free(temp);
return val;
}
往日 2024-10-14 10:47:25

您还可以考虑使用“递归”,这在某种程度上类似于构造堆栈,只是它是为您的方法调用隐式完成的。
回文问题是学习递归威力的经典练习:)

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 :)

那请放手 2024-10-14 10:47:25

这是您调用它的函数:

push(&head, i, word[i]);

这是声明和定义的函数:

void push(Stack**head, int i, char value[i]);

因此声明中的 arg 3 是一个字符数组,而调用部分中的 arg 3 是一个字符。更改您的 push() 以使用字符作为 value 并省略 i

void push(Stack**head, char value){
    Stack *temp = (Stack*)malloc(sizeof(Stack));
    temp->name = value;
    temp->next = *head;
    *head = temp; 
}

现在用以下方式调用它:

push(&head, word[i]);

This is the function as you are calling it:

push(&head, i, word[i]);

This is the function as declared and defined:

void push(Stack**head, int i, char value[i]);

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 for value and just omit i:

void push(Stack**head, char value){
    Stack *temp = (Stack*)malloc(sizeof(Stack));
    temp->name = value;
    temp->next = *head;
    *head = temp; 
}

Now call it with:

push(&head, word[i]);
柏拉图鍀咏恒 2024-10-14 10:47:25

您的代码在 while-pop 部分出现问题。

为了您的方便,我已为您附上修改后的工作代码:

typedef struct stack{
    char name;
    struct stack * next;
}Stack;

void push(Stack**head, char value);
char pop(Stack**head);



int main (int argc, const char * argv[]) {


    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++;
    //}

    int counter=0;
    i=0;
    for (counter=0; counter<lenght; counter++) {
    if (pop(&head) == word[counter])
    {
        i++;
    }
    }


    if(i==lenght) printf("The word is a palindrome");
    else printf("The word is not a palindrome");


    return 0;
}

void push(Stack**head,char value){

    Stack *temp = (Stack*)malloc(sizeof(Stack));
    temp->name = value;
    temp->next = *head;
    *head = temp; 
}

char pop(Stack**head){

    Stack* temp;

    char val;
    temp = *head;
    val = temp->name;
    *head = (*head)->next;

    free(temp);
    return val;
}

your code got problem on while-pop portion.

For your convinience, I have attached the modified working code for you:

typedef struct stack{
    char name;
    struct stack * next;
}Stack;

void push(Stack**head, char value);
char pop(Stack**head);



int main (int argc, const char * argv[]) {


    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++;
    //}

    int counter=0;
    i=0;
    for (counter=0; counter<lenght; counter++) {
    if (pop(&head) == word[counter])
    {
        i++;
    }
    }


    if(i==lenght) printf("The word is a palindrome");
    else printf("The word is not a palindrome");


    return 0;
}

void push(Stack**head,char value){

    Stack *temp = (Stack*)malloc(sizeof(Stack));
    temp->name = value;
    temp->next = *head;
    *head = temp; 
}

char pop(Stack**head){

    Stack* temp;

    char val;
    temp = *head;
    val = temp->name;
    *head = (*head)->next;

    free(temp);
    return val;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文