Stack表示对象的最后进先出集合。 当您需要项目的最后进入,首先访问时使用。 当您在列表中添加项目时,称为推送项目,当您删除它时,它被称为弹出项目。
堆栈类的属性和方法
属性 | 描述 |
---|---|
Count | Gets the number of elements contained in the Stack. 获取堆栈中包含的元素数。 |
S.N | 方法名称和用途 |
---|---|
1 | Public Overridable Sub Clear Removes all elements from the Stack. 从堆栈中删除所有元素。 |
2 | Public Overridable Function Contains (obj As Object) As Boolean Determines whether an element is in the Stack. 确定元素是否在堆栈中。 |
3 | Public Overridable Function Peek As Object Returns the object at the top of the Stack without removing it. 返回堆栈顶部的对象,而不删除它。 |
4 | Public Overridable Function Pop As Object Removes and returns the object at the top of the Stack. 删除并返回堆栈顶部的对象。 |
5 | Public Overridable Sub Push (obj As Object) Inserts an object at the top of the Stack. 在堆栈顶部插入一个对象。 |
6 | Public Overridable Function ToArray As Object() Copies the Stack to a new array. 将堆栈复制到新数组。 |
示例:
- Module collections
- Sub Main()
- Dim st As Stack = New Stack()
- st.Push("A")
- st.Push("M")
- st.Push("G")
- st.Push("W")
- Console.WriteLine("Current stack: ")
- Dim c As Char
- For Each c In st
- Console.Write(c + " ")
- Next c
- Console.WriteLine()
- st.Push("V")
- st.Push("H")
- Console.WriteLine("The next poppable value in stack: {0}", st.Peek())
- Console.WriteLine("Current stack: ")
- For Each c In st
- Console.Write(c + " ")
- Next c
- Console.WriteLine()
- Console.WriteLine("Removing values ")
- st.Pop()
- st.Pop()
- st.Pop()
- Console.WriteLine("Current stack: ")
- For Each c In st
- Console.Write(c + " ")
- Next c
- Console.ReadKey()
- End Sub
- End Module
- Current stack:
- W G M A
- The next poppable value in stack: H
- Current stack:
- H V W G M A
- Removing values
- Current stack:
- G M A
转载本站内容时,请务必注明来自W3xue,违者必究。