栈&&队列判断回文

 1 #include <stdio.h>
 2 #include <stdbool.h>
 3 #include <stdlib.h>
 4 
 5 #define MAXSIZE 10000
 6 typedef struct{
 7     char data[MAXSIZE];
 8     int top;
 9 
10 }SqStack;
11 typedef SqStack *Stack;
12 typedef struct{
13     char data[MAXSIZE];
14     int front;
15     int rear;
16 
17 }Queue;
18 typedef Queue* queue;
19 char PopQueue(Queue *q);
20 bool PushQueue(Queue *q,char c);
21 bool PushStack(SqStack *s,char c);
22 bool StatckEmpty(SqStack *s);
23 char PopStack(SqStack *s);
24 int main() {
25    // char string[MAXSIZE];
26     Stack s= (Stack)malloc(sizeof(SqStack));
27     s->top=-1;
28     queue q=(queue)malloc(sizeof(Queue));
29     q->front=q->rear=0;
30     bool flag=true;
31    // gets(string);
32     char p;
33     while ((p=getchar())!='?'){
34         PushQueue(q,p);
35         PushStack(s,p);
36     }
37     while(!StatckEmpty(s)){
38         if(PopQueue(q)!=PopStack(s)){
39             flag=false;
40             printf("NO");
41             break;
42         }
43 
44 
45     }
46     if(flag){
47         printf("Yes");
48     }
49 
50    // printf("Hello, World!\n");
51     return 0;
52 }
53 
54 bool StatckEmpty(SqStack *s){
55     return s->top==-1;
56 }
57 bool PushStack(SqStack *s,char c){
58     if(s->top==MAXSIZE-1){
59         return false;
60     }
61     s->top++;
62     s->data[s->top]=c;
63     return true;
64 }
65 char PopStack(SqStack *s){
66     if(s->top==-1){
67         return '0';
68     }
69     return s->data[s->top--];
70 }
71 bool PushQueue(Queue *q,char c){
72     if((q->rear+1)%MAXSIZE==q->front){
73         return false;
74     }
75     q->rear=(q->rear+1)%MAXSIZE;
76     q->data[q->rear]=c;
77     return  true;
78 }
79 char PopQueue(Queue *q)
80 {
81     if(q->rear==q->front){
82         return '0';
83     }
84     q->front=(q->front+1)%MAXSIZE;
85     char f=q->data[q->front];
86     return f;
87 }
View Code

思路:队列先进先出,栈后进后出,读入字符串分别入栈入队,再分别出栈出队,当任一次不匹配则字符串不是回文

 

 

 

posted @ 2020-10-11 14:09  ethon-wang  阅读(414)  评论(0编辑  收藏  举报