|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- #define MaxSize 100
- typedef int ElemType;
- typedef struct
- { ElemType data[MaxSize];
- int top;
- }SqStack;
-
- //初始化栈运算算法
- void InitStack(SqStack &st)
- {
- st.top=-1;
- }
-
- //销毁栈运算算法
- void DestroyStack(SqStack st)
- {
- }
-
- //进栈运算算法
- int Push(SqStack &st,ElemType x)
- { if(st.top==MaxSize-1) //栈满上溢出返回0
- return 0;
- else
- { st.top++;
- st.data[st.top]=x;
- return 1; //成功进栈返回1
- }
- }
- //出栈运算算法
- int Pop(SqStack &st,ElemType &x) //x为引用型参数
- { if(st.top==-1) //栈空返回0
- return 0;
- else
- { x=st.data[st.top];
- st.top--; //成功出栈返回1
- return 1;
- }
- }
-
- //取栈顶元素运算算法
- int GetTop(SqStack st,ElemType &x) //x为引用型参数
- { if(st.top==-1) //栈空返回0
- return 0;
- else
- { x=st.data[st.top];
- return 1; //成功取栈项返回1
- }
- }
-
- //判断栈空运算算法
- int StackEmpty(SqStack st)
- { if(st.top==-1)return 1; //栈空返回1
- else return 0; //栈不空返回0
- }
-
- int Palindrome(char str[],int n)
- {
- SqStack st;
- InitStack(st);
- int i;
- int ch;
- for(i=0;i<n;i++)
- Push(st,str[i]);
- i=0;
- while(!StackEmpty(st))
- {
- Pop(st,ch);
- if(ch!=str[i++])
- {
- DestroyStack(st);
- return 0;
- }
- }
- DestroyStack(st);
- return 1;
- }
- int main(){
- printf("输入一个字符串:\n");
- char str[81];
- int n;
- int flag;
- scanf("%d",&n);
- flag=Palindrome(str,n);
- if (flag)
- {
- printf("字符串是回文");
- }
- else
- {
- printf("字符串不是回文");
- }
- return 0;
- }
-
复制代码
程序可以运行但是结果有问题
主函数那里有点问题
- #include<stdio.h>
- #include<stdlib.h>
- #define MaxSize 100
- typedef int ElemType;
- typedef struct
- { ElemType data[MaxSize];
- int top;
- }SqStack;
-
- //初始化栈运算算法
- void InitStack(SqStack &st)
- {
- st.top=-1;
- }
-
- //销毁栈运算算法
- void DestroyStack(SqStack st)
- {
- }
-
- //进栈运算算法
- int Push(SqStack &st,ElemType x)
- { if(st.top==MaxSize-1) //栈满上溢出返回0
- return 0;
- else
- { st.top++;
- st.data[st.top]=x;
- return 1; //成功进栈返回1
- }
- }
- //出栈运算算法
- int Pop(SqStack &st,ElemType &x) //x为引用型参数
- { if(st.top==-1) //栈空返回0
- return 0;
- else
- { x=st.data[st.top];
- st.top--; //成功出栈返回1
- return 1;
- }
- }
-
- //取栈顶元素运算算法
- int GetTop(SqStack st,ElemType &x) //x为引用型参数
- { if(st.top==-1) //栈空返回0
- return 0;
- else
- { x=st.data[st.top];
- return 1; //成功取栈项返回1
- }
- }
-
- //判断栈空运算算法
- int StackEmpty(SqStack st)
- { if(st.top==-1)return 1; //栈空返回1
- else return 0; //栈不空返回0
- }
-
- int Palindrome(char str[],int n)
- {
- SqStack st;
- InitStack(st);
- int i;
- int ch;
- for(i=0;i<n;i++)
- Push(st,str[i]);
- i=0;
- while(!StackEmpty(st))
- {
- Pop(st,ch);
- if(ch!=str[i++])
- {
- DestroyStack(st);
- return 0;
- }
- }
- DestroyStack(st);
- return 1;
- }
- int main(){
- printf("输入一个字符串长度:\n");
- char str[81];
- int n;
- int flag;
- scanf("%d",&n);
- getchar(); // 缓冲输入的回车符
- printf("输入%d个字符:\n",n);
- gets(str);
- flag=Palindrome(str,n);
- if (flag)
- {
- printf("字符串是回文");
- }
- else
- {
- printf("字符串不是回文");
- }
- return 0;
- }
复制代码
|
|