利用栈和队列完成回文的判断代码
时间: 2023-10-22 07:05:22 浏览: 32
好的,以下是利用栈和队列判断回文的代码,供您参考:
```python
def is_palindrome(s):
# 创建栈和队列
stack = []
queue = []
# 将字符串中的字符依次入栈和入队
for c in s:
if c.isalpha():
stack.append(c.lower())
queue.append(c.lower())
# 依次比较栈和队列中的字符是否相等
while stack and queue:
if stack.pop() != queue.pop(0):
return False
return True
```
当然,上面这段代码只是实现了判断回文的功能,如果需要更全面的栈和队列知识,还需要更加深入的学习。
相关问题
c语言利用栈和队列完成回文的判断代码
好的,以下是 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;
}
```
请问您还有什么需要我帮忙的吗?
利用栈和队列完成回文的判断代码c++
栈和队列都可以用来实现回文的判断。以下是两种方法的C++代码实现:
使用栈:
```cpp
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isPalindrome(string str) {
stack<char> s;
int n = str.length();
int i;
for (i = 0; i < n / 2; i++) {
s.push(str[i]);
}
if (n % 2 != 0) {
i++;
}
while (i < n) {
if (s.top() != str[i]) {
return false;
}
s.pop();
i++;
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
if (isPalindrome(str)) {
cout << str << " is a palindrome" << endl;
} else {
cout << str << " is not a palindrome" << endl;
}
return 0;
}
```
使用队列:
```cpp
#include <iostream>
#include <queue>
#include <string>
using namespace std;
bool isPalindrome(string str) {
queue<char> q;
int n = str.length();
int i;
for (i = n - 1; i >= n / 2; i--) {
q.push(str[i]);
}
if (n % 2 != 0) {
i--;
}
while (i >= 0) {
if (q.front() != str[i]) {
return false;
}
q.pop();
i--;
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
if (isPalindrome(str)) {
cout << str << " is a palindrome" << endl;
} else {
cout << str << " is not a palindrome" << endl;
}
return 0;
}
```