哪个是更好的链表实现? [英] Which is the better implementation of of a linked list?

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

问题描述

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?

推荐答案

更好是主观的。鉴于你的课程的目的是教你OOP概念,讲师的代码是更好的,因为它更适合他试图教你的东西。他的代码有一个船对象,也是一个链表。所有代码都在该类中,船不仅​​具有属性,而且具有列表的概念,即船也知道列表中的下一个。另一方面,您的代码是两个类。你有一个列表的通用实现队列,然后船和船不知道它是一个列表,船不知道其他船只。



在现实世界中,我们倾向于将对象与其存储机制分开,因此您使用List< Boat>,但是您不是在现实世界中编写真实代码,而是在学校学习OOP的概念,讲师的代码是以对象为导向的,而你的代码却不是。



另外这个



"Better" is subjective. Given the aim of your class is to teach you OOP concepts the lecturer's code is "better" as it better fits what he is trying to teach you. His code has a boat object that is also a linked list. All of the code is within that class, the boat not only has properties but it has the concept of being a list, ie the boat also knows what is next in the list. On the other hand your code is two classes. You have the Queue which is a generic implementation of a list, and then the boat and the boat as no idea it is a list, the boat is unaware of other boats.

In the "real world" we do tend to separate objects from their storage mechanism so you'd use List<Boat>, but you're not in the real world writing real code, you're in school learning concepts of OOP, and the lecturer's code is object-orientated and yours isn't.

Also this

Boat _result = new MarinaBerthClassLibrary.Boat();





应该是





should just be

Boat _result;





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



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


我认为你的稍好一些(当然,提供的最佳抽象是 LinkedList< T>)。要告诉您是否正确封装了船信息,您应该向我们提供定义。



注意我'我没有仔细检查你的代码是否正确。
I think your is slightly better (the best abstraction being provided, of course, by LinkedList<T>). To tell if you properly encapsulated the boat info, you should provide us the Boat definition.

Note I've not scrutinized your code for correctness.


查看你的代码:类中的所有数据都是 public - 这意味着它是可用于节点类之外的任何内容,并且可以随时更改。所以他是对的 - 根本没有妥善封装。它揭示了你班级的内部运作,并且修复了它,所以你不能在不考虑对外界可能造成的影响的情况下改变你的班级。



字段不应该是公共的:如果你需要公开它们,那么使用具有setter和getter的属性,你可以控制数据发生了什么。

Look at your code: all your data inside the class is public - which means it is available to anything outside your node class and can be changed at any time. So he's right - it's not properly encapsulated at all. It reveals the "inner workings" of your class, and that "fixes" it so you can't change your class without considering the possible effects on the outside world.

Fields should not be public: if you need to expose them, then use properties, which have a setter and a getter, and you can control what happens to the data.
private Boat _Data = null;
public Boat Data
   {
   get { return _Data; }
   private set { _Data = value; }
   }

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

That way, you retain control over the node content.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆