C# homework

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Here is the task:
Create a Console Application project.
Please implement a Linear List (Linked List) named LinearCollection with the following
methods:
 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.
Don’t use recursion at all.
Here is what I did:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Naloga2
{
public class LinearCollection
{
public LinearCollection()
{
ListOfElements = new List<NodeElement>();
}

public List<NodeElement> ListOfElements { get; set; }

public bool Add(NodeElement element)
{
try
{
ListOfElements.Add(element);
return true;
}
catch
{
return false;
}
}

public NodeElement Get()
{
NodeElement nodeElement = ListOfElements.FirstOrDefault();
ListOfElements.Remove(nodeElement);
return nodeElement;
}

public void Print()
{
foreach (NodeElement element in ListOfElements)
{
NodeElementString neString = (NodeElementString)element;
Console.WriteLine(neString.Data);
}
}
}

public class NodeElement : IDisposable
{
public NodeElement() { }

~NodeElement()
{
Dispose(false);
}

public virtual void Print()
{

}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{

}
}
}

public class NodeElementString : NodeElement, ICloneable
{
public NodeElementString() { }

public String Data { get; set; }

public override void Print()
{

}

public object Clone()
{
NodeElementString temp = new NodeElementString();
temp.Data = this.Data.Clone() as String;
return temp;
}
}

public class NodeElementLong : NodeElement, ICloneable
{
public NodeElementLong() { }

public NodeElementLong(long data)
{
Data = data;
}

public long Data { get; set; }

public override void Print()
{

}

public object Clone()
{
return new NodeElementLong(Data);
}
}

}

This is my main class:static void Main(string[] args)
{
LinearCollection linearCollection = new LinearCollection();

// Read words from file and add them to collection
string[] wordsFromFile = File.ReadAllLines("words.txt");

foreach (string word in wordsFromFile)
{
NodeElementString neString = new NodeElementString();
neString.Data = word;
linearCollection.Add(neString);
}

// Display words
linearCollection.Print();
Console.WriteLine("nn");

// Add item to collection
NodeElementString neNewItem = new NodeElementString();
neNewItem.Data = "New item to add to list";
linearCollection.Add(neNewItem);
Console.WriteLine("New element was added to the list: {0}", neNewItem.Data);
Console.WriteLine("nn");

// Display collection after adding element
linearCollection.Print();
Console.WriteLine("nn");

// Show first element and remove from list
NodeElementString neFirst = (NodeElementString)linearCollection.Get();
Console.Write("Element that will be erased: {0}", neFirst.Data);
Console.WriteLine("nn");

// Display collection after deleting element
linearCollection.Print();

Console.ReadLine();
}

Program works fine, but the class is not implemented as it should be.Can somebody please fix my mistakes?

View the full article
 
Back
Top