java 栈 队列 回文_数据结构 用栈和队列判断回文数

12321,你是不是你,这样的东西叫回文,由于队列和栈的存储方式不同,栈是LIFO,last in first out ,盘子一个一个堆,堆完后从上面开始拿;队列是FIFO,first  in first out, 就像现实的排队。将数字存进这两种结构中,逐一取出,如果相同,那就是回文数。

StackAndQueqe.h

#include

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]!='\n')

{

scanf("%c",&s[++n]);

}

printf(" the length is %d: \n",n);

printf(" the string is : ");

for(i=0;i

printf("%c",s[i]);

}

pqueqe = createQueqe();

for(i=0;i

//printf("\n%c",s[i]);

pushQueqe(pqueqe,s[i]);

}

pstack = createStack();

for(i=0;i

pushStack(pstack,s[i]);

}

printf(" \nthe queqe is : ");

printQueqe(pqueqe);

printf(" \nthe stack is : ");

printStack(pstack);

printf(" \n");

for(i=0;i

if(popQueqe(pqueqe)!= popStack(pstack)){

printf("is not HUIWEN!\n");

break;

}

else {

printf("it is HUIWEN!\n");

break;

}

}

return 0;

}

简单的话只需要取出的数切半对比就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值