利用栈和循环队列判断回文
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;
}//==========================================================
#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;
}//==========================================================
相关文章推荐
- 利用栈和队列判断回文
- 利用栈和队列判断字符串是否是回文——c语言
- 利用while循环翻转数字之后,比较翻转后的数字是否与原来的数字相同的方法来判断是不是回文数字
- 数据结构常见问题(二)利用栈和队列判断字符串是否是回文
- js入门·循环与判断/利用函数的简单实例/使用对象/列举对象属性的名称
- 栈和队列判断回文数
- 利用非循环顺序队列采用广度搜索法求解迷宫问题(一条路径)
- 利用循环数组实现的队列
- 利用组合实现循环队列
- 循环顺序队列2——使用标示位判断队满或空
- C语言实现回文判断(利用指针的方法)
- 利用python的双向队列(Deque)数据结构实现回文检测的算法
- C语言之循环队列判断满与空
- 循环队列判断满与空
- C语言利用栈判断字符串是否为回文
- 利用短路求值和函数实现判断与(部分)循环
- javascript中利用数组实现的循环队列
- 循环队列中判断队满与队空
- 循环队列实现(通过设置标志位tag位判断空队满队)
- 利用for语句 + switch语句进行中奖判断和循环