qq_43928395 2019-11-01 08:34 采纳率: 66.7%
浏览 212

请问栈的遍历和回文的判断出现了什么问题?

#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define QueueSize 100 
typedef int Status;
typedef char ElemType;
typedef struct Stack{
    ElemType data[STACK_INIT_SIZE];
    int top;
}SqStack;//静态顺序栈 
void TraverseStack(SqStack s)
{//对栈进行遍历 
    while(s.top!=0)
    {
        s.top--;
        printf("s.data[%d]=%c",s.top,s.data[s.top]);
    }
 } 
Status InitStack(SqStack&s)
{//栈的初始化 
    s.top=0;
    return OK;
}
Status Push(SqStack&s,ElemType e)
{//入栈 
    if(s.top>=0)
    return ERROR;   
    s.data[s.top]=e;
    s.top++;
    return OK;
 } 
 Status Pop(SqStack&s,ElemType &e)
 {//出栈
  if(s.top==0)
  return ERROR;
  s.top--;
  e=s.data[s.top];
  return OK;
 }
 int StackEmpty(SqStack s)
 {//判断栈是否为空
  if(s.top==0)
  return 1;
  else 
  return 0;
 }
 ElemType GetTop(SqStack s)
 {//返回栈顶的元素 
    if(!StackEmpty(s))
    return ERROR;
    ElemType e;
    s.top--;
    e=s.data[s.top];
    return  e;
  } 
  typedef struct Queue
  {
    ElemType *base;
    int front,rear;
  }SeqQueue;
  Status InitQueue(SeqQueue&Q)
  {//初始化队列 
    Q.base=(ElemType*)malloc(QueueSize*sizeof(ElemType));
    if(!Q.base)exit(OVERFLOW);
    Q.front=Q.rear=0;
    return OK;
  }
  Status EnQueue(SeqQueue&Q,ElemType e)
  {//入队 
  if((Q.rear+1)%QueueSize ==Q.front)
  {
    printf("Queue overflow");
    return OK;
  }
  Q.base[Q.rear]=e;
  Q.rear=(Q.rear+1)%QueueSize;
  return OK;
}
Status DeQueue(SeqQueue&Q,ElemType &e)
{//出队 
    if(Q.front==Q.rear)
    {
        printf("Queue empty");
        return ERROR;
    }
    e=Q.base[Q.front];
    Q.front=(Q.front+1%QueueSize);
    return OK;
 } 
 ElemType GetHead(SeqQueue Q)
 {//取队列的第一个元素 
    if(Q.front==Q.rear)
     {
     printf("Queue empty");
     return ERROR ;
 }
 else return Q.base[Q.front];
}
Status QueueTraverse(SeqQueue Q)
{//遍历队列 
    int p;
    if(Q.front==Q.rear){
     printf("Queue empty");
     return ERROR ;
 }
 p=Q.front;
 do
 {
    printf("%2c",Q.base[p]);
    p=(p+1)%QueueSize;
 }while(p!=Q.rear);
 return OK;
}
int main()
{
    SqStack s;
    SeqQueue q;
    ElemType ch ,e1,e2;
    int state;
    InitStack(s);
    InitQueue(q);
    printf("input a string ending by#:");
    scanf("%c",&ch);
    while(ch!='#')
    {
        Push(s,ch);
        EnQueue(q,ch);
        scanf("%c",&ch);
    }
    printf("\n The Stack is:");
    TraverseStack(s);
    printf("\n The Queue is:");
    QueueTraverse(q);
    printf("\n");
    state=TRUE;
    while(!StackEmpty(s)&&state)
    {
        if(GetTop(s)==GetHead(q))
        {
            Pop(s,e1);
            DeQueue(q,e2);
        }
        else
        state=FALSE;
    }
    if(state)
    printf("This string is HuiWen!\n ");
    else
    printf("This string is not HuiWen!\n");
    return 0;
}

图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • 大紫明宫空离境 2019-11-01 10:59
    关注

    同时使用栈和队列,没有判断中点,所以你输入任何串都是回文数!!!

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?