tags:

views:

66

answers:

3

Hi,

Before posting my question, I would like to tell you that I have no prior experience in .Net technologies and have recently started to learn C# (and WPF). My company is looking to move onto .Net technologies and I am the only one in my team learning it, so have noone apart from you guys to discuss or ask something. So if my questions are too stupid or basic at best, please bear with me.

I was trying to create a generic linked list class to allow creation of linked lists of different types. I wrote the following code and would like to have your advise on whether I have written this code properly or not. Any suggestions to improve the code in any way is most welcome.

Main program

class Program
{
    static void Main(string[] args)
    {
        GenLinkedList<string> list = new GenLinkedList<string>("abc");
        list.AddtoList("def");
        int i = 0;
        string[] arr = new string[10];
        list.LinkedList.CopyTo(arr,0);
        for (i = 0; i < list.LinkedList.Count; i++)
        {
            Console.WriteLine(arr[i]);
        }

        GenLinkedList<int> listInt = new GenLinkedList<int>(1);
        listInt.AddtoList(2);
        i = 0;
        int[] arrInt = new int[10];
        listInt.LinkedList.CopyTo(arrInt, 0);
        for (i = 0; i < listInt.LinkedList.Count; i++)
        {
            Console.WriteLine(arrInt[i]);
        }

    }
}

Class GenLinkedList

public class GenLinkedList<T>
{
    private LinkedList<T> _linkedlist;
    public GenLinkedList(T a)
    {
        _linkedlist = new LinkedList<T>();
        _linkedlist.AddLast(a);
    }

    public LinkedList<T> LinkedList
    {
        get
        {
            return _linkedlist;
        }
    }

    public void AddtoList(T a)
    {
        LinkedList.AddLast(a);
    }
}
+6  A: 

Why not use System.Collections.Generic.LinkedList<T>? You're using it internally in your GenLinkedList<T> already and it's already Generic for you.

It's the Framework Provided Generic Doubly Linked List implementation. Read up on it at:

MSDN - LinkedList(T) Class

If you're creating a Generic Linked List as an exercise, you shouldn't base your implementation of of an existing Generic Linked List. You're really not going to be learning anything by wrapping something that already does exactly what you need to do.

Justin Niessner
Which is already being used (wrapped in `GenLinkedList`).
Oded
I know. Actually I was doing a practice test from a book and the exercise was "Create a linked-list generic class that enables you to create a chain of objects of different types." I think it just wanted me to check whether I can write a generic class or not??
Abhi
He knows- his implementation uses it.
DeadMG
@user366436: If you're trying to complete an *exercise* to create a linked list, then using a prebuilt linked list is cheating. If you're *not* doing it purely as an exercise, then using a prebuilt linked list class is a waste of time.
Jon Skeet
Practice questions such as that do not want you to use `LinkedList<T>` internally; they want you to write your own.
Stephen Cleary
@user366436 - If you're just trying to implement a Generic Class, then yes. This is how you would do it. This isn't really exposing you to the more complex details of generics since you're using an existing generic class internally though.
Justin Niessner
@Jon Skeet - My thoughts exactly. Updated my answer before I saw your comment. At least I feel a little better about my edit now.
Justin Niessner
+1  A: 

The obvious question is why you don't just use LinkedList<T> directly, although it looks like you're trying to emulate a singly-linked list.

In this case, you should avoid exposing the underlying LinkedList<T> instance, since any client could manipulate it directly. I would also implement IEnumerable<T> which will make your list usable by linq.

Lee
IEnumerable<T> is definitely your friend.
R0MANARMY
+2  A: 

1

A generic linked list implementation already exists in the .NET framework: LinkedList<T>. But you already know that; your code wraps it.

2

OK, so you know that. Why would you wrap it, then? The only functionality you appear to have implemented is AddtoList, which doesn't do anything the LinkedList<T> doesn't already do itself (after all, this is only a thin wrapper around LinkedList<T>.AddLast). What this means is that your GenLinkedList<T> class really doesn't offer the functionality of a linked list; it's basically an add-only collection (which could just as easily have been implemented with a List<T>, or a Stack<T>, or a Queue<T> -- anything, really).

3

Assuming you do have a good reason to wrap a LinkedList<T> (e.g., you're planning to add more functionality down the line that would actually leverage the behavior of a LinkedList<T> and/or -- here's a key ingredient -- you want to restrict the way calling code is able to interact with the list (e.g., no removals)), you really shouldn't expose your LinkedList<T> member at all. The purpose of a wrapper is just that: to wrap. You take an existing class and basically give it a new kind of interface. By exposing the underlying object directly, you cripple your wrapper. Any additional restrictions/validation/logic you have in your wrapper can be bypassed.

So, for example, if you want to be able to copy your list to an array, instead of doing this:

list.LinkedList.CopyTo(arr,0);

You would implement a CopyTo method within your GenLinkedList<T> class (which could simply call _linkedlist.CopyTo) and use that.

But I really think the first question you should be asking yourself is what you want to accomplish by wrapping LinkedList<T> in the first place.

Dan Tao
Actually I was doing a practice test from the MCTS book and the exercise was "Create a linked-list generic class that enables you to create a chain of objects of different types." Now I think that they wanted me to implement my own linked list. I will try that approach then.Also, I would try to implement your third suggestion in my code. Thanks. I guess you mean that the user should be able to access only those methods that I want them to access. Is that right?
Abhi
@user366436: That's correct. Now, I want to point out something else, though. If the exercise is to write a list that can "create a chain of objects of *different types*," then writing a *generic* class really isn't the correct approach to begin with, as it restricts you to a specific type. But since the exercise also includes the word "generic", I'm not really sure what the book wants you to do.
Dan Tao