用栈和队列实现回文判断

#include<stdio.h>
#include<stdlib.h>
struct node;
typedef struct node *pnode;
struct node
{
    char info;
    pnode link;
};
struct linkstack
{
    pnode top;
};
typedef struct linkstack *plinkstack;
plinkstack createnullstack()
{
    plinkstack plstack;
    plstack=(plinkstack)malloc(sizeof(struct linkstack));
    if(plstack!=NULL)
    {
        plstack->top=NULL;
    }
    else printf("空栈建立失败\n");
    return plstack;
}
int isnullstack(plinkstack plstack)
{
    return (plstack->top==NULL);
}
void push_stack(plinkstack plstack,char x)
{
    pnode p;
    p=(pnode)malloc(sizeof(struct node));
    if(p==NULL)
    {
        printf("进栈结点申请失败\n");
    }
    else
    {
        p->info=x;
        p->link=plstack->top;
        plstack->top=p;
    }
}
void pop_stack(plinkstack plstack)
{
    pnode p;
    if(isnullstack(plstack))
    {
        printf("当前栈没有元素\n");
    }
    else
    {
        p=plstack->top;    
        plstack->top=plstack->top->link;
        free(p);
    }
}
char top_stack(plinkstack plstack)
{
    if(plstack->top==NULL)
    {
        printf("栈已空\n");
    }
    else return(plstack->top->info);
}
struct linkqueue
{
    char data;
    linkqueue *next;
};
typedef struct linkqueue *plinkqueue;
struct queue
{
    plinkqueue r;
};
typedef struct queue *pqueue;

pqueue createnullqueue()
{
    pqueue paqu;
    paqu=(pqueue)malloc(sizeof(struct linkqueue));
    if(paqu==NULL)
    {
        printf("创建空队列失败\n");
    }
    else paqu->r=NULL;
    return paqu;
}
int isnullqueue(pqueue paqu)
{
    return (paqu->r==NULL);
}

void enqueue(pqueue paqu,char x)
{
    plinkqueue p,q;
    p=(plinkqueue)malloc(sizeof(struct linkqueue));
    if(p==NULL)
        printf("申请入队结点失败\n");
    else
    {
        p->data=x;
        p->next=NULL;
        if(paqu->r==NULL)
        {
            paqu->r=p;
            paqu->r->next=paqu->r;
        }
        else
        {
            q=paqu->r->next;
            paqu->r->next=p;
            p->next=q;
            paqu->r=p;
        }
    }
}
void dequeue(pqueue paqu)
{
    plinkqueue p;
    if(paqu->r->next==paqu->r)
    {
            p=paqu->r;
            paqu->r=NULL;
            free(p);
    }
    else
    {
        p=paqu->r->next;
        paqu->r->next=paqu->r->next->next;
        free(p);
    }

}
char front_queue(pqueue paqu)
{
    if(paqu->r==NULL)
    {
        printf("当前队为空\n");
    }
    else return (paqu->r->next->data);
}
int main()
{
    plinkstack stack;
    pqueue paqu;
    char data;
    paqu=createnullqueue();
    stack=createnullstack();
    printf("请输入回文数,以#结束\n\n");
    scanf("%c",&data);
    while(data!='#')
    {
        push_stack(stack,data);
        enqueue(paqu,data);
        scanf("%c",&data);
    }
    while(!isnullqueue(paqu))
    {
        if(front_queue(paqu)==top_stack(stack))
        {
            dequeue(paqu);
            pop_stack(stack);
        }
        else
        {
            printf("NO\n");
            break;
        }
    }
    if(isnullstack(stack))
        printf("YES\n");
}
  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值