C#版栈

 

在前面的文章中我们实现了链表,下面给大家一个C#语言栈的链式实现,类中的每个方法都有注释,

有不明白的问题,大家给我发EMAIL: warensoft@163.com  

代码
using System;

using System.Collections.Generic;

using System.Text;



namespace 链式栈

{

/// <summary>

/// 用于表示栈中的一个结点

/// </summary>

public class CSStackNode:IDisposable

{

private object element;

/// <summary>

/// 用于表示该结点中存储的元素值

/// </summary>

public object Element

{

get { return element; }

set { element = value; }

}



private CSStackNode nextNode;

/// <summary>

/// 用于表示对下一个结点的引用

/// </summary>

public CSStackNode NextNode

{

get { return nextNode; }

set { nextNode = value; }

}





#region IDisposable 成员



public void Dispose()

{

this.element = null;

}



#endregion

}









/// <summary>

/// 用于表示栈

/// </summary>

public class CSStack

{

/// <summary>

/// 用于指向栈顶元素

/// </summary>

private CSStackNode top = null;









/// <summary>

/// 构造方法

/// </summary>

public CSStack()

{

this.InitStack();

}





private int count;

/// <summary>

/// 获取 Stack 中包含的元素数。

/// </summary>

public int Count

{

get

{

return this.count ;

}

}



private void InitStack()

{

//在初始化的时候让栈顶和栈底指向同一个位置

if (this.top !=null)

{

CSStackNode tmpnode
= this.top.NextNode;

//释放所有占用的资源

while (tmpnode != null)

{

CSStackNode node
= tmpnode.NextNode;

tmpnode.NextNode
= null;

tmpnode.Dispose();



tmpnode
= node;



}

this.top.NextNode = null;

this.top.Dispose();

}

this.top = new CSStackNode();

this.count = 0;

}

/// <summary>

/// 从 Stack 中移除所有对象。

/// </summary>

public void Clear()

{

this.InitStack();

}



/// <summary>

/// 确定某元素是否在 Stack 中。

/// </summary>

/// <param name="obj">要在 Stack 中查找的 Object。</param>

/// <returns>如果在 Stack 中找到 obj,则为 true;否则为 false。</returns>

public bool Contains(object obj)

{

CSStackNode tmpnode
= this.top;

while (tmpnode !=null )

{

if (tmpnode .Equals (obj ))

{

return true;

}

tmpnode
= tmpnode.NextNode;

}

return false;

}



/// <summary>

/// 返回位于 Stack 顶部的对象但不将其移除。

/// </summary>

/// <returns>位于 Stack 顶部的 Object。</returns>

public object Peek()

{

return this.top.Element;

}



/// <summary>

/// 移除并返回位于 Stack 顶部的对象。

/// </summary>

/// <returns>从 Stack 的顶部移除的 Object。</returns>

public object Pop()

{

if (this.count ==0)

{

return null;

}



CSStackNode node
= this.top;

this.top = this.top.NextNode;

this.count--;

return node.Element;

}



/// <summary>

/// 将对象插入 Stack 的顶部。

/// </summary>

/// <param name="obj">要推入到 Stack 中的 Object。</param>

public void Push(object obj)

{

if (this.count ==0)

{

this.top.Element = obj;

this.count = 1;



}

else

{

CSStackNode node
= new CSStackNode();

node.Element
= obj;

node.NextNode
= this.top;

this.top = node;

this.count++;

}

}



/// <summary>

/// 将 Stack 复制到新数组中。

/// </summary>

public object[] ToArray()

{

object []list=new object [this.count ];

CSStackNode node
= this.top;

for (int i = 0; i < this.count ; i++)

{

list[i]
= node.Element;

node
= node.NextNode;

}

return list;

}

}

}

 

 

 

 

原文地址:https://www.cnblogs.com/warensoft/p/1788300.html