现在的位置: 首页 > 综合 > 正文

c#数据结构———栈

2013年02月01日 ⁄ 综合 ⁄ 共 1369字 ⁄ 字号 评论关闭
栈是栈是受约束的链表,栈是一种后进先(LIFO)出的数据结构。

using System;

using LinkedListLibrary;

//栈是受约束的链表

//栈底结点的链接成员社为null

namespace StackInheritanceLibrary

{

     public class StackInheritance:List

     {

         public StackInheritance():base("stack")

         {

         }

         public void Push(object dataValue)//压栈

         {

              InsertAtFront(dataValue);

         }

         public object Pop()//出栈

         {

              return RemoveFromFront();

         }

     }

}

第二种实现方法

using System;

using LinkedListLibrary;

 

//通过合成来重用List

namespace StackCompositionLibrary

{

     public class StackComposition:List

     {

         public List stack;

         public StackComposition()

         {

              stack = new List("stack");

         }

         public void Push(object dataValue)

         {

              stack.InsertAtFront(dataValue);

         }

         public object Pop()

         {

              return stack.RemoveFromFront();

         }

         public bool IsEmpty()

         {

              return stack.IsEmpty();

         }

         public void Print()

         {

              stack.Print();

         }

     }

}

队列实现

using System;

using LinkedListLibrary;

namespace QueueInheritanceLibrary

{

     public class QueueInheritance:List

     {

         public QueueInheritance():base("queue")

         {

         }

         public void Enqueue(object dataValue)

         {

              InsertAtBack(dataValue);

         }

         public object Dequeue()

         {

              return RemoveFromFront();

         }

     }

}

 

【上篇】
【下篇】

抱歉!评论已关闭.