回文判断Word格式.docx

上传人:b****6 文档编号:19725040 上传时间:2023-01-09 格式:DOCX 页数:25 大小:20.71KB
下载 相关 举报
回文判断Word格式.docx_第1页
第1页 / 共25页
回文判断Word格式.docx_第2页
第2页 / 共25页
回文判断Word格式.docx_第3页
第3页 / 共25页
回文判断Word格式.docx_第4页
第4页 / 共25页
回文判断Word格式.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

回文判断Word格式.docx

《回文判断Word格式.docx》由会员分享,可在线阅读,更多相关《回文判断Word格式.docx(25页珍藏版)》请在冰豆网上搜索。

回文判断Word格式.docx

free(p);

}returnd;

}intmain(void){datainput[MAX];

stacktemp;

inti=0;

intflag=0;

initialize(&

temp);

//初始化临时栈scanf("

%s"

&

input);

//输入字符串while(input[i]!

='

@'

){//字符串入栈push(input[i],&

i++;

}while(!

empty(&

temp)){//字符依次出栈和字符数组比较,判断是否回文数if(temp.top->

d==input[flag]){pop(&

flag++;

}else{printf("

此字符序列不是回文数!

\n"

);

break;

}}if(empty(&

temp))printf("

此字符序列是回文数!

return1;

}

判断用户输入的字符串是否为回文

//**********题目****************//

//判断用户输入的字符串是否为回文

//回文是指顺读和反读都一样的串

//例:

abccba为回文,abcdab不是回文

//*****************************//

//数据结构:

循环队列和顺序栈

//算法思想:

//1.将字符串按照用户输入的顺序分别入栈和队列

//2.分别从队列和栈中取出首个字符

//3.比较取出的字符,若相等,继续分别从队列和栈中取首个字符;

否则跳出循环,并设置标志flag=0;

//4.若队列和栈中的字符取完,则结束,设置标志flag=1;

//5.flag=1,表示字符从前往后和从后往前的序列完全匹配,该字符串属于回文

//6.flag=0,表示字符从前往后和从后往前的序列不完全匹配,该字符串不属于回文

//programer:

cooler

#include<

stdio.h>

#include<

stdlib.h>

#definem100

typedefstruct

{

charstack[m];

inttop;

}stackstru;

//定义栈

typedefstruct{

charqueue[m];

intfront;

intrear;

}queuestru;

//定义队列

voidmain()

//函数声明

intstinit(stackstru*s);

//初始化顺序栈

intstempty(stackstru*s);

//判断栈是否为空

intstpush(stackstru*s,charx);

//入栈

charstpop(stackstru*s);

//出栈

intquinit(queuestru*q);

//初始化循环队列

intquempty(queuestru*q);

//判断队列是否为空

intenqueue(queuestru*q,chare);

//入队

chardequeue(queuestru*q);

//出队

//

charc;

intflag=0;

stackstru*s=(stackstru*)malloc(sizeof(stackstru));

//为顺序栈申请空间

queuestru*q=(queuestru*)malloc(sizeof(queuestru));

//为队列申请空间

stinit(s);

//初始化栈

quinit(q);

//初始化队列

printf("

Inputastring:

//输入字符串,输入@标示输入结束。

while((c=getchar())!

='

)//将输入的字符串入栈和队列

putchar(c);

//输出输入的字符

stpush(s,c);

//字符进栈

enqueue(q,c);

//字符进队列

Endinput!

//提示信息

while(stempty(s))//栈中还有元素

if(stpop(s)==dequeue(q))//出栈的字符与出队列的字符匹配

flag=1;

//将标志设置为1

continue;

//继续从栈和队列中区字符

else//字符不匹配

flag=0;

break;

//跳出循环,将标志设置为0

if(flag==1)

Thisstringispalindrome!

//标志位为1,完全匹配,是回文

else

Thisstringisn'

tpalindrome!

//标志位为0,不完全匹配,不是回文

intstinit(stackstru*s)

s->

top=0;

return1;

}//初始化栈

intstempty(stackstru*s)

if(s->

top==0)//栈顶为空

return0;

}//判断栈是否空

intstpush(stackstru*s,charx)

top==m)//栈满

Thestackisoverflow!

//输出提示信息

else//栈未满

top=s->

top+1;

//栈顶后移

stack[s->

top]=x;

//字符入栈

}//入栈操作

charstpop(stackstru*s)

chary;

top==0)//栈为空

Thestackisempty!

return'

'

;

//返回空格

else//栈不为空

y=s->

top];

//取出栈顶元素

top-1;

//栈顶指示移动

returny;

}//出栈操作

intquinit(queuestru*q)

q->

front=0;

rear=0;

}//初始化为一个空的循环队列

intquempty(queuestru*q)

if(q->

front==q->

rear)//队头和队尾相等

}//判断队列是否为空

intenqueue(queuestru*q,chare)

if((q->

rear+1)%m==q->

front)//队列已满

Thequeueisoverflow!

queue[q->

rear]=e;

rear=(q->

rear+1)%m;

//移动队尾指针

}//入队操作

chardequeue(queuestru*q)

charf;

rear)//队列为空

Thequeueisempty!

f=q->

front];

//取出队首元素

front=(q->

front+1)%m;

//移动对头指针

returnf;

}//出队操作

typedefstructnode{

chardata;

structnode*next;

}node;

node*top;

unsignedintsize;

}stack;

typedefstruct{

node*front;

node*back;

}queue;

charpops(stack*a)

node*tf=NULL;

charrt=0;

if(a->

size){

tf=a->

a->

top=a->

--a->

size;

rt=tf->

data;

free(tf);

}

returnrt;

charpopq(queue*a)

front;

front=a->

front->

voidpush(stack*a,constcharc)

node*t=(node*)malloc(sizeof(node));

if(t){

t->

data=c;

next=a->

top=t;

++a->

voidpush_back(queue*a,constcharc)

next=NULL;

a->

back=t;

else

{

back->

next=t;

intisempty(void*a,inttp)

if(tp==1)return!

(((stack*)a)->

size);

elsereturn!

(((queue*)a)->

voidinitqs(void*a,inttp)

if(tp==1){

((stack*)a)->

top=NULL;

size=0;

else{

((queue*)a)->

front=((queue*)a)->

back=NULL;

voiddel(void*a,inttp)

node*t;

while(((stack*)a)->

top){

t=((stack*)a)->

top=((stack*)a)->

free(t);

free(a);

else{

while(((queue*)a)->

front){

t=((queue*)a)->

intchk(void)

charch;

intfg=1,rt=0;

stack*a=(stack*)malloc(sizeof(stack));

queue*b=(queue*)malloc(sizeof(queue));

a||!

b){

fprintf(stderr,"

MEMORYERROR"

exit(-1);

initqs(a,1);

initqs(b,0);

puts("

Enterastring,endwith@"

while((ch=getchar())!

push(a,ch);

push_back(b,ch);

while(!

isempty(a,1)&

&

!

isempty(b,0))

if(pops(a)!

=popq(b)){

fg=0;

if(fg&

isempty(b,0))rt=1;

del(a,1);

del(b,0);

intmain(void)

if(chk())puts("

YES"

elseputs("

NO"

return0;

//---------------------------------------------------------------------------

用栈实现了判断回文数的操作,即把字符串依次入栈,然后出栈并依次和字符数组比较是否相等,从而判断字符序列是否回文数,代码如下:

#defineEMPTY0

#defineFULL10000

#defineMAX10000

typedefchardata;

typedefstructelem{

datad;

typedefstructstack{

intcnt;

//栈操作函数

voidinitialize(stack*stk)

boolempty(conststack*stk)

returnstk->

boolfull(conststack*stk)

voidpush(datad,stack*stk)

full(stk))

p=(elem*)malloc(sizeof(elem));

datapop(stack*stk)

empty(stk))

d=stk->

returnd;

datainput[MAX];

//初始化临时栈

scanf("

//输入字符串

while(input[i]!

{//字符串入栈

push(input[i],&

temp))

{//字符依次出栈和字符数组比较,判断是否回文数

if(temp.top->

d==input[flag])

pop(&

printf("

if(empty(&

本文来自CSDN博客,转载请标明出处:

我自己写了一个,你看看

/**********************************

用栈和队列进行回文判断

输入字符以@结束

***********************************/

/*定义一个栈*/

typedefstructStack

intsize;

char*Base;

char*Top;

}Stack;

/*创建一个栈*/

voidCreateStack(Stack*S,intsize)

S->

size=size;

Base=(char*)malloc(size);

Top=S->

Base;

/*推入一个元素*/

voidPush(Stack*S,charc)

/*栈满了,不能插入了*/

if(S->

Top-S->

Base==S->

size)

Stackisfullandcan'

tpush!

"

return;

*(++S->

Top)=c;

voidPop(Stack*S)

/*栈空了*/

Top==S->

Base)

Stackisempty!

Top--;

StackS;

intBegin;

CreateStack(&

S,100);

Begin=0;

while

(1)

scanf("

%c"

&

c);

if(c=='

'

&

!

Begin)

Begin=1;

if(Begin)

if(*(S.Top)==c)

Pop(&

S);

Push(&

S,c);

if(S.Top==S.Base)

ok\n"

fail\n"

getch();

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

参考答案:

//---------------------------------------------------------------------------

node*tf=null;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 商业计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1