本文介绍了哪个是更好的链表实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My lecturer has this implementation of a Linked List. The data object is the representation of a boat but the data object in the structure seems tightly coupled from my pov because data object should be separate from the linked list.







class Boatnode


    {
        private string Owner;          // owners name
        private string Boatname;       // name of boat
        private string Boattype;        //type of boat
        private int Boatlength;         //length of boat
        private Boatnode next;            // link to next

        public Boatnode(string owner , string boatname, string boattype ,int boatlength)
        {
            Owner = owner;  // store name
            Boatname = boatname;
            Boattype = boattype;
            Boatlength = boatlength;
            next = null;                  // initialise next
        }

        public void setNext(Boatnode nextNode)
        {
            next = nextNode;  // change next node
        }

        public Boatnode getNext()
        {
            return next;
        }

        public string getOwner()
        {
            return Owner;
        }
        public string getBoatName()
        {
        return Boatname;
         }
        public string getBoatType()
        {
        return Boattype;
         }
        public int getBoatLength()
        {
        return Boatlength;
         }
} // end class TownNode





我尝试了什么:





What I have tried:

I proposed this solution instead but he rejected it saying that the boat object was not properly encapsulated







public class Node
{
    public   Boat Data;
    public   Node Next;
    public  Node(Boat data)
    {
        Data = data;
    }
}

public class Queue
{
    private Node _head;
    private Node _tail;
    private int _count = 0;
    public Queue()
    {
    }
    public void Enqueue(Boat data)
    {
        Node _newNode = new Node(data);
        if (_head == null)
        {
            _head = _newNode;
            _tail = _head;
        }
        else
        {
            _tail.Next = _newNode;
            _tail = _tail.Next;
        }
        _count++;
    }
    public Boat Dequeue()
    {
        Boat _result = new MarinaBerthClassLibrary.Boat();
        if (_head == null)
        {
            throw new Exception("Queue is Empty");
        }
         _result = _head.Data;
        _head = _head.Next;
        return _result;
    }
    public int Count
    {
        get
        {
            return this._count;
        }
    }

    public string PrintElements()
    {
        var node = _head;
        string[] elements = new string[_count];
        int i = 0;
        if (node!=null)
        {
            while (node != null)
            {
                elements[i++] = node.Data.NameOfBoat;
                node = node.Next;
            }
            return string.Join(" ", elements);
        }

        return ("No Data");
    }
}







What would be a better implementation of this?

推荐答案

Boat _result = new MarinaBerthClassLibrary.Boat();





应该是





should just be

Boat _result;





你不必要在你的船上创建一个船的实例代码。



You're needlessly creating an instance of Boat in your code.



private Boat _Data = null;
public Boat Data
   {
   get { return _Data; }
   private set { _Data = value; }
   }

这样,您就可以保留对节点内容的控制权。

That way, you retain control over the node content.


这篇关于哪个是更好的链表实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:14