通用链表中的队列

时间:2015-06-20 12:20:32

标签: c# list generics queue

我是新手并尝试为队列编写此代码,但它不起作用。我被困了,我为一个链表写了队列,我找不到我错在哪里,任何帮助都很棒。先感谢您。

class Queue
{
    public Node<T> head { get; set; }
    public Node<T> tail { get; set; }
    public int size { get; set; }

    public Queue()
    {
        head = null;
        tail = null;
    }

    public void Enqueue(T value)
    {
        if (head == null)
        {
            head = new Node<T>(value);
            tail = head;
        }
        else
        {
            tail.sljedeci = new Node<T>(value);
            tail = tail.next;
        }
        size++;
    }

    public T Dequeue()
    {
        if (size == 0)
        {
            throw new IndexOutOfRangeException();
        }
        else if (size == 1)
        {
            T value = head.value;
            size--;
            head = tail = null;
            return value;
        }
        else
        {
            T value = head.value;
            head = head.next;
            size--;
            return value;
        }
    }

    public void Peek()
    {
        if (size == 0)
        {
            Console.WriteLine("List is empty");
        }
        else
        {
            Node<T> temp = head;
            Console.Write("{ ");
            while (temp.next != null)
            {
                Console.Write("{0}, ", temp.value);
                temp = temp.next;
            }
            Console.Write(temp.valuet + " }");
        }
    }

0 个答案:

没有答案