简体   繁体   English

使用堆栈检查字符串是否为回文结构

[英]Checking if a string is a palindrome using a stack

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

typedef struct node
{
    char data;
    struct node *link;
}StackNode;

void insertData(StackNode **);
void push(StackNode **, char);
void checkData(StackNode **);
bool pop(StackNode **,char *);

char sent[20] = "";

void main()
{
   StackNode *stackTop;
   insertData(&stackTop);
   checkData(&stackTop);
   printf("\n");
   return;
}

void insertData(StackNode **stackTop)
{
    char c;
    int len;

    printf("Enter the Sentence\n");
    while( ( ( c = getchar() ) != '\n'))
    {   
        if( ( ( c>='a' &&c<='z') || (c>='A' && c<='Z')))
        {
            if((c>='A' && c<='Z'))
            {
                int rem;
                rem = c-'A';
                c='a' + rem;
            }
            push(stackTop,c);
            len = strlen(sent);
            sent[len++]=c;
            sent[len]='\0';
        }
    }
    printf("Letters are %s\n\n",sent);
}

void push(StackNode **stackTop,char c)
{
    StackNode *pNew;
    pNew = (StackNode*) malloc(sizeof(StackNode));
    if(!pNew)
    {
        printf("Error 100:Out of memory\n");
        exit(100);
    }
    pNew->data = c;
    pNew->link = *stackTop;
    *stackTop = pNew;
}

void checkData(StackNode **stackTop)
{
    char c;
    int i=0;
    while(pop(stackTop,&c))
    {
        if( c !=sent[i++])
        {
            printf("Not palindrome");
            return;
        }
    }
    printf("Palindrome");
}

bool pop(StackNode **stackTop,char *c)
{
    StackNode *pNew;
    pNew = *stackTop;
    if(pNew == NULL)
        return false;
    *c = pNew->data;
    *stackTop = pNew->link;
    printf("char poped %c\n",*c);
    free(pNew);
    return true;
}

I am able to pop each letter, after that the exe file is not working, I think I'm unable to check if the letters are same in reverse order. 我能够弹出每个字母,之后exe文件不起作用,我想我无法检查字母是否以相反的顺序相同。 please help 请帮忙

I've been working all day to write this program,but I'm stuck at this point. 我整天都在努力写这个程序,但是我已经陷入困境了。

Are you allowed to use the implicit call stack? 你被允许使用隐式调用堆栈吗?

int is_pal(const char *begin, const char *end)
{
    if (*begin != *end)
        return 0;

    if ((end - begin) < 2)
        return *begin == *end;

    return is_pal(begin + 1, end - 1);
}

When programming in C, you should always initialize pointers to NULL . 在C中编程时,应始终将指针初始化为NULL Because you didn't initialize the StackNode *stackTop (in main) it is pointing to garbage and is not null. 因为您没有初始化StackNode *stackTop (在main中),它指向garbage并且不为null。 So when you are checking for a palindrome, your pop method just keeps going off the end of the stack and never returns false because it never hits a null. 因此,当你检查回文时,你的pop方法只是继续离开堆栈的末尾并且永远不会返回false,因为它永远不会返回null。 StackNode *stackTop = NULL; should fix your problem. 应该解决你的问题。

I am not sure why you want to do this, but this is one way 我不确定你为什么要这样做,但这是一种方式

put everything on the stack 把所有东西放在堆栈上

and then pop everything comparing with the elements of the string from the start. 然后从头开始将所有内容与字符串元素进行比较。 something like this 这样的事情

for(int i = 0; i < strlen(s); i++) push(s[i]); for(int i = 0; i <strlen(s); i ++)push(s [i]);

for(int i = 0; i < strlen(s); i++) if (s[i] == pop()) continue; for(int i = 0; i <strlen(s); i ++)if(s [i] == pop())continue;

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

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