#include<stdlib.h>
#include<stdio.h>
#include<string.h>
typedef struct LinkNode Queue;
struct LinkNode{
char data;
Queue* next;
};
typedef struct LQueue LinkQueue;
struct LQueue
{
Queue* front;//队头
Queue* rear;// 队尾
};
LinkQueue* EnQueue(LinkQueue* S, char num)//入队
{
Queue* p = (Queue*)malloc(sizeof(Queue));
if (p) {
S->rear->next = p;//指向p,建立联系
S->rear = p;
S->rear->data = num;
S->rear->next = NULL;
}
return S;
}
LinkQueue* DeQueue(LinkQueue* S)//出队
{
if (S->front->next == NULL) {
return S;
}
else {
Queue* p = S->front->next;
S->front->next = S->front->next->next;
free(p);
return S;
}
}
int main()
{
int i, j, Length = 0;
char a[] = "AEHIJLMOSTUVWXYZ12358";//记录下所有镜像串
char b[] = "A3HILJMO2TUVWXYZ1SEZ8";
Queue* head = (Queue*)malloc(sizeof(Queue));//空结点
LinkQueue* S = (LinkQueue*)malloc(sizeof(LinkQueue));
if (S && head) {
S->front = head;//初始化
S->rear = head;//初始化
/*建栈*/
char str[101];
scanf_s("%s", str, 100);
str[100] = '\0';
while (Length < strlen(str)) {
S = EnQueue(S, str[Length]);
++Length;
}
/*查找并判断*/
Queue* p = S->front->next;//头结点的下一个;
int h = 0;
for (i = 0, j = 0; i < strlen(a) && j < Length / 2; i++) {
if (p->data == a[i]) {
while (h < Length - 1 - j) {
//使P指向对应的结点
p = p->next;
++h;
}
if (p->data != b[i])
{
break;
}
else {
S = DeQueue(S);
++j;
i = 0;//刷新
}
p = S->front->next;//刷新
h = j;//刷新
}
}
if (j != Length / 2)
printf("%s--is not a mirrored string", str);
else
printf("%s--is a mirrored string", str);
}
}
利用队列判断镜像串
猜你喜欢
转载自blog.csdn.net/qq_52001969/article/details/113574706
今日推荐
周排行