数据结构C实验报告
二零一八年
f一、题目要求
栈和队列1实验目标(1)理解与掌握栈和队列的表示与操作方法。(2)通过解决栈和队列的应用问题,训练学生解决实际编程问题的分析、设计等思维能力。2实验要求(1)利用两个顺序栈共享一个存储空间的设计,完成入栈、出栈和判断栈空的函数。(2)二项式ab
展开后,其系数构成杨辉三角形,利用队列实现打印杨辉三角形的前
行的算法。(选做)
二、设计思路
1.结构体定义
栈函数
templateclassElemType
classSeqStack两个顺序栈共享一个数组空间
protected
i
ttop1top2栈顶指针
i
tmaxSize栈最大容量
ElemTypeelems元素存储空间
public
SeqStacki
tsizeSIZE
virtualSeqStack
i
tGetLe
gthi
tico
st取栈长度,i为栈的编号
boolIsEmptyi
tico
st判断栈是否为空
void
Traversei
t
i
voidVisitco
st
ElemTypeco
st遍历函数,i为栈的编号
StatusPushi
tico
stElemTypee入栈,i为栈的编号
StatusTopi
tiElemTypeeco
st取栈顶元素
StatusPopi
tiElemTypee出栈(删除栈顶元素)
f杨辉三角templateclassElemTypeclassSeqprotected
ElemTypeelemsi
tfro
ti
treari
tmaxsizepublicSeqi
tsizeSIZEvirtualSeqStatusE
QueueElemTypeeStatusDelQueueElemTypeeStatusGetHeadElemTypeeco
stboolIsEmptyco
st2.思路描述、算法流程栈函数templateclassElemTypeSeqStackElemTypeSeqStacki
tsizemaxSizesizeifelemsNULLdeleteelems如果空间已被存储,释放已有空间elems
ewElemTypemaxSize分配新的内存空间top11top2maxSize
ftemplateclassElemTypeSeqStackElemTypeSeqStack
deleteelemstemplateclassElemTypei
tSeqStackElemTypeGetLe
gthi
tico
st
retur
i1top11maxSizetop2templateclassElemTypeboolSeqStackElemTypeIsEmptyi
tico
st
ifi1retur
top11elseretur
top2maxSizetemplateclassElemTypevoidSeqStackElemTypeTraversei
tivoidVisitco
stElemTypeco
stifi1fori
tjtop1j0j
Visitelemsjelse
ffori
tjtop2jmaxSizejVisitelemsj
templateclassElemTypeStatusSeqStackElemTypePushi
tico
stElemTypee
iftop11top2retur
OVER_FLOWelseifi1
top1elemstop1eelsetop2elemstop2eretur
SUCCESStemplateclassElemTypeStatusSeqStackElemTypeTopi
tiElemTypeeco
stifi1
fiftop11retur
UNDER_FLOW
elseeelemstop1elseiftop21
retur
UNDER_FLOWelse
eelemstop2retur
SUCCESStemplateclassElemTypeStatusSeqStackElemTypePopi
tiElemTypeeifi1iftop11
retur
UNDER_FLOWer