【问题标题】:Palindrome Using a stack使用堆栈的回文
【发布时间】:2011-05-24 12:02:48
【问题描述】:

我们的教授要求我们使用堆栈来检查一个单词是否是回文。 每次运行都会报错:Unhandled Exception. Access violation我做错了什么?我怎样才能改进我的代码?我的代码如下:

 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");
}

【问题讨论】:

  • 第一个:在函数签名中使用value而不是value[i]。

标签: c data-structures palindrome stack


【解决方案1】:

您的push 函数应该采用

  • 堆栈头的地址(你说得对)和
  • 需要推入的角色(这需要修复)。

所以方法签名变成:

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

并在函数体中将value 添加到堆栈顶部:

temp->name = value;

您还必须始终检查malloc 的返回值。

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

char pop(Stack**head)

还有一个逻辑错误:

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

要解决此问题,您只需弹出您通过以下操作推送的字符:

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

由于&amp;&amp; 被短路,所以在弹出所有字符后不会调用pop。

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

【讨论】:

  • 我的第二个错误是不应忽略的无效值
  • @newbie:很高兴我能帮上忙。确保您了解所做的更改。
【解决方案2】:

这是你调用的函数:

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]);

【讨论】:

  • 我的第二个错误是不应忽略的无效值
  • @newbie pop() 被声明为 void 函数,但返回的是 val。
【解决方案3】:

我认为你应该把 '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;
}

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        您的代码在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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多