栈的定义和概念
栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表。
(
1
)通常称插入、删除的这一端为栈顶(Top),另一端称为栈底(Bottom)。
(
2
)当表中没有元素时称为空栈。
(
3
)栈为后进先出(Last In First Out)的线性表,简称为LIFO表。
栈的修改是按后进先出的原则进行。每次删除(退栈)的总是当前栈中
"
最新
"
的元素,即最后插入(进栈)的元素,而最先插入的是被放在栈的底部,要到最后才能删除。
栈在C#中的代码实现
using
System;
namespace
EveryDayStudy.数据结构
{
public
class
DAPStack
{
private
object
[] _array;
private
const
int
_defaultCapacity
=
10
;
private
int
_size;
public
DAPStack()
{
_array
=
new
object
[_defaultCapacity];
_size
=
0
;
}
public
DAPStack(
int
initialCapacity)
{
if
(initialCapacity
<
0
)
{
throw
new
ArgumentOutOfRangeException(
"
栈空间不能小于零
"
);
}
if
(initialCapacity
<
_defaultCapacity)
{
initialCapacity
=
_defaultCapacity;
}
_array
=
new
object
[initialCapacity];
_size
=
0
;
}
public
virtual
object
Pop()
{
if
(_size
==
0
)
{
throw
new
InvalidOperationException(
"
栈内已经没有数据了。
"
);
}
object
obj2
=
_array[
--
this
._size];
_array[_size]
=
null
;
return
obj2;
}
public
virtual
void
Push(
object
obj)
{
if
(_size
==
_array.Length)
{
object
[] destinationArray
=
new
object
[
2
*
_array.Length];
_array
=
destinationArray;
}
_array[_size
++
]
=
obj;
}
public
virtual
int
Count
{
get
{
return
_size;
}
}
}
}
其他
其他的好像没有什么好主意的了,大概是因为栈这个结构在我们上学的时候接触的太多了,还是这个栈本来就好理解吧。