Anyview 第四节 15年数据结构.docVIP

  • 8
  • 0
  • 约9.93千字
  • 约 12页
  • 2017-07-08 发布于湖北
  • 举报
DS03-PE22 /********** 【题目】试写一算法,借助辅助栈,复制顺序栈S1得到S2。 顺序栈的类型定义为: typedef struct { ElemType *elem; // 存储空间的基址 int top; // 栈顶元素的下一个位置,简称栈顶位标 int size; // 当前分配的存储容量 int increment; // 扩容时,增加的存储容量 } SqStack; // 顺序栈 可调用顺序栈接口中下列函数: Status InitStack_Sq(SqStack S, int size, int inc); // 初始化顺序栈S Status DestroyStack_Sq(SqStack S); // 销毁顺序栈S Status StackEmpty_Sq(SqStack S); // 栈S判空,若空则返回TRUE,否则FALSE Status Push_Sq(SqStack S, ElemType e); // 将元素e压入栈S Status Pop_Sq(SqStack S, ElemType e); // 栈S的栈顶元素出栈到e ***********/ Status CopyStack_Sq(SqStack S1, SqStack S2) /* 借助辅助栈,复制顺序栈S1得到S2。 */ /* 若复制成功,则返回TRUE;否则FALSE。 */ { // if( TRUE==StackEmpty_Sq(S1) ) return FALSE;//当S1是空的时候 SqStack S3; if( ERROR==InitStack_Sq( S2,S1.size,S1.increment ) ) return FALSE; if( ERROR==InitStack_Sq( S3,S1.size,S1.increment ) ) return FALSE;//对S1、S2的初始化 if( !StackEmpty_Sq(S2) )return FALSE; if( !StackEmpty_Sq(S3) )return FALSE;//对S1、S2的判空 ElemType e; while(S1.top) { Pop_Sq(S1,e); Push_Sq(S3,e); } while(S3.top) { Pop_Sq(S3,e); Push_Sq(S2,e); } // DestroyStack_Sq(S1); // DestroyStack_Sq(S3); return TRUE; } DS03-PE37 /********** 【题目】假设将循环队列定义为:以域变量rear 和length分别指示循环队列中队尾元素的位置和内 含元素的个数。试给出此循环队列的队满条件,并 写出相应的入队列和出队列的算法(在出队列的算 法中要返回队头元素)。 本题的循环队列CLenQueue的类型定义如下: typedef struct { ElemType elem[MAXQSIZE]; int length; int rear; } CLenQueue; **********/ Status EnCQueue(CLenQueue Q, ElemType x) /* 将元素x加入队列Q,并返回OK;*/ /* 若失败,则返回ERROR。 */ { if( MAXQSIZE=Q.length ) return ERROR;//队满,无法插入 Q.rear = (Q.rear+1)%MAXQSIZE; Q.elem[Q.rear]=x; Q.length+=1; return OK; } Status DeCQueue(CLenQueue Q, ElemType x) /* 将队列Q的队头元素退队到x,并返回OK;*/ /* 若失败,则返回ERROR。 */ { if( 0==Q.length ) return ERROR; x=Q.elem[((Q.rear+MAXQSIZE-Q.length+1)%MAXQSIZE)]; Q.length-=1; return OK; } DS03-PE44 /********** 【题目】已知k阶斐波那契序列的定义为:

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档