C语言使用栈和队列来判断回文

C语言使用栈和队列来判断回文

算法思想
使用栈和队列来分别接受输入的字符串,利用栈先进后出,队列先进先出的特点进行前后比较来判断是否是回文

你可以使用双端队列更好的解决问题。

使用数组的话,代码又长又不高效

 #include<stdio.h>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define MaxSize 50
bool isHWord(char* str) {
    stack<char> p;
    queue<char> q;
    string Hword= str;    
    for (int i = 0; i < Hword.length(); i++) {
        char s= Hword[i];
        p.push(s);
        q.push(s);
    }
    
    while (!p.empty() && !q.empty()) {
        if (p.top() != q.front()) {
            break;
        }
        p.pop();
        q.pop();
    }
    return p.empty() && q.empty();
}
int main() {
 		while(1){
        printf( "输入一个字符串:");
        char str[MaxSize];
        gets(str);
        isHWord(str) ? printf("是回文!\n") :printf("不是回文\n");//调用函数进行判断
        } 
    return 0;
}

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是 C 语言利用栈和队列实现回文判断的代码示例: ```c #include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 typedef int ElemType; typedef struct { ElemType data[MAXSIZE]; int top; }SqStack; typedef struct QNode { ElemType data; struct QNode *next; }QNode, *QueuePtr; typedef struct { QueuePtr front, rear; }LinkQueue; void InitStack(SqStack *S) { S->top = -1; } int StackEmpty(SqStack S) { if(S.top == -1) return 1; else return 0; } int StackFull(SqStack S) { if(S.top == MAXSIZE - 1) return 1; else return 0; } void Push(SqStack *S, ElemType x) { if(StackFull(*S)) { printf("Stack is full!\n"); exit(1); } else { S->data[++S->top] = x; } } ElemType Pop(SqStack *S) { if(StackEmpty(*S)) { printf("Stack is empty!\n"); exit(1); } else { return S->data[S->top--]; } } void InitQueue(LinkQueue *Q) { Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); if(!Q->front) { printf("Memory allocation failed!\n"); exit(1); } Q->front->next = NULL; } void EnQueue(LinkQueue *Q, ElemType x) { QueuePtr p; p = (QueuePtr)malloc(sizeof(QNode)); if(!p) { printf("Memory allocation failed!\n"); exit(1); } p->data = x; p->next = NULL; Q->rear->next = p; Q->rear = p; } ElemType DeQueue(LinkQueue *Q) { QueuePtr p; ElemType x; if(Q->front == Q->rear) { printf("Queue is empty!\n"); exit(1); } p = Q->front->next; Q->front->next = p->next; x = p->data; if(Q->rear == p) Q->rear = Q->front; free(p); return x; } int main() { SqStack s; LinkQueue q; int n, i, flag = 1; char c[MAXSIZE]; printf("Input a string:"); gets(c); n = strlen(c); InitStack(&s); InitQueue(&q); for(i = 0; i < n; i++) { if(c[i] >= 'A' && c[i] <= 'Z') c[i] = c[i] + 32; if(c[i] >= 'a' && c[i] <= 'z') { Push(&s, c[i]); EnQueue(&q, c[i]); } } while(!StackEmpty(s) && flag) { if(Pop(&s) != DeQueue(&q)) flag = 0; } if(flag == 1) printf("The string is a palindrome!\n"); else printf("The string is not a palindrome!\n"); return 0; } ``` 请问您还有什么需要我帮忙的吗?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大星心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值