DS03-PE22
/**********
【题目】试写一算法,借助辅助栈,复制顺序?/p>
S1
得到
S2
?/p>
顺序栈的类型定义为:
typedef struct {
ElemType *elem; //
存储空间的基址
int top;
//
栈顶元素的下一个位置,简称栈顶位?/p>
int size;
//
当前分配的存储容?/p>
int increment;
//
扩容时,增加的存储容?/p>
} SqStack;
//
顺序?/p>
可调用顺序栈接口中下列函数:
Status InitStack_Sq(SqStack &S, int size, int inc); //
初始化顺序栈
S
Status DestroyStack_Sq(SqStack &S); //
销毁顺序栈
S
Status StackEmpty_Sq(SqStack S);
//
?/p>
S
判空,若空则返回
TRUE
,否?/p>
FALSE
Status Push_Sq(SqStack &S, ElemType e); //
将元?/p>
e
压入?/p>
S
Status Pop_Sq(SqStack &S, ElemType &e); //
?/p>
S
的栈顶元素出栈到
e
***********/
Status CopyStack_Sq(SqStack S1, SqStack &S2)
/*
借助辅助栈,复制顺序?/p>
S1
得到
S2
?/p>
*/
/*
若复制成功,则返?/p>
TRUE
;否?/p>
FALSE
?/p>
*/
{
//
if( TRUE==StackEmpty_Sq(S1) ) return FALSE;//
?/p>
S1
是空的时?/p>
SqStack S3;
if( ERROR==InitStack_Sq( S2,S1.size,S1.increment ) ) return FALSE;
if( ERROR==InitStack_Sq( S3,S1.size,S1.increment ) ) return FALSE;//
?/p>
S1
?/p>
S2
的初始化
if( !StackEmpty_Sq(S2) )return FALSE;
if( !StackEmpty_Sq(S3) )return FALSE;//
?/p>
S1
?/p>
S2
的判?/p>
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