求助数据结构大神,题目是编写一个算法判断指定的字符向量是否为回文,我编写一个程序一直判断为不是回文

求助数据结构大神,题目是编写一个算法判断指定的字符向量是否为回文,我编写一个程序一直判断为不是回文有大佬吗,能不能帮看一下这个程序有什么问题,即使输入回文字符,输出结果都说不是回文
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAXSIZE 256
typedef char elemtype;
typedef struct LinkedStackNode
{
elemtype data;
struct LinkedStackNode *next;
}LinkedStackNode,*LinkedStack;
//链栈的初始化
LinkedStack Init_LinkedStack()
{
LinkedStack top=(LinkedStackNode*)malloc(sizeof(LinkedStackNode));
if(top!=NULL)
top->next=NULL;
return top;
}
//将数据元素x插入链栈的栈顶
int Push_LinkedStack(LinkedStack top,elemtype x)
{
LinkedStackNode *node;
node=(LinkedStackNode *)malloc(sizeof(LinkedStackNode));
if(node==NULL)
{
return 0;
}
else
{
node->data=x;
node->next=top->next;
top->next=node;
return 1;
}
}
//出栈
int Pop_LinkedStack(LinkedStack top,elemtype x)
{
LinkedStackNode *node;
if(top->next==NULL)
{
return 0;
}
else
{
node=top->next;
x=node->data;
top->next=node->next;
free(node);
return 1;
}
}
//判断栈是否为空
int LinkedStack_Empty(LinkedStack top)
{
if(top->next==NULL)
{
return 1;
}
else
{
return 0;
}
}
//判断是否为回文
int Huiwen(char *line)
{
LinkedStack top;
int len,i,j=0;
char x;
top=Init_LinkedStack();
if(top==NULL)
{
printf("申请链栈空间失败,程序结束!\n");
return -1;
}
len=strlen(line);
for(i=0;i<len/2;i++)
Push_LinkedStack(top,line[i]);
while(!LinkedStack_Empty(top))
{
Pop_LinkedStack(top,x);
if(x!=line[j])
return 0;
else
j++;
}
return 1;
}
void main()
{
int k;
LinkedStack top;
char line[MAXSIZE];
printf("Please input a string:");
gets(line);
k=Huiwen(line);
if(k==1)
printf("给定的字符向量是回文!\n");
else
printf("给定的字符向量不是回文!\n");
}

错误的地方有两处:

    出栈函数"int Pop_LinkedStack(LinkedStack top,elemtype x)"应该改为"int Pop_LinkedStack(LinkedStack top,elemtype *x)",也就是说传出参数时应该用指针变量。

    回文判断函数"int Huiwen(char *line)"的while循环出错,应该先将j初始化未len-1,即在while之前加一句"j = len - 1;",同时后面的j++也要变成j--。另外,进入while循环之后的第一条出栈语句调用时就不能是值调用,而要变成地址调用,也就是改成“ Pop_LinkedStack(top, &x);”。

下附完整代码,亲测通过!

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAXSIZE 256
typedef char elemtype;
typedef struct LinkedStackNode
{
    elemtype data;
    struct LinkedStackNode *next;
}LinkedStackNode, *LinkedStack;
//链栈的初始化
LinkedStack Init_LinkedStack()
{
    LinkedStack top = (LinkedStackNode*)malloc(sizeof(LinkedStackNode));
    if (top != NULL)
        top->next = NULL;
    return top;
}
//将数据元素x插入链栈的栈顶
int Push_LinkedStack(LinkedStack top, elemtype x)
{
    LinkedStackNode *node;
    node = (LinkedStackNode *)malloc(sizeof(LinkedStackNode));
    if (node == NULL)
    {
        return 0;
    }
    else
    {
        node->data = x;
        node->next = top->next;
        top->next = node;
        return 1;
    }
}
//出栈
int Pop_LinkedStack(LinkedStack top, elemtype *x)
{
    LinkedStackNode *node;
    if (top->next == NULL)
    {
        return 0;
    }
    else
    {
        node = top->next;
        *x = node->data;
        top->next = node->next;
        free(node);
        return 1;
    }
}
//判断栈是否为空
int LinkedStack_Empty(LinkedStack top)
{
    if (top->next == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
//判断是否为回文
int Huiwen(char *line)
{
    LinkedStack top;
    int len, i, j = 0;
    char x = 0;
    top = Init_LinkedStack();
    if (top == NULL)
    {
        printf("申请链栈空间失败,程序结束!\n");
        return -1;
    }
    len = strlen(line);
    for (i = 0; i<len / 2; i++)
        Push_LinkedStack(top, line[i]);
    j = len - 1;
    while (!LinkedStack_Empty(top))
    {
        
        if (x != line[j])
            return 0;
        else
            j--;
    }
    return 1;
}
void main()
{
    int k;
    LinkedStack top;
    char line[MAXSIZE];
    printf("Please input a string:");
    gets(line);
    k = Huiwen(line);
    if (k == 1)
        printf("给定的字符向量是回文!\n");
    else
        printf("给定的字符向量不是回文!\n");

    system("pause");
}

追问

不行呀,还是那样

有没有别的什么地方了,谢谢了,麻烦你了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-02
不会
相似回答