LinkedQueue的实现

发表于:2007-06-30来源:作者:点击数: 标签:
public class LinkedQueue { private class Node { public object Data; public Node Link; } private Node front; private Node rear; public LinkedQueue() { } public bool IsEmpty { get { return front == null; } } public bool IsFull { get { Node p;
public class LinkedQueue
{
        private class Node
        {
                public object Data;
                public Node Link;
        }
  
        private Node front;
        private Node rear;
  
        public LinkedQueue()
        {
        }
  
        public bool IsEmpty
        {
                get
                {
                        return front == null;
                }
        }
  
  
        public bool IsFull
        {
                get
                {
                        Node p;
  
                        try
                        {
                                p = new Node();
                                return false;
                        }
                        catch
                        {
                                return true;
                        }
                }
        }
  
        public object First
        {
                get
                {
                        if(IsEmpty)
                        {
                                throw new Exception("Queue is empty.");
                        }
  
                        return front.Data;
                }
        }
  
        public object Last
        {
                get
                {
                        if(IsEmpty)
                        {
                                throw new Exception("Queue is empty.");
                        }
  
                        return rear.Data;
                }
        }
  
        public LinkedQueue Add(object x)
        {
                Node p;
  
                // create node for new element
                p = new Node();
                p.Data = x;
                p.Link = null;
  
                if(front != null) // queue not empty
                {
                        rear.Link = p;
                }
                else // queue empty
                {
                        front = p;
                }
  
                rear = p;
  
                return this;
        }
  
        public object Delete()
        {
                Node p;
                object x;
  
                if(IsEmpty)
                {
                        throw new Exception("queue is empty.");
                }
  
                        // save element in first node
                x = front.Data;

  
                // delete first node
                p = front;
                front = front.Link;
                p = null;
  
                return x;
        }

原文转自:http://www.ltesting.net