using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace node
{
class program
{
static void Main(string[] args)
{
MyList<string> ml = new MyList<string>();
ml.AddElement("wang");
ml.AddElement("xu");
ml.AddElement("liu");
ml.AddElement("sun");
ml.InsertAIndex(2, "li");
ml.RemoveAIndex(3);
ml.Remove("wang");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
public class MyListNode
{
public T data { get; set; }
public MyListNode next { get; set; }
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
class MyList<T>
{
private MyListNode firstNode;
private int count;
public MyList()
{
this.firstNode = null;
this.count = 0;
}
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++;
}
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = firstNode.next;
this.count--;
return true;
}
while (first.next.data.equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
return false;
}
public T GetAIndex(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 InsertAIndex(int index, T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index > count)
{
throw new Exception("out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first; }
else
{
while (innercount < index - 1)
{
first = first.nest;
innercount++;
}
MyListNode newnode = new MyListNode(data);
newnode.next = first.next;
first.next = newnode;
}
this.count++; }
public void RemoveAIndex(int index)
{
int innercout = 1;
MyListNode first = this.firstNode;
if (index > count)
{
throw new Exception("out of boundary"); }
else
{
if (index == 1)
{
this.firstNode = first.next; }
else
{
while (innercout < index - 1)
{
first = first.next;
innercout++;
}
first.next = first.next.next;
}
count--;
} }
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first != null)
{
yield return first.data;
first = first.next;
}
}
}
}最后报错说Error The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
究竟是缺失了哪个reference呢?谢谢!
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace node
{
class program
{
static void Main(string[] args)
{
MyList<string> ml = new MyList<string>();
ml.AddElement("wang");
ml.AddElement("xu");
ml.AddElement("liu");
ml.AddElement("sun");
ml.InsertAIndex(2, "li");
ml.RemoveAIndex(3);
ml.Remove("wang");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
public class MyListNode
{
public T data { get; set; }
public MyListNode next { get; set; }
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
class MyList<T>
{
private MyListNode firstNode;
private int count;
public MyList()
{
this.firstNode = null;
this.count = 0;
}
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++;
}
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = firstNode.next;
this.count--;
return true;
}
while (first.next.data.equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
return false;
}
public T GetAIndex(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 InsertAIndex(int index, T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index > count)
{
throw new Exception("out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first; }
else
{
while (innercount < index - 1)
{
first = first.nest;
innercount++;
}
MyListNode newnode = new MyListNode(data);
newnode.next = first.next;
first.next = newnode;
}
this.count++; }
public void RemoveAIndex(int index)
{
int innercout = 1;
MyListNode first = this.firstNode;
if (index > count)
{
throw new Exception("out of boundary"); }
else
{
if (index == 1)
{
this.firstNode = first.next; }
else
{
while (innercout < index - 1)
{
first = first.next;
innercout++;
}
first.next = first.next.next;
}
count--;
} }
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first != null)
{
yield return first.data;
first = first.next;
}
}
}
}最后报错说Error The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
究竟是缺失了哪个reference呢?谢谢!
{
static void Main(string[] args)
{
MyList<string> ml = new MyList<string>();
ml.AddElement("wang");
ml.AddElement("xu");
ml.AddElement("liu");
ml.AddElement("sun");
ml.InsertAIndex(2, "li");
ml.RemoveAIndex(3);
ml.Remove("wang");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
public class MyListNode<T>
{
public T data { get; set; }
public MyListNode<T> next { get; set; }
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
class MyList<T>
{
private MyListNode<T> firstNode;
private int count;
public MyList()
{
this.firstNode = null;
this.count = 0;
}
public int GetLength()
{
return this.count;
}
public void AddElement(T data)
{
MyListNode<T> first = this.firstNode;
if (first == null)
{
this.firstNode = new MyListNode<T>(data);
this.count++;
return;
}
while (first.next != null)
{
first = first.next;
}
first.next = new MyListNode<T>(data);
this.count++;
}
public bool Remove(T data)
{
MyListNode<T> first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = firstNode.next;
this.count--;
return true;
}
while (first.next.data.Equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
return false;
}
public T GetAIndex(int index)
{
int innercount = 1;
MyListNode<T> 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 InsertAIndex(int index, T data)
{
int innercount = 1;
MyListNode<T> first = this.firstNode;
if (index > count)
{
throw new Exception("out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode<T>(data);
this.firstNode.next = first; }
else
{
while (innercount < index - 1)
{
first = first.next;
innercount++;
}
MyListNode<T> newnode = new MyListNode<T>(data);
newnode.next = first.next;
first.next = newnode;
}
this.count++; }
public void RemoveAIndex(int index)
{
int innercout = 1;
MyListNode<T> first = this.firstNode;
if (index > count)
{
throw new Exception("out of boundary"); }
else
{
if (index == 1)
{
this.firstNode = first.next; }
else
{
while (innercout < index - 1)
{
first = first.next;
innercout++;
}
first.next = first.next.next;
}
count--;
} }
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
public IEnumerator<T> GetEnumerator()
{
MyListNode<T> first = this.firstNode;
while (first != null)
{
yield return first.data;
first = first.next;
}
}
}