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.
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!
- 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!