c#中如何让用泛型<list>写链表foreach 接口都行,最好代码写出来 谢了。。。。
2个回答
展开全部
下面是关于基于list<t>泛型链表的应用,如有疑问可以追问。
using System.Collections;
//节点类
class MyListNode<T>
{
private T data;//数据域
public T Data
{
get { return data; }
set { data = value; }
}
private MyListNode<T> next;//引用域
public MyListNode<T> Next
{
get { return next; }
set { next = value; }
}
public MyListNode(T val)//构造函数
{
data = val;
next = null;
}
}
//模拟实现泛型集合List﹤T﹥类
class MyList﹤T﹥
{
private MyListNode firstNode;//首节点
private int count;//C# 泛型集合-节点计数
public MyList()
{
this.firstNode = null;
this.count = 0;
}
//C# 泛型集合-得到List长度
public int GetLength()
{
return this.count;
}
//增加一个节点
public void AddElement(T data)
{
MyListNode first = this.firstNode;
if(first==null)
{
this.firstNode=new MyListNode(data);
this.count++;
return;
}
while (first.next != null)
{
first = first.next;
}
first.next = new MyListNode(data);
this.count++;
}
//C# 泛型集合-删除一个节点
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = first.next;
this.count--;
return true;
}
while (first.next!=null)
{
if (first.next.data.Equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
}
return false;
}
//C# 泛型集合-得到指定索引上的集合元素
public T GetAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
else
{
while (innercount ﹤ index)
{
first = first.next;
innercount++;
}
return first.data;
}
}
//在指定的索引上插入新的元素
public void InsertAtIndex(int index,T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
MyListNode newNode = new MyListNode(data);
newNode.next = first.next;
first.next = newNode;
}
this.count++;
}
//C# 泛型集合-删除指定索引上的集合元素
public void RemoveAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = first.next;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
first.next = first.next.next;
}
this.count--;
}
//C# 泛型集合-删除集合中的所有元素
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
//为实现该集合类能用foreach进行遍历
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first!= null)
{
yield return first.data;
first = first.next;
}
}
//内部节点类
private class MyListNode
{
public T data { get; set; }//节点上的元素值
public MyListNode next { get; set; }//节点的下一个节点
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
}
下面是C# 泛型集合对这个模拟类的使用:
class Program
{
static void Main(string[] args)
{
MyList﹤string﹥ ml = new MyList﹤string﹥();
ml.AddElement("xu");
ml.AddElement("jin");
ml.AddElement("lin");
ml.AddElement("love");
ml.AddElement("jasmine");
ml.InsertAtIndex(4, "fiercely");
ml.RemoveAtIndex(2);
ml.Remove("lin");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
C# 泛型集合实例应用的基本内容就向你介绍到这里,希望对你了解和学习C# 泛型集合有所帮助
using System.Collections;
//节点类
class MyListNode<T>
{
private T data;//数据域
public T Data
{
get { return data; }
set { data = value; }
}
private MyListNode<T> next;//引用域
public MyListNode<T> Next
{
get { return next; }
set { next = value; }
}
public MyListNode(T val)//构造函数
{
data = val;
next = null;
}
}
//模拟实现泛型集合List﹤T﹥类
class MyList﹤T﹥
{
private MyListNode firstNode;//首节点
private int count;//C# 泛型集合-节点计数
public MyList()
{
this.firstNode = null;
this.count = 0;
}
//C# 泛型集合-得到List长度
public int GetLength()
{
return this.count;
}
//增加一个节点
public void AddElement(T data)
{
MyListNode first = this.firstNode;
if(first==null)
{
this.firstNode=new MyListNode(data);
this.count++;
return;
}
while (first.next != null)
{
first = first.next;
}
first.next = new MyListNode(data);
this.count++;
}
//C# 泛型集合-删除一个节点
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = first.next;
this.count--;
return true;
}
while (first.next!=null)
{
if (first.next.data.Equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
}
return false;
}
//C# 泛型集合-得到指定索引上的集合元素
public T GetAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
else
{
while (innercount ﹤ index)
{
first = first.next;
innercount++;
}
return first.data;
}
}
//在指定的索引上插入新的元素
public void InsertAtIndex(int index,T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
MyListNode newNode = new MyListNode(data);
newNode.next = first.next;
first.next = newNode;
}
this.count++;
}
//C# 泛型集合-删除指定索引上的集合元素
public void RemoveAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = first.next;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
first.next = first.next.next;
}
this.count--;
}
//C# 泛型集合-删除集合中的所有元素
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
//为实现该集合类能用foreach进行遍历
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first!= null)
{
yield return first.data;
first = first.next;
}
}
//内部节点类
private class MyListNode
{
public T data { get; set; }//节点上的元素值
public MyListNode next { get; set; }//节点的下一个节点
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
}
下面是C# 泛型集合对这个模拟类的使用:
class Program
{
static void Main(string[] args)
{
MyList﹤string﹥ ml = new MyList﹤string﹥();
ml.AddElement("xu");
ml.AddElement("jin");
ml.AddElement("lin");
ml.AddElement("love");
ml.AddElement("jasmine");
ml.InsertAtIndex(4, "fiercely");
ml.RemoveAtIndex(2);
ml.Remove("lin");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
C# 泛型集合实例应用的基本内容就向你介绍到这里,希望对你了解和学习C# 泛型集合有所帮助
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询