1

所以我有我的代码在链接列表版本中实现队列。一切正常,但显示方法。我花了几天时间试图找出正确的方法来显示我的队列中节点的内容,而不修改节点或队列本身。

这是我的代码:

public class QueueNode
    {        
        private Object bike;
        private QueueNode next;

        public QueueNode(Object bike)
        {
            this.bike = bike;
            next = null;            
        }

        public Object Bike //Content
        {
            get
            {
                return bike;
            }
            set
            {
                bike = value;
            }
        }

        public QueueNode Next //Pointer
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }

        
    }  // end of QueueNode

// This class inherits the interface IQueue, that uses these methods, not relevant here. 
    public class CircularQueue : IQueue 
    {
        private int capacity = Int32.MaxValue;
        private int count = 0;
        private QueueNode tail = null; //Node

        public int Capacity
        {
            get
            {
                return capacity;
            }
        }

        public int Count
        {
            get
            {
                return count;
            }
        }

        public bool IsEmpty()
        {
            return count == 0;
        }


        public bool IsFull()
        {
            return count == capacity;
        }

        public void Enqueue(Object item)
        {
            // check the pre-condition
            if (!IsFull())
            {
                QueueNode aNode = new QueueNode(item);
                if (count == 0) //special case: the queue is empty
                {
                    tail = aNode;
                    tail.Next = tail;
                }
                else //general case
                {
                    aNode.Next = tail.Next;
                    tail.Next = aNode;
                    tail = aNode;
                }
                count++;
            }
        }

        public Object Dequeue()
        {
            // check the pre-condition
            if (!IsEmpty())
            {
                Object data;
                if (count == 1) //special case: the queue has only 1 item
                {
                    data = tail.Bike;
                    tail = null;
                }
                else //general case
                {
                    data = tail.Next.Bike;
                    tail.Next = tail.Next.Next;
                }
                count--;
                return data;
            }
            else
                return null;
        }

我的问题:我创建了这段代码来显示节点的内容(并将它们全部显示),但它最终修改了内容,在任务结束时在每个节点中放置了相同的值。

        /*public void DisplayBikes()
        {
            if (!IsEmpty())
            {
                Object data;
                for(int i = 0; i <count; i++)
                {
                    data = tail.Next.Bike;
                    Console.WriteLine(data);                    
                    tail.Next = tail.Next.Next;
                }                
            }
            else
                Console.WriteLine("Sorry. There are no Bikes available");
        }*/

然后我有点冒险,并创建了一个临时节点来显示我队列中所有节点的内容,但最终变得一团糟。我收到这条消息:

System.NullReferenceException:“对象引用未设置为对象的实例。”

尾巴为空。

所以,在这一点上,我对如何做到这一点的想法为零。我知道我已经很接近了,但是我不知道我在这里缺少什么,以使此代码打印我的节点的内容....请帮助!


        public void DisplayBikes()
        {
            int c = count;
            QueueNode temp = tail.Next;
            if(!isFull())
            {
              while(c > 0)
              {                
                  Console.WriteLine(temp.Bike);
                  temp = tail.Next.Next;
                  c--;
              }
            }
            else
                Console.WriteLine("Sorry. There are no Bikes available");
        }
4

1 回答 1

0

自己解决了

public void DisplayBikes()
        {
            int c = count;
            QueueNode temp = tail.Next;
            if(!isFull())
            {
              while(c > 0)
              {                
                  Console.WriteLine(temp.Bike);
                  temp = temp.Next; //Here. I needed to iterate over the same node
                  c--;
              }
            }
            else
                Console.WriteLine("Sorry. There are no Bikes available");
        }

就是这样。没有人敢帮忙....所以,谢谢你们> :(

于 2021-06-19T02:53:28.150 回答