正在加载图片...
Q rear(Q rear+1)%MAXSIZE Q base[Q rear=x Q length+ eturn OK. i//EnCyQueue Status DeCy Queue( qUeue&Qnt&x带 length域的循环队列出队算法 if(Q length==0)return INFEASIBLE head=( Q. rear-Q length+1)% MAXSIZE,∥详见书后注释 xQ. base head] Q. length-; 1//DeCyQueue 3.31 int Palindrome TestO∥判别输入的字符串是否回文序列是则返回1,否则返回0 Init Stack(S), Init Queue(Q) while((c=getchar=(@) Push(S,c) EnQueue(Q,c)∥同时使用栈和队列两种结构 while(STack Empty(s) Pop(s, a), DeQueue(Q, b)); if(a!=b) return ERROR return OK. i//Palindrome Test 3.32 void Get Fib Cy Queue(int k, int n/求k阶斐波那契序列的前n+1项 Init CyQueue(Q),∥其 MAXSIZE设置为k for(i0; i<k-1; 1++)Qbase[=0; Q basek-ll=1;/给前k项赋初值 for(i=0; i<k; i++)printf("%d",Q base[i]) for(Fk; K<=n; i++) m=i%k:sum=0 for(=0; j<k j++) sum+=Q base[(m+j)%k] Q base[m}=sum,∥求第i项的值存入队列中并取代已无用的第一项 printf("%",sum);Q.rear=(Q.rear+1)%MAXSIZE; Q.base[Q.rear]=x; Q.length++; return OK; }//EnCyQueue Status DeCyQueue(CyQueue &Q,int &x)//带 length 域的循环队列出队算法 { if(Q.length==0) return INFEASIBLE; head=(Q.rear-Q.length+1)%MAXSIZE; //详见书后注释 x=Q.base[head]; Q.length--; }//DeCyQueue 3.31 int Palindrome_Test()//判别输入的字符串是否回文序列,是则返回 1,否则返回 0 { InitStack(S);InitQueue(Q); while((c=getchar()!='@') { Push(S,c);EnQueue(Q,c); //同时使用栈和队列两种结构 } while(!StackEmpty(S)) { Pop(S,a);DeQueue(Q,b)); if(a!=b) return ERROR; } return OK; }//Palindrome_Test 3.32 void GetFib_CyQueue(int k,int n)//求 k 阶斐波那契序列的前 n+1 项 { InitCyQueue(Q); //其 MAXSIZE 设置为 k for(i=0;i<k-1;i++) Q.base[i]=0; Q.base[k-1]=1; //给前 k 项赋初值 for(i=0;i<k;i++) printf("%d",Q.base[i]); for(i=k;i<=n;i++) { m=i%k;sum=0; for(j=0;j<k;j++) sum+=Q.base[(m+j)%k]; Q.base[m]=sum; //求第 i 项的值存入队列中并取代已无用的第一项 printf("%d",sum);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有