//@file stack.cs
//功能:数组构造堆栈stack
//VS2005 complied ok with chinayaosir
using System;
using System.Collections;//导入容器空间
//using System.Text;
namespace DS{
class Stack{
private int index_top;//指向堆栈顶部
private ArrayList list;
//0.1.构造函数
public Stack(){
list = new ArrayList();
index_top = -1;
}
//0.2元素个数
public int count{
get { return list.Count; }
}
//0.3清空堆栈
public void clear(){
list.Clear();
index_top = -1;
}
//1.push()
public void push(object item){
list.Add(item);
index_top++;
}
//2.pop()
public object pop(){
object obj = list[index_top];
list.RemoveAt(index_top);
index_top--;
return obj;
}
//3.取栈顶元素
public object peek(){
return list[index_top];
}
//4.测试堆栈Main函数
static void Main(string[] args)
{
Stack slist = new Stack();
string w,word = "Hello-Stack";
for (int x = 0; x < word.Length; x++)
slist.push(word.Substring(x, 1));
//后进先出正向压入"Hello-Stack"
Console.WriteLine(word);
//后进先出反向弹出"kcatS-olleH"
while (slist.count > 0)
{
w= slist.pop().ToString();
Console.Write(w);
}
slist.clear();
Console.ReadLine();
}
}//class end
}//namespace end
/*
运行结果是:
Hello-Stack
kcatS-olleH
*/