ä¸é¢æ¯å
³äºåºäº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# æ³åéåææ帮å©
温馨提示:答案为网友推荐,仅供参考