您的位置:首页 > 其它

利用栈和循环队列判断回文

2009-11-10 19:33 369 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 40
typedef struct{
char *base;
int frone;
int rear;
}user_queue;
typedef struct{
char *base;
char *top;
}user_stack;
user_queue *reword;
user_stack *c_stack;
//--------------------------------------------------
int EnQueue(user_queue *reword,char e){
if((reword->rear+1)%MAXSIZE==reword->frone) return 0;
reword->base[reword->rear]=e;
reword->rear=(reword->rear+1)%MAXSIZE;
return 1;
}//-----------------------------------------------------
int DeQueue(user_queue *reword,char *e){
if(reword->frone==reword->rear) return 0;
*e=reword->base[reword->frone];
reword->frone=(reword->frone+1)%MAXSIZE;
return 0;
}//--------------------------------------------------------
int queuelen(user_queue *reword){
return (reword->rear-reword->frone+MAXSIZE)%MAXSIZE;
}//--------------------------------------------------------
int push(user_stack *c_stack,char e){
*(c_stack->top)=e;
(c_stack->top)++;
return 1;
}//-----------------------------------------------------------
int pop(user_stack *c_stack,char *e){
(c_stack->top)--;
*e=*(c_stack->top);
return 0;
}//---------------------------------------------------------
int main(){
char e,a,b;
int L,i;
reword=(user_queue *)malloc(sizeof(user_queue));
reword->base=(char *)malloc(MAXSIZE*sizeof(char));
reword->frone=reword->rear=0;
c_stack=(user_stack *)malloc(sizeof(user_stack));
c_stack->base=(char *)malloc(MAXSIZE/2*sizeof(char));
c_stack->top=c_stack->base;
printf("please input char,end('#'):/n");
e=getchar();
while(e!='#'){
EnQueue(reword,e);
e=getchar();
}
L=queuelen(reword);
for(i=0;i<L/2;i++){
DeQueue(reword,&e);
push(c_stack,e);
}
if(L%2!=0) DeQueue(reword,&e);
for(i=0;i<L/2;i++){
DeQueue(reword,&a);
pop(c_stack,&b);
if(a!=b){
printf("the string is NOT back_word./n");
return 0;
}
}
printf("the string is back_word./n");
return 1;
}//==========================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: