代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
namespace DataStructures.Lists
{
/// <summary>
/// The Stack (LIFO) Data Structure.
/// </summary>
/// <typeparam name="T">Type</typeparam>
public class Stack<T> : IEnumerable<T> where T : IComparable<T>
{
/// <summary>
/// Instance variables.
/// _collection: Array-Based List.
/// Count: Public Getter for returning the number of elements.
/// </summary>
private ArrayList<T> _collection { get; set; }
public int Count { get { return _collection.Count; } }
/// <summary>
/// CONSTRUCTORS
/// </summary>
public Stack()
{
// The internal collection is implemented as an array-based list.
// See the ArrayList.cs for the list implementation.
_collection = new ArrayList<T>();
}
public Stack(int initialCapacity)
{
if (initialCapacity < 0)
{
throw new ArgumentOutOfRangeException();
}
// The internal collection is implemented as an array-based list.
// See the ArrayList.cs for the list implementation.
_collection = new ArrayList<T>(initialCapacity);
}
/// <summary>
/// Checks whether the stack is empty.
/// </summary>
/// <returns>True if stack is empty, false otherwise.</returns>
public bool IsEmpty
{
get
{
return _collection.IsEmpty;
}
}
/// <summary>
/// Returns the top element in the stack.
/// </summary>
public T Top
{
get
{
try
{
return _collection[_collection.Count - 1];
}
catch (Exception)
{
throw new Exception("Stack is empty.");
}
}
}
/// <summary>
/// Inserts an element at the top of the stack.
/// </summary>
/// <param name="dataItem">Element to be inserted.</param>
public void Push(T dataItem)
{
_collection.Add(dataItem);
}
/// <summary>
/// Removes the top element from stack.
/// </summary>
public T Pop()
{
if (Count > 0)
{
var top = Top;
_collection.RemoveAt(_collection.Count - 1);
return top;
}
throw new Exception("Stack is empty.");
}
/// <summary>
/// Returns an array version of this stack.
/// </summary>
/// <returns>System.Array.</returns>
public T[] ToArray()
{
return _collection.ToArray();
}
/// <summary>
/// Returns a human-readable, multi-line, print-out (string) of this stack.
/// </summary>
/// <returns>String.</returns>
public string ToHumanReadable()
{
return _collection.ToHumanReadable();
}
/********************************************************************************/
public IEnumerator<T> GetEnumerator()
{
for (int i = _collection.Count - 1; i >= 0; --i)
yield return _collection[i];
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。