C语言数据结构中利用栈和队列实现回文的判断

数据结构中栈有着极为广大的运用,其操作特点是FILO先进后出。队列的特点是FIFO先进先出。判断回文,回文序列很好理解,正反来看它都一样。那我们可以巧妙的利用栈和队列特点来判断回文,存入进抽象结构中,如果输出结果匹配,则为回文。

例如:aabaa为回文序列。进栈顺序为aabaa,其出栈为aabaa。同理队列进队aabaa,出队为aabaa。可以发现,回文前半段在队列中先输出,后半段在栈先输出,则进行匹配,如果相同,则是回文。

这里栈和队列都采用链式结构:

 

typedef char DataType;
typedef struct node
{
    DataType info;
    struct node *next;
}queuedata;

typedef struct queuerecord{
    queuedata *front, *rear ;
}linkqueue;
typedef struct node *pqueuedata;
typedef struct queuerecord *plinkqueue;

struct node1{
    DataType element;
    struct node1 *next;
};

typedef struct node1 *PtrToNode;
typedef struct node1 * stack;

核心代码如下

int palindrome(char src[])
{
    plinkqueue p1=createqueue();
    stack p2=createStack();
    int i=0;
    int j=0;
    while(src[i]!='\0')
    {
        pushqueue(src[i], p1);
        i++;
    }
    while(src[j]!='\0')
    {
        pushstack(src[j],p2);
        j++;
    }
    while(emptyequeue(p1)!=1)
    {
        DataType x1=popqueue(p1);
        DataType x2=popstack(p2);
        if(x1!=x2)
            return 0;
    }
    return 1;
}

 完整代码:

#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct node
{
    DataType info;
    struct node *next;
}queuedata;

typedef struct queuerecord{
    queuedata *front, *rear ;
}linkqueue;
typedef struct node *pqueuedata;
typedef struct queuerecord *plinkqueue;

plinkqueue createqueue( )
{
    plinkqueue p=(plinkqueue)malloc(sizeof(linkqueue));
    p->front=NULL;
    p->rear=NULL;

}

int emptyequeue(plinkqueue q)
{
    plinkqueue p;
    p=q;
    if(p->front==NULL)
        return 1;
    else
        return 0;


}

void pushqueue(DataType x, plinkqueue q)
{
    pqueuedata p=(pqueuedata)malloc(sizeof(struct node));
    if(q->front==NULL&&q->rear==NULL)
    {
        q->front=p;
        q->rear=p;
    }
    p->info=x;
    p->next=NULL;
    q->rear->next=p;
    q->rear=q->rear->next;


}


DataType popqueue(plinkqueue q)
{
    pqueuedata p=q->front;
    DataType temp=q->front->info;
    q->front=q->front->next;
    free(p);
    return temp;

}

struct node1{
    DataType element;
    struct node1 *next;
};

typedef struct node1 *PtrToNode;
typedef struct node1 * stack;


int emptystack(stack s)
{

    if(s->element==0&&s->next==NULL)
        return 1;
    else
        return 0;


}
stack createStack(void)
{

    stack top=(stack)malloc(sizeof(struct node1));
    top->element=0;
    top->next=NULL;
    return top;


}


void pushstack(DataType  x,stack s)
{

    stack temp=(struct node1 *)malloc(sizeof(struct node1));
    temp->element=x;
    temp->next=s->next;
    s->next=temp;


}
DataType popstack(stack s)
{

    if(emptystack(s))
        return -1;
    else
    {
        stack node=s->next;
        int tmp;
        tmp=node->element;
        s->next=node->next;
        free(node);

        return tmp;

    }

}

int palindrome(char src[])
{
    plinkqueue p1=createqueue();
    stack p2=createStack();
    int i=0;
    int j=0;
    while(src[i]!='\0')
    {
        pushqueue(src[i], p1);
        i++;
    }
    while(src[j]!='\0')
    {
        pushstack(src[j],p2);
        j++;
    }
    while(emptyequeue(p1)!=1)
    {
        DataType x1=popqueue(p1);
        DataType x2=popstack(p2);
        if(x1!=x2)
            return 0;
    }
    return 1;
}

int main(void)
{

    char a[100]={'0'};
    scanf("%s",a);

    int res=palindrome(a);
    if(res==1)
        printf("该序列是回文序列");
    else
        printf("该序列不是回文序列");




    return 0;
}

  • 10
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值