如何用vb语言编写栈的入栈与出栈?

来源:百度知道 编辑:UC知道 时间:2024/06/17 09:19:56
栈只能通过栈顶进行插入和删除,并且栈的数据符合"先进先出,后进后出"的原则.

VB的栈麻烦多了 建议不要用
C++ 的pop、push很容易的……

栈具体内容不记得了 呵呵 凭借记忆写得:
dim stack() as string

sub push(str as string) as string '入栈
dim ub as long
ub = ubound(stack)
redim preserve stack(ub+1)
stack(ub+1)=str
end sub

function pop() as string '出栈
dim ub as long
ub = ubound(stack)
pop = stack(ub)
redim preserve stack(ub-1)
end function

function top() as string '读取顶元素不修改栈
dim ub as long
ub = ubound(stack)
top = stack(ub)
end function

用个集合吧,集合不限制栈顶,使用好方便的

public stk as new collection

private sub push(v)
stk.add v
end sub

private function pop()
if stk.count>0 then
pop=stk(stk.count)
stk.remove stk.count
end if
end function

下面是测试代码
Private Sub Command1_Click()
For i = 1 To 10
push i
Next
For i = 1 To 10
Debug.Print pop
Next
End Sub