Click here to Skip to main content
15,849,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 23-Mar-19 2:22am
Comments
Richard MacCutchan 23-Mar-19 7:22am    
I agree with you. The node class should be able to handle any object to make a linkedlist. Putting the boat data into the node prevents that. My only comment on your solution is that your nodes expect a boat reference, but they would be better if they accepted an Object. You can then use that code to manage any type that inherits from object.

"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.
 
Share this answer
 
v2
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.
 
Share this answer
 
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.
C#
private Boat _Data = null;
public Boat Data
   {
   get { return _Data; }
   private set { _Data = value; }
   }
That way, you retain control over the node content.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900