看看那些高手们看过这样的代码。

samlinux 2006-05-25 03:49:21
问一下各位高手们是否以前看过这样的代码?小弟不是很明白,如果有明白的给说明一下,谢谢。


using System;
using System.Collections;
using System.Collections.Generic;

//K is the key, T is the data item
class Node<K,T>
{
public K Key;
public T Item;
public Node<K,T> NextNode;

public Node()
{
Key = default(K);
Item = default(T);
NextNode = null;
}
public Node(K key,T item,Node<K,T> nextNode)
{
Key = key;
Item = item;
NextNode = nextNode;
}
}

public class LinkedList<K,T> : IEnumerable<T> where K : IComparable<K>
{
Node<K,T> m_Head;

public LinkedList()
{
m_Head = new Node<K,T>();
}
public void AddHead(K key,T item)
{
Node<K,T> newNode = new Node<K,T>(key,item,m_Head.NextNode);
m_Head.NextNode = newNode;
}
public T this[K key]
{
get
{
return Find(key);
}
}
T Find(K key)
{
Node<K,T> current = m_Head;

while(current.NextNode != null)
{
if(current.Key.Equals(key))
{
break;
}
else
{
current = current.NextNode;
}
}
return current.Item;
}

public IEnumerator<T> GetEnumerator()
{
Node<K,T> current = m_Head;
while(current != null)
{
yield return current.Item;
current = current.NextNode;
}
}
public static LinkedList<K,T> operator+(LinkedList<K,T> lhs,LinkedList<K,T> rhs)
{
return concatenate(lhs,rhs);
}
static LinkedList<K,T> concatenate(LinkedList<K,T> list1,LinkedList<K,T> list2)
{
LinkedList<K,T> newList = new LinkedList<K,T>();
Node<K,T> current;

current = list1.m_Head;
while(current != null)
{
newList.AddHead(current.Key,current.Item);
current = current.NextNode;
}

current = list2.m_Head;

while(current != null)
{
newList.AddHead(current.Key,current.Item);
current = current.NextNode;
}
return newList;
}
}
*************************************************************************************
...全文
82 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
samlinux 2006-05-25
  • 打赏
  • 举报
回复
楼上的好像没明白我的意思,我想知道这个类是干什么用的,因为确实没看明白。
tiaoci 2006-05-25
  • 打赏
  • 举报
回复
不是有现成的 KeyValuePair<K,V>和 LinkedList<T>吗?

何必自己在写一套?
samlinux 2006-05-25
  • 打赏
  • 举报
回复
怎么没有回应的了,难道都没见过?
samlinux 2006-05-25
  • 打赏
  • 举报
回复
楼上的是否可以说得详细些,谢谢。
vshake 2006-05-25
  • 打赏
  • 举报
回复
就是个单链表模板啊.
不过要我写我是写不出来,看还是看得懂地.
liujiayu10 2006-05-25
  • 打赏
  • 举报
回复
看着头晕

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧