本文共 2255 字,大约阅读时间需要 7 分钟。
interface IQueue{ int GetLength(); void Clear(); void EnQueue(T elem); void Dequeue(); T Peek(); bool IsEmepty();
////// 链队列 /// ///class LinkQueue : IQueue { private int count; //元素个数 private Node front;//头指针 private Node rear;//尾指针
public LinkQueue() { count = 0; front = rear = new Node(); //初始化指向头结点 } public void Clear() { count = 0; front.Next = null; rear = null; }
//对头出队 public T Dequeue() { //判断是否为空队 //对头指针改变 //count-- if (rear==front) { throw new Exception("空队无法删除"); } Nodep = front.Next; front.Next = p.Next; count--; return p.Data; }
//队尾入队 public void EnQueue(T elem) { NodenewNode=new Node (elem); //把新的节点添加到队尾 //改变尾指针的指向地址 //count++ rear.Next = newNode; rear = newNode; count++; } public int GetLength() { return count; }
public bool IsEmepty() { return front == rear; }
public T Peek() { Nodep = front.Next; return p.Data; } void IQueue .Dequeue() { } }
class Program { static void Main(string[] args) { //Queue sum=new Queue (6); LinkQueue sum=new LinkQueue (); Console.WriteLine(sum.IsEmepty()); Console.WriteLine(sum.GetLength()); sum.EnQueue(100); sum.EnQueue(200); sum.EnQueue(300); sum.EnQueue(400); Console.WriteLine(sum.Dequeue()); Console.WriteLine(sum.Peek()); Console.WriteLine(sum.IsEmepty()); sum.Clear(); Console.WriteLine(sum.GetLength()); Console.ReadKey(); } }
转载地址:http://odrxo.baihongyu.com/