数据结构 用栈和队列判断回文数
2013-08-17 23:02
405 查看
12321,你是不是你,这样的东西叫回文,由于队列和栈的存储方式不同,栈是LIFO,last in first out ,盘子一个一个堆,堆完后从上面开始拿;队列是FIFO,first in first out, 就像现实的排队。将数字存进这两种结构中,逐一取出,如果相同,那就是回文数。
StackAndQueqe.h
StackAndQueqe.c
简单的话只需要取出的数切半对比就行了。
StackAndQueqe.h
#include<stdio.h> typedef char DataType; typedef struct Node *PNode; struct Node{ DataType info; PNode link; }; typedef struct LinkQueqe *PQueqe; struct LinkQueqe{ PNode f; PNode b; }; typedef struct LinkStack *PStack; struct LinkStack{ PNode top; }; PStack createStack(); int isEmptyStack(PStack pstack); void pushStack(PStack pstack,DataType element); DataType popStack(PStack pstack); DataType topStack(PStack pstack); void printStack(PStack pstack); PQueqe createQueqe(); int isEmptyQueqe(PQueqe pqueqe); void pushQueqe(PQueqe pqueqe,DataType element); DataType popQueqe(PQueqe pqueqe); DataType topQueqe(PQueqe pqueqe); void printQueqe(PQueqe pqueqe);
StackAndQueqe.c
#include "StackAndQueqe.h" PQueqe createQueqe(){ PQueqe pqueqe = (PQueqe)malloc(sizeof(struct LinkQueqe)); if(pqueqe == NULL) printf("create fail"); pqueqe ->f = NULL; pqueqe ->b = NULL; return pqueqe; } int isEmptyQueqe(PQueqe pqueqe){ return (pqueqe ->f == NULL); } void pushQueqe(PQueqe pqueqe,DataType element){ PNode p = (PNode)malloc(sizeof(struct Node)); if(p == NULL) printf("nothing push"); p ->info = element; p ->link = NULL; if(pqueqe ->f == NULL){ pqueqe->f = p; //printf(" f %5d\n",pqueqe->f->info); } else pqueqe ->b ->link = p; pqueqe ->b = p; // printf("b %d\n",pqueqe->b->info); } DataType popQueqe(PQueqe pqueqe){ PNode p; DataType temp; if(isEmptyQueqe(pqueqe)) printf("queqe is empty"); p = pqueqe ->f; temp = p->info; pqueqe ->f = p->link; free(p); return temp; } DataType topQueqe(PQueqe pqueqe){ if(pqueqe->f == NULL){ printf("nothing top"); return NULL; } return pqueqe -> f->info ; } void printQueqe(PQueqe pqueqe){ PNode p = pqueqe ->f; if( isEmptyQueqe(pqueqe)){ printf("nothing print"); } while(pqueqe->f!= NULL){ printf("%3c",pqueqe->f->info); pqueqe ->f = pqueqe ->f ->link; } pqueqe ->f = p;//此处的f随着link的变化变化 最后需要还原回去! } PStack createStack(){ PStack pstack = (PStack)malloc(sizeof(struct LinkStack)); if(pstack == NULL) printf("create fail"); pstack ->top = NULL; return pstack; } int isEmptyStack(PStack pstack){ return(pstack->top == NULL); } void pushStack(PStack pstack,DataType element){ PNode p = (PNode)malloc(sizeof(struct Node)); if(p == NULL) printf("push fail"); p ->info = element; p ->link = pstack ->top; pstack ->top = p; } DataType popStack(PStack pstack){ PNode p; DataType temp; if(pstack ->top == NULL) printf("nothing pop"); p = pstack ->top; temp = p->info; pstack ->top = pstack->top->link; free(p); return temp; } DataType topStack(PStack pstack){ if(pstack ->top == NULL){ printf("nothing pop"); return NULL; } return pstack->top->info; } void printStack(PStack pstack){ PNode p= pstack ->top; if(pstack ->top == NULL){ printf("nothing pop"); } while(pstack->top != NULL){ printf("%3c",pstack ->top ->info); pstack ->top = pstack ->top ->link; } pstack ->top = p; }
#include "StackAndQueqe.h" int main(){ int i; char s[100]; int n=0; PQueqe pqueqe ; PStack pstack; printf("please input string to judge whether it is a palindrome(回文数):\n"); scanf("%c",&s[0]); while(s !='\n') { scanf("%c",&s[++n]); } printf(" the length is %d: \n",n); printf(" the string is : "); for(i=0;i<n;i++){ printf("%c",s[i]); } pqueqe = createQueqe(); for(i=0;i<n;i++){ //printf("\n%c",s[i]); pushQueqe(pqueqe,s[i]); } pstack = createStack(); for(i=0;i<n;i++){ pushStack(pstack,s[i]); } printf(" \nthe queqe is : "); printQueqe(pqueqe); printf(" \nthe stack is : "); printStack(pstack); printf(" \n"); for(i=0;i<n/2;i++){ if(popQueqe(pqueqe)!= popStack(pstack)){ printf("is not HUIWEN!\n"); break; } else { printf("it is HUIWEN!\n"); break; } } return 0; }
简单的话只需要取出的数切半对比就行了。
相关文章推荐
- 数据结构 用栈和队列判断回文数
- 数据结构 基于队列的广度优先遍历算法判断完全二叉树
- 用数据结构的栈和队列 写 回文判断
- 数据结构常见问题(二)利用栈和队列判断字符串是否是回文
- 数据结构C语言描述———用顺序队列判断回文数
- 【数据结构】队列+栈 判断回文数
- 数据结构:实验5(堆栈判断回文数)
- 【数据结构】使用栈和队列判断字符串是否是回文
- [数据结构]循环队列中判断队满与队空
- 栈和队列判断回文数
- 数据结构(十) 链队列的基本操作 ----入队列,出队列,判断队列是否为空
- 数据结构|顺序队列的实现(实验3.3)
- 数据结构-------列车重排-----队列的应用
- 判断一个数是不是回文数(Java StringBuffer)
- 数据结构之简单队列的实现
- 【栈队列】判断出栈顺序的合法性
- [Java数据结构]线性表之队列Queue
- 数据结构(ZKNU OJ)猴子选大王(循环队列解法)
- 数据结构学习之队列(链式存储)
- 【c++版数据结构】之循环队列的实现