using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MyTest
{
static void Main(string[] args)
{
//test linked list
{
Console.WriteLine("test linked list");
Console.WriteLine();
MyLinkedList<int> list1 = LinkedListFactory.QuicklyGenerateRandomIntLinkedList(5);
MyLinkedList<int> list2 = list1.ShallowCopy();
MyLinkedList<int> list3 = list1.DeepCopy();
//test ShallowCopy() method
Console.WriteLine("test ShallowCopy() method - comapare list1 andlist2");
Test(list1, list2);
Console.WriteLine("------------------------------------------------");
//test DeepCopy() method
Console.WriteLine("test DeepCopy() method - comapare list1andlist3");
Test(list1, list3);
Console.WriteLine("------------------------------------------------");
}
//test Circular Linked list
{
Console.WriteLine();
Console.WriteLine("test Circular Linked list");
Console.WriteLine();
MyCircularLinkedList<int> list1 = LinkedListFactory.QuicklyGenerateRandomIntCircularLinkedList(5);
MyCircularLinkedList<int> list2 = list1.ShallowCopy();
MyCircularLinkedList<int> list3 = list1.DeepCopy();
//test ShallowCopy() method
Console.WriteLine("test ShallowCopy() method - comapare list1 andlist2");
Test(list1, list2);
Console.WriteLine("------------------------------------------------");
//test DeepCopy() method
Console.WriteLine("test DeepCopy() method - comapare list1andlist3");
Test(list1, list3);
}
Console.ReadKey();
}
public static void Test(MyLinkedList<int> list1, MyLinkedList<int> list2)
{
if (list1.Count != list2.Count)
{
Console.WriteLine("the counts of the two lists are different!");
return;
}
Node<int>p1= list1.Head;
Node<int>p2= list2.Head;
string format = "NodeNo.{0}compare result (if same instance): {1}";
if (p1 == p2)
Console.WriteLine(string.Format(format, 0, true));
else
Console.WriteLine(string.Format(format, 0, false));
for(int i = 1;p1.Next != null;++i)
{
p1 = p1.Next;
p2 = p2.Next;
if (p1 == p2)
Console.WriteLine(string.Format(format, i, true));
else
Console.WriteLine(string.Format(format, i, false));
}
}
public static void Test(MyCircularLinkedList<int> list1, MyCircularLinkedList<int> list2)
{
if (list1.Count != list2.Count)
{
Console.WriteLine("the counts of the two lists are different!");
return;
}
Node<int>p1= list1.Head;
Node<int>p2= list2.Head;
string format = "NodeNo.{0}compare result (if same instance): {1}";
if (p1 == p2)
Console.WriteLine(string.Format(format, 0, true));
else
Console.WriteLine(string.Format(format, 0, false));
for (int i = 1;p1.Next!= list1.Head; ++i)
{
p1 = p1.Next;
p2 = p2.Next;
if (p1 == p2)
Console.WriteLine(string.Format(format, i, true));
else
Console.WriteLine(string.Format(format, i, false));
}
}
}
public class Node<T>
{
public T Data { get; set; }
public Node<T>Next { get; set;}
public Node(T Data, Node<T>Next)
{
this.Data = Data;
this.Next = Next;
}
public Node(T Data)
{
this.Data = Data;
}
}
public class MyLinkedList<T>
{
public Node<T>Head { get; private set; }
public int Count
{
get
{
Node<T> p = this.Head;
int i = 0;
while (p.Next != null)
{
p = p.Next;
++i;
}
return i;
}
}
public MyLinkedList()
{
this.Head = new Node<T>(default(T));
}
public MyLinkedList<T>ShallowCopy()
{
//The MemberwiseClone method creates a shallow copybycreating a new object,
//and then copying the nonstatic fields of thecurrentobject to the new object.
//If a field is a value type, a bit-by-bit copy of thefieldis performed.
//If a field is a reference type, the reference iscopiedbut the referred object is not;
//therefore, the original object and its clone refer tothesame object.
return this.MemberwiseClone()as MyLinkedList<T>;
//MyLinkedList<T>newList = newMyLinkedList<T>();
//newList.Head.Next = this.Head.Next;
//return newList;
}
public MyLinkedList<T>DeepCopy()
{
MyLinkedList<T> newList = new MyLinkedList<T>();
Node<T> pOld = this.Head;
Node<T> pNew = newList.Head;
while (pOld.Next != null)
{
pOld = pOld.Next;
pNew.Next = new Node<T>(pOld.Data);
pNew = pNew.Next;
}
return newList;
}
}
public class MyCircularLinkedList<T>
{
public Node<T>Head { get; private set; }
public int Count
{
get
{
Node<T> p = this.Head;
int i = 0;
while (p.Next != this.Head)
{
p = p.Next;
++i;
}
return i;
}
}
public MyCircularLinkedList()
{
this.Head = new Node<T>(default(T));
}
public MyCircularLinkedList<T>ShallowCopy()
{
return this.MemberwiseClone()as MyCircularLinkedList<T>;
}
public MyCircularLinkedList<T>DeepCopy()
{
MyCircularLinkedList<T> newList = new MyCircularLinkedList<T>();
Node<T> pOld = this.Head;
Node<T> pNew = newList.Head;
while (pOld.Next != this.Head)
{
pOld = pOld.Next;
pNew.Next = new Node<T>(pOld.Data);
pNew = pNew.Next;
}
pNew.Next = newList.Head;
return newList;
}
}
public class LinkedListFactory
{
public static MyLinkedList<int>QuicklyGenerateRandomIntLinkedList(int length)
{
if (length > 0)
{
MyLinkedList<int> randomList = new MyLinkedList<int>();
Node<int>p= randomList.Head;
Random random = new Random(47);
for (int i = 0; i < length; ++i)
{
p.Next = new Node<int>(random.Next(1, 100));
p = p.Next;
}
return randomList;
}
else
throw new ArgumentOutOfRangeException("length", "Inputlengthshould be greater than 0.");
}
public static MyCircularLinkedList<int>QuicklyGenerateRandomIntCircularLinkedList(int length)
{
if (length > 0)
{
MyCircularLinkedList<int> randomList = new MyCircularLinkedList<int>();
Node<int>p= randomList.Head;
Random random = new Random(47);
for (int i = 0; i < length; ++i)
{
p.Next = new Node<int>(random.Next(1, 100));
p = p.Next;
}
p.Next = randomList.Head;
return randomList;
}
else
throw new ArgumentOutOfRangeException("length", "Inputlengthshould be greater than 0.");
}
}
}