小白学c语言,求解用栈判断回文字符串,错在哪里,急求

qq_40478118 2018-05-23 05:43:52
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define M 100

typedef struct stack
{
char data[M];
int top;
}node;



void ruzhan(node &s,char x)
{
if(s.top==M)
{
printf("栈空\n");
return;
}
else
{
s.data[s.top]=x;
s.top;
}
}



char chuzhan(node &s)
{

if(s.top==0)

{
printf("栈空\n");
return '0';
}
else
{
return s.data[--s.top];

}
}



void main()
{
int i=0;
char a[M]="\0",b[M]="\0";
node head={"\0",0};
printf("input :");
gets(a);
while(a[i]!='\0')
{
ruzhan(head,a[i]);
i++;
}
i=0;
while(head.top!=0)
{
b[i]=chuzhan(head);
i++;
}
b[i]='\0';
i=0;
while(b[i]!='\0')
{
printf("b[]=%c\n",b[i]);
i++;
}
if(strcmp(a,b)==0)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
...全文
575 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
天外怪魔 2018-05-23
  • 打赏
  • 举报
回复
入栈函数修改下即可: void ruzhan(node &s, char x) { if (s.top == M) { printf("栈空\n"); return; } else { s.data[s.top++] = x; } }
天外怪魔 2018-05-23
  • 打赏
  • 举报
回复
入栈函数修改下即可:
void ruzhan(node &s, char x)
{
    if (s.top == M)
    {
        printf("栈空\n");
        return;
    }
    else
    {
        s.data[s.top++] = x;
    }
}
自信男孩 2018-05-23
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>
#include<string.h>

#define M 100

typedef struct stack
{
    char data[M];
    int top;
}node;



void push(node &s, char x)
{
    if(s.top==M)
    {
        printf("栈满\n");
        return;
    }
    s.data[s.top++] = x;
}



char pop(node &s)
{
    if (s.top == 0) {
        printf("栈空\n");
        return '\0';
    }
    return s.data[--s.top];
}



//void main()
int main()
{
    int i=0;
    char a[M], b[M];
    node head = {"\0", 0};
    printf("input :");
    gets(a);
    while(a[i] !='\0') {
        push(head,a[i]);
        i++;
    }
    i = 0;
    while(head.top != 0)
    {
        b[i] = pop(head);
        i++;
    }
    b[i] = '\0';
    i = 0;
    while(b[i]!='\0')
    {
        printf("b[]=%c\n",b[i]);
        i++;
    }
    if (i <= 0) {
        printf("null\n");
        return 0;
    }
    if(strcmp(a,b)==0)
        printf("yes\n");
    else
        printf("no\n");
}
参考一下吧 push, pop一般表示栈的入和出。不建议使用拼音作为函数函数名。 入栈和出栈的逻辑建议对比看一下;

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧