using System;
using System.Collections.Generic;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int length = 100000;
Stack<int> stack = new Stack<int>(length);
for (int i = 0; i < length; i++)
{
stack.AddItem(i);
}
for (int i = 0; i < length; i++)
{
stack.OutputItem();
}
Console.ReadKey();
}
}
class Stack<T>
{
private T[] container;
private int count = 0;
public void AddItem(T item)
{
if(count<container.Length)
container[count++] = item;
}
public void OutputItem()
{
if(count>0)
Console.WriteLine(container[--count]);
}
public Stack(int length)
{
container = new T[length];
}
}
}
本文介绍了一个简单的泛型栈的实现过程,并演示了如何使用该栈进行元素的压入与弹出操作。通过具体的代码示例,展示了栈的基本原理及其在实际编程中的应用。
223

被折叠的 条评论
为什么被折叠?



