集合的概念
集合是由一些確定的、彼此不同的成員或者元素構成的一個整體。如果將緊密相關的數據組合到一個集合中,則能夠更有效地處理這些緊密相關的數據。代替編寫不同的代碼來處理每一單獨的對象,您可以使用相同的調用代碼來處理一個集合的所有元素。
在c#中可以使用 Array 類和 System.Collections 類添加、移除和修改集合中的個別元素或某一范圍內的元素。甚至可以將整個集合復制到另一個集合中。在 .NET Framework 2.0 版中,泛型集合類提供了新功能,并使得創建強類型集合變得容易。
雖然c#中提供了多種集合類可供我們選擇,但為了加深對這種數據類型的理解,構造自己的集合類無疑是最好的選擇。下面給出了基于數組及鏈表兩種實現的集合類。
定義集合實現的接口
view plaincopy to clipboardPRint?
public interface IBag:IEnumerable
{
void Add(object element);
void AddAll(IBag bag);
int Size{get;}
bool IsEmpty();
object RemoveRandom();
object Remove(object target);
IBag Union(IBag bag);
bool Contains(object target);
}
public interface IBag:IEnumerable
{
void Add(object element);
void AddAll(IBag bag);
int Size{get;}
bool IsEmpty();
object RemoveRandom();
object Remove(object target);
IBag Union(IBag bag);
bool Contains(object target);
}
基于數組的集合實現
view plaincopy to clipboardprint?
public class ArrayBag:IBag
{
int DEDAULT_CAPACITY = 1000;
Random random = new Random();
int count=0;
object[] contents;
public ArrayBag()
{
contents = new object[DEDAULT_CAPACITY];
//count = 0;
}
public ArrayBag(int initialCapacity)
{
contents = new object[initialCapacity];
// count = 0;
}
#region IBag 成員
public void Add(object element)
{
if (Size == contents.Length)
{
ExpandCapacity();
}
contents[count] = element;
count++;
}
private void ExpandCapacity()
{
object[] temp = new object[contents.Length * 2];
for (int index = 0; index < contents.Length; index++)
{
temp[index] = contents[index];
}
contents = temp;
}
public void AddAll(IBag bag)
{
foreach (object o in bag)
{
Add(o);
}
}
public int Size
{
get { return count; }
}
public bool IsEmpty()
{
return (count==0);
}
public object RemoveRandom()
{
if (IsEmpty())
{
throw new Exception("this colleciton is empty!");
}
int choice = random.Next(count);
object result = contents[choice];
contents[choice] = contents[count - 1];
contents[count - 1] = null;
count--;
return result;
}
public object Remove(object target)
{
if (IsEmpty())
{
throw new Exception("the collection is empty");
}
int index = Find(target);
if (index == -1)
{
throw new Exception("not found!");
}
else
{
object result = contents[index];
contents[index] = contents[count - 1];
contents[count - 1] = null;
count--;
return result;
}
}
private int Find(object target)
{
for (int i = 0; i < count; i++)
{
if (contents[i].Equals(target))
return i;
}
return -1;
}
public IBag Union(IBag bag)
{
ArrayBag both = new ArrayBag();
for (int index = 0; index < count; index++)
{
both.Add(contents[index]);
}
foreach (object o in bag)
{
both.Add(o);
}
return both;
}
public bool Contains(object target)
{
if (Find(target) != -1)
{
return true;
}
else
{
return false;
}
}
public IEnumerator GetEnumerator()
{
for (int index = 0; index < count; index++)
{
yield return contents[index];
}
}
#endregion
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++)
{
sb.Append(contents[i] + " ");
}
return sb.ToString();
}
}
public class ArrayBag:IBag
{
int DEDAULT_CAPACITY = 1000;
Random random = new Random();
int count=0;
object[] contents;
public ArrayBag()
{
contents = new object[DEDAULT_CAPACITY];
//count = 0;
}
public ArrayBag(int initialCapacity)
{
contents = new object[initialCapacity];
// count = 0;
}
#region IBag 成員
public void Add(object element)
{
if (Size == contents.Length)
{
ExpandCapacity();
}
contents[count] = element;
count++;
}
private void ExpandCapacity()
{
object[] temp = new object[contents.Length * 2];
for (int index = 0; index < contents.Length; index++)
{
temp[index] = contents[index];
}
contents = temp;
}
public void AddAll(IBag bag)
{
foreach (object o in bag)
{
Add(o);
}
}
public int Size
{
get { return count; }
}
public bool IsEmpty()
{
return (count==0);
}
public object RemoveRandom()
{
if (IsEmpty())
{
throw new Exception("this colleciton is empty!");
}
int choice = random.Next(count);
object result = contents[choice];
contents[choice] = contents[count - 1];
contents[count - 1] = null;
count--;
return result;
}
public object Remove(object target)
{
if (IsEmpty())
{
throw new Exception("the collection is empty");
}
int index = Find(target);
if (index == -1)
{
throw new Exception("not found!");
}
else
{
object result = contents[index];
contents[index] = contents[count - 1];
contents[count - 1] = null;
count--;
return result;
}
}
private int Find(object target)
{
for (int i = 0; i < count; i++)
{
if (contents[i].Equals(target))
return i;
}
return -1;
}
public IBag Union(IBag bag)
{
ArrayBag both = new ArrayBag();
for (int index = 0; index < count; index++)
{
both.Add(contents[index]);
}
foreach (object o in bag)
{
both.Add(o);
}
return both;
}
public bool Contains(object target)
{
if (Find(target) != -1)
{
return true;
}
else
{
return false;
}
}
public IEnumerator GetEnumerator()
{
for (int index = 0; index < count; index++)
{
yield return contents[index];
}
}
#endregion
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++)
{
sb.Append(contents[i] + " ");
}
return sb.ToString();
}
}
基于鏈表的集合實現
view plaincopy to clipboardprint?
public class LinkNode
{
object element;
public object Element
{
get { return element; }
set { element = value; }
}
LinkNode next;
public LinkNode Next
{
get { return next; }
set { next = value; }
}
public LinkNode(object element)
{
this.element = element;
next = null;
}
public LinkNode()
{
}
}
public class LinkedBag:IBag
{
#region IBag 成員
LinkNode contents;
int count;
Random r = new Random();
public void Add(object element)
{
LinkNode node = new LinkNode(element);
node.Next = contents;
contents = node;
count++;
}
public void AddAll(IBag bag)
{
foreach (object o in bag)
{
Add(o);
}
}
public int Size
{
get { return count; }
}
public bool IsEmpty()
{
return (count==0);
}
public object RemoveRandom()
{
object result = null;
LinkNode previous, current;
if (IsEmpty())
{
throw new Exception("collection is empty!");
}
int choice = r.Next(count);
if (choice == 0)
{
result = contents.Element;
contents = contents.Next;
}
else
{
previous = contents;
for (int i = 1; i < choice; i++)
{
previous = previous.Next;
}
current = previous.Next;
result = current.Element;
previous.Next = current.Next;
}
count--;
return result;
}
public object Remove(object target)
{
bool found = false;
LinkNode previous, current;
object result = null;
if (IsEmpty())
{
throw new Exception("collection is empty!");
}
if (contents.Element.Equals(target))
{
result = contents.Element;
contents = contents.Next;
}
else
{
previous = contents;
current = previous.Next;
for(int i=1;i<count&&!found;i++)
{
if (current.Element.Equals(target))
{
found = true;
}
previous = current;
current = previous.Next;
}
if (!found)
{
throw new Exception("Elements not found!");
}
result = current.Next;
previous.Next = current.Next;
}
count--;
return result;
}
public IBag Union(IBag bag)
{
LinkedBag both = new LinkedBag();
foreach (LinkNode node in this)
{
both.Add(node.Element);
}
foreach (LinkNode node in bag)
{
both.Add(node.Element);
}
return both;
}
public bool Contains(object target)
{
bool found = false;
LinkNode current = contents;
while (current != null && !found)
{
if (current.Element.Equals(target))
{
found = true;
}
current = current.Next;
}
return found;
}
#endregion
#region IEnumerable 成員
public System.Collections.IEnumerator GetEnumerator()
{
LinkNode current = contents;
while (current != null)
{
yield return current;
current = current.Next;
}
}
#endregion
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (LinkNode node in this)
sb.Append(node.Element.ToString() + " ");
return sb.ToString();
}
}
新聞熱點
疑難解答