33,311
社区成员
发帖
与我相关
我的任务
分享
void ruzhan(node &s, char x)
{
if (s.top == M)
{
printf("栈空\n");
return;
}
else
{
s.data[s.top++] = x;
}
}
#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一般表示栈的入和出。不建议使用拼音作为函数函数名。
入栈和出栈的逻辑建议对比看一下;