第一:定义一个实现栈的接口:
/// <summary>
/// 栈接口,栈抽象数据类型
/// </summary>
public interface IStack<T>
{
/// <summary>
/// 是否为空栈
/// </summary>
/// <returns></returns>
bool IsEmpty();
/// <summary>
/// 取出栈顶元素,未出栈
/// </summary>
T Peek();
/// <summary>
/// 出栈
/// </summary>
/// <returns></returns>
T Pop();
/// <summary>
/// 入栈
/// </summary>
/// <param name="item">元素</param>
/// <returns></returns>
void Push(T item);
}
第二:链式栈数据结构:
/// <summary>
/// 构建链栈指针
/// </summary>
/// <typeparam name="T"></typeparam>
public class Node<T>
{
/// <summary>
/// 数据域 保存数据元素
/// </summary>
public T Data;
/// <summary>
/// 后继结点指针
/// </summary>
public Node<T> Next;
public Node(T data, Node<T> next) {
this.Data = data;
this.Next = next;
}
}
/// <summary>
/// 自定义数据结构 链式栈
/// </summary>
/// <typeparam name="T">T</typeparam>
public class LinkedStack<T> : IStack<T>
{
private int size;
private object syncRoot;
/// <summary>
/// 节点指针
/// </summary>
private Node<T> top;
/// <summary>
/// 构造一个空的链栈
/// </summary>
public LinkedStack()
{
this.top = null;
}
/// <summary>
/// 判断一个链栈是否为空 True 为空
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
return this.top == null;
}
/// <summary>
/// 取出栈顶元素 若栈为空返回null
/// </summary>
public T Peek()
{
return this.top == null ? default(T) : this.top.Data;
}
/// <summary>
/// 出栈 若栈为空返回null
/// </summary>
/// <returns></returns>
public T Pop()
{
if (this.top == null)
return default(T);
T temp = this.top.Data; //取栈顶结点元素
this.top = this.top.Next; //删除栈顶结点
size--;
return temp;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="item">元素</param>
public void Push(T item)
{
if (item != null)
{
this.top = new Node<T>(item, top);
size++;
}
}
/// <summary>
/// 获取链栈元素数
/// </summary>
public int Count
{
get { return this.size; }
}
/// <summary>
/// 是否是线程安全的
/// </summary>
public bool IsSynchronized
{
get {
return false;
}
}
/// <summary>
/// 同步访问对象
/// </summary>
public object SyncRoot
{
get {
if (this.syncRoot == null)
Interlocked.CompareExchange<object>(ref this.syncRoot, new object(), null);
return this.syncRoot;
}
}
/// <summary>
/// 找出链栈相同元素
/// </summary>
/// <param name="value">value</param>
/// <returns></returns>
public Node<T> Find(T value)
{
Node<T> head = this.top;
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
if (head != null)
{
if (value != null)
{
do
{
if (head != null)
{
if (comparer.Equals(head.Data, value))
{
return head;
}
head = head.Next;
}
else
{
return null;
}
}
while (head != this.top);
}
else
{
do
{
if (head != null)
{
if (head.Data == null)
{
return head;
}
head = head.Next;
}
else {
return null;
}
}
while (head != this.top);
}
}
return null;
}
/// <summary>
/// 对象比较
/// </summary>
/// <param name="item">item</param>
/// <returns></returns>
public bool Contains(T item)
{
return (this.Find(item) != null);
}
/// <summary>
/// 清空链栈
/// </summary>
public void Clear()
{
Node<T> head = this.top;
while (head != null)
{
Node<T> node1 = head;
head = head.Next;
node1.Next = null;
}
this.top = null;
this.size = 0;
}
}
第三:封装弱引用对象池
/// <summary>
/// 构造弱引用对象池
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class WeakObjectPool<T> where T : class
{
private WeakReference stackRef;
/// <summary>
/// 获取资源数
/// </summary>
public int Size
{
get
{
return this.LinkedStack.Count;
}
}
/// <summary>
/// 是否是线程安全的
/// </summary>
public bool IsSynchronized
{
get { return true; }
}
/// <summary>
/// 获取同步访问对象
/// </summary>
public object SyncRoot
{
get { return stackRef; }
}
private LinkedStack<T> LinkedStack
{
get
{
if (stackRef.IsAlive)
{
return stackRef.Target as LinkedStack<T>;
}
else
{
LinkedStack<T> linkedStack = new LinkedStack<T>();
lock (SyncRoot)
{
stackRef.Target = linkedStack;
}
return linkedStack;
}
}
}
public WeakObjectPool()
{
stackRef = new WeakReference(new LinkedStack<T>());
}
/// <summary>
/// 将对象放入池中
/// </summary>
/// <param name="t">T</param>
public void Entrance(T t)
{
if (t != null)
{
LinkedStack<T> linkedStack = this.LinkedStack;
if (!linkedStack.Contains(t))
{
lock (linkedStack)
{
if (!linkedStack.Contains(t))
{
linkedStack.Push(t);
}
}
}
}
}
/// <summary>
/// 取出池中顶部对象.移除对象
/// </summary>
/// <returns>T</returns>
public T Export()
{
LinkedStack<T> stack = this.LinkedStack;
if (stack.Count > 0)
{
lock (SyncRoot)
{
if (stack.Count > 0)
{
return stack.Pop();
}
}
}
return default(T);
}
/// <summary>
/// 取栈顶对象.对象不移除
/// </summary>
/// <returns></returns>
public T Fetch()
{
LinkedStack<T> stack = this.LinkedStack;
if (stack.Count > 0)
{
lock (SyncRoot)
{
if (stack.Count > 0)
{
return stack.Peek();
}
}
}
return default(T);
}
/// <summary>
/// 清空资源
/// </summary>
public void Clear()
{
LinkedStack<T> stack = this.LinkedStack;
if (stack.Count > 0)
{
lock (SyncRoot)
{
stack.Clear();
}
}
}
}