C语言利用栈和队列实现回文字符串的判断

算法思路:

1. 定义一个队列和一个栈的结构体,分别用于存储字符串中的元素。

2. 初始化队列和栈。

3. 将字符串中的每个字符依次入队和入栈。

4. 依次将队列和栈中的元素出队和出栈进行比较,如果出队和出栈的元素不相等,则该字符串不是回文字符串,返回false;否则,继续比较下一个元素。

5. 如果队列和栈中的元素全部比较完毕,且每次比较出队和出栈的元素都相等,则该字符串是回文字符串,返回true。

6. 在主函数中,获取用户输入的字符串,调用isPalindrome()函数判断该字符串是否为回文字符串,最后输出结果。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h> // 添加头文件,使用字符串处理函数
 
#define MAX_SIZE 100 // 定义队列和栈的最大容量
 
// 定义队列的结构体
typedef struct {
    int front; // 队首指针
    int rear; // 队尾指针
    char elements[MAX_SIZE]; // 队列元素数组
}Queue;
 
// 定义栈的结构体
typedef struct{
    int elements[MAX_SIZE]; // 栈元素数组
    int top; // 栈顶指针
}Stack;
 
// 初始化队列
void initqueue(Queue *queue){
    queue->front=0;
    queue->rear=0;
}
 
// 初始化栈
void initstack(Stack *stack){
    stack->top=-1;
}
 
// 判断队列是否为空
int queueIsEmpty (Queue *queue){
    return(queue->front==queue->rear);
}
 
// 判断队列是否已满
int queueIsFull(Queue *queue){
    return (queue->rear==(queue->front+1)%MAX_SIZE);
}
 
// 入队
bool Enqueue(Queue *queue,char ch){
    if(queueIsFull(queue)){
        return false;
    }
    queue->elements[queue->rear]=ch;
    queue->rear=(queue->rear+1)%MAX_SIZE; // 环形队列,实现循环利用存储空间
    return true;
 
}
 
// 出队
bool Popqueue(Queue *queue,char *ch){
    if(queueIsEmpty(queue)){
        return false;
    }
    *ch=queue->elements[queue->front];
    queue->front=(queue->front+1)%MAX_SIZE; // 环形队列,实现循环利用存储空间
    return true;
}
 
// 入栈
bool Pushstack(Stack *stack,char ch){
    if(stack->top==MAX_SIZE-1){
        return false;
    }
    stack->elements[++stack->top]=ch;
    return true;
}
 
// 出栈
bool Popstack(Stack *stack,char *ch){
    if(stack->top==-1){
        return false;
    }
    *ch=stack->elements[stack->top--];
    return true;
}
 
// 判断字符串是否为回文字符串
bool isPalindrome(char *str){
    Stack stack;
    Queue queue;
    initqueue(&queue); // 初始化队列
    initstack(&stack); // 初始化栈
 
    int i=0;
    while(str[i]!='\0'){ // 将字符串中的字符依次入队和入栈
        Enqueue(&queue,str[i]);
        Pushstack(&stack,str[i]);
        i++;
        if(i >= MAX_SIZE){ // 判断字符串长度是否超过最大容量
            printf("输入的字符串过长!\n");
            return false;
        }
    }
 
    char queuefront,stacktop;
    while(!queueIsEmpty(&queue)){ // 将队列和栈中的元素依次出队和出栈进行比较
        Popqueue(&queue,&queuefront);
        Popstack(&stack,&stacktop);
        if(queuefront!=stacktop){ // 若出队和出栈的元素不相等,则不是回文字符串
            return false;
        }
    }
    return true; // 字符串是回文字符串
}
 
int main(void)
{
    char str[MAX_SIZE];
    printf("请输入一个字符串:");
    fgets(str, MAX_SIZE, stdin); // 使用fgets()函数获取输入字符串
    str[strlen(str) - 1] = '\0'; // 去掉fgets()函数读入的换行符
    if(isPalindrome(str)){
        printf("%s是回文字符串\n",str);
    }
    else{
        printf("%s不是回文字符串\n",str);
    }
    return 0;
}

  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LLZWHS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值