【万丈高楼平地起 第二季 队列和栈】

接上一篇 【万丈高楼平地起 第一季 链表是怎样链成的】,本文是基于上一篇链表来操作队列和栈的。

二、栈

1.        堆栈是受约束的链表——堆栈只在栈顶(top)获取新节点和释放节点。因此,堆栈是一种后进先出(LIFO)的数据结构。底部(最后一个)节点的链接成员设成null表示栈底。

2.        堆栈的基本操作是压入(push)和弹出(pop)。压入操作是在堆栈顶部添加一个节点。弹出操作是从堆栈顶删除一个节点并从被弹出的节点返回被删除的数据。

3.        C#中命名空间System.Collections下,包含了用于实现和操作堆栈的Stack类,在程序运行时,该类可改变大小。

     我们来实现自己的Stack类。

首先新建一个控制台程序,

接着新建一个StackInheritance.cs类文件,写Stack类的方法和属性,继承自List类。

using System;
using LinkedListLibrary;

namespace StackInheritanceLibrary
{
public class StackInheritance : List
{
public StackInheritance()
:
base("stack")
{
}

// 将dataValue压入栈
public void Push(object dataValue)
{
InsertAtFront(dataValue);
}

// 出栈
public object Pop()
{
return RemoveFromFront();
}
}
}

   

测试代码:

using System;
using LinkedListLibrary;
using StackInheritanceLibrary;
using QueueInheritanceLibrary;

namespace ListTest
{
class ListTest
{
// 测试 stack
static void Main(string[] args)
{
StackInheritance stack
= new StackInheritance();

// 新建一些测试数据
bool aBoolean = true;
char aChar = '$';
int anInteger = 34567;
string aString = "hello";

// use method Push to add items to stack
stack.Push(aBoolean);
stack.Print();
stack.Push(aChar);
stack.Print();
stack.Push(anInteger);
stack.Print();
stack.Push(aString);
stack.Print();

// use method Pop to remove items from stack
try
{
while (true)
{
object removeObject = stack.Pop();
Console.WriteLine(removeObject
+ " poped.");
stack.Print();
}
}
catch (EmptyListException emptyListException)
{
Console.Error.WriteLine(emptyListException.StackTrace);
}

Console.ReadKey();
}

}
}

   

三、队列

1.        队列(Queue):顾名思义,意味着排队等候。通常,在队列的后端(称作“尾”)插入,前端(称作“头”)删除。

2.        队列中的节点只能从头部移除,从末尾插入。因此,队列是一个先进先出(FIFO)的数据结构。

3.        插入和删除操作分别成为入列(Enqueue)和出列(Dequeue)。

队列的用途有很多,比如打印机打印、计算机网络中消息排队等。

 

QueueInheritance.cs类也是继承自List类。

using System;

using LinkedListLibrary;

 

namespace QueueInheritanceLibrary

{

    public class QueueInheritance : List

    {

        public QueueInheritance()

            : base("queue")

        {

        }

 

        // dataValue加入队列

        public void Enqueue(object dataValue)

        {

            InsertAtBack(dataValue);

        }

 

        // 出队

        public object Dequeue()

        {

            return RemoveFromFront();

        }

    }

}

 

测试代码 Program.cs:

using System;
using LinkedListLibrary;
using StackInheritanceLibrary;
using QueueInheritanceLibrary;

namespace ListTest
{
class ListTest
{
// 测试 Queue
static void Main(string[] args)
{
QueueInheritance queue
= new QueueInheritance();

// 新建一些测试数据
bool aBoolean = true;
char aChar = '$';
int anIntger = 34567;
string aString = "hello";

// use method Enqueue to add items to queue
queue.Enqueue(aBoolean);
queue.Print();
queue.Enqueue(aChar);
queue.Print();
queue.Enqueue(anIntger);
queue.Print();
queue.Enqueue(aString);
queue.Print();

// use method Dequeue to remove items from queue
object removeObject = null;
try
{
while (true)
{
removeObject
= queue.Dequeue();
Console.WriteLine(removeObject
+ " dequeue");
queue.Print();
}
}
catch (EmptyListException emptyListException)
{
Console.Error.WriteLine(emptyListException.StackTrace);
}


Console.ReadKey();
}
}
}

  

 

posted on 2011-09-14 16:22  Frank.Fan  阅读(1159)  评论(0编辑  收藏  举报