3 Replies - 661 Views - Last Post: 02 March 2013 - 10:18 AM Rate Topic: -----

#1 justawnb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 13

Linked list questions

Posted 02 March 2013 - 09:30 AM

So i got a task to implement a linked list with the following methods: class LinkedList
- public bool Add(NodeElement element) – adds a new element to the end of the list;
- public NodeElement Get() – gets an element from the beginning of the list and removes it from the list;
- public void Print() – prints the list contents to the standard output (for each NodeElement call its Print() method);

Nodes of the lists are descendants of type NodeElement and they are of two types:
NodeElementLong (with public property “long Data”) and NodeElementString (with public property “string Data”) (both of the classes should be inherited from NodeElement class).
In NodeElement you should specify virtual method Print which should be properly overridden in descendant classes.
Both descendant classes should properly implement ICloneable interface.
Use of generics is encouraged; take special attention to properly implement the IDisposable interface, because in the future a descendant class that might contain memory leaks could appear.

So i dont exactly know how to solve this. Like i have 2 types of NodeElement and i dont know how i would implement it in my code.

public class LinkedList
    {
        private NodeElement head;
        private NodeElement current;

        public LinkedList()
        {
            head = null;
            current = null;
        }

        public bool Add(NodeElement element)
        {
            if (head == null)
            {
                head = element;
                current = head;
            }
            else
            {
                current.Next = element;
                current = element;
                
            }
            return true;
        }

        //  returns null if there is no data
        public NodeElement Get()
        {
            if (head == null) return null;
            NodeElement tmp = head;
            head = tmp.Next;
            tmp.Next = null;
            return tmp;
        }


        public void Print()
        {
            NodeElement tmp = head;
            while (tmp != null)
            {
                tmp.Print();
                tmp = tmp.Next;
            }
        }

    }


public class NodeElement
    {
        private NodeElement next;
        private int test;

        public NodeElement()
        {
            next = null;
            test = 0;
        }

        public NodeElement(int data)
        {
            this.next = null;
            test = data;
        }

        public NodeElement Next
        {
            get { return this.next; }
            set { this.next = value;}
        }

        virtual public void Print()
        {
        }

        public object Clone()
        {
            return 0;
        }

    }

public class NodeElementLong : NodeElement
    {
        public long Data;

        public NodeElementLong(long data) 
        {
            base.Next = null;
            this.Data = data;
        }

        public override void Print()
        {
            Console.WriteLine(Data);
        }
            
    }




So how do i create linked class so that it can use both NodeElementLong and NodeElementString?
And how can i do the ICloneable and IDisposable both at the same time, since NodeElementLong descends from NodeElement so i cant descend from ICloneable??

Ty for answers and any help you can give me!

Is This A Good Question/Topic? 0
  • +

Replies To: Linked list questions

#2 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Linked list questions

Posted 02 March 2013 - 09:40 AM

Quote

And how can i do the ICloneable and IDisposable both at the same time, since NodeElementLong descends from NodeElement so i cant descend from ICloneable??


ICloneable and IDisposable are both interfaces, and a class can implement as many interfaces as it wishes. On the other hand, a class can only inherit from one other class.

Quote

So how do i create linked class so that it can use both NodeElementLong and NodeElementString?


By creating the two classes, and having both inherit from the NodeElement class, as the instructions suggest, and perhaps creating a generic type parameter on the LinkedList class and the NodeElement class.

This post has been edited by CodingSup3rnatur@l-360: 02 March 2013 - 10:56 AM
Reason for edit:: Changed code tags for quote tags.

Was This Post Helpful? 1
  • +
  • -

#3 justawnb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 13

Re: Linked list questions

Posted 02 March 2013 - 10:00 AM

Ok, so i understand now that i can use more Interfaces.
The thing I dont understand is how would I in my LinkedList class now use class NodeElementLong or NodeElementString without acctually changing the class?

 
public class LinkedList
{
    private NodeElement head;     // So this should be NodeElementString or NodeElementLong
    private NodeElement current;  // So how can i use the same class with both?
... 


Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Linked list questions

Posted 02 March 2013 - 10:18 AM

Quote

The thing I dont understand is how would I in my LinkedList class now use class NodeElementLong or NodeElementString without acctually changing the class?


There are two things you need to understand to achieve this.

1) When class C inherits from class A, an instance of class C is substitutable for an instance of class A. This means that wherever an instance of class A is expected, you can pass an instance of class C instead.

class A {
    public int Property { get; set; }
}

class C : A {
}

...

void Test(A a) {

}



I can call Test() like this:

C c = new C();
Test(c);



because C inherits from A, and is therefore substitutable for it.

2) Inheritance gets you part of the way there, but you have still got the problem that NodeElementLong and NodeElementString hold data of different types. So, how can you generalize the NodeElement class, and the LinkedList class so they can work with either type?

The answer is to use generics.

Say I want the property in class A above to be able to hold any type of data. How would I do that? I would introduce a generic type parameter:

class A<T> {
    public T Property { get; set; }
}



Now, class A (or, more specifically, Property) can hold any data of type T.

For example:

A<int> a1 = new A<int>(); //this instance holds an int
A<string> a2 = new A<string>(); //this instance holds a string



I won't cover how that affects the derived class C at this stage. I'll just leave you to think about how these things can be applied to your code. Have a go at it, and post your updated code if you get stuck :)

This post has been edited by CodingSup3rnatur@l-360: 02 March 2013 - 10:44 AM
Reason for edit:: Changed class B to class C to avoid forum bug.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1