qq_43928395 2019-11-01 08:25 采纳率: 66.7%
浏览 282
已采纳

用链表和静态栈判断回文的问题

#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条回答 默认 最新

  • dabocaiqq 2019-11-01 12:09
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况