using System;class GenericAList
{
    static void Main(string[] args)
    {  
        GElementList<int>  theGElementList =  new GElementList<int>()  ;
        for (int i = 1; i < 6; i++  )
            theGElementList.AddE(i*10);        string listValue = theGElementList.GetListData();
        Console.WriteLine(listValue);        GElementList<string> theGElementList1 = new GElementList<string>();        theGElementList1.AddE("A");
        theGElementList1.AddE("B");
        theGElementList1.AddE("C");
        theGElementList1.AddE("D");
        theGElementList1.AddE("E");
        
        listValue = theGElementList1.GetListData();
        
        Console.WriteLine(listValue); 
        Console.Read();
    }
}
public class ElementS<T>
{
    private T cElement ;
        public T GetElement()
    {
        return cElement; 
    }
    public void SetElement(T pElement)
    {
        this.cElement = pElement; 
    }
    private ElementS<T> theElementS = null   ;    public ElementS<T> GetEClass()
    {       
        return theElementS;
    }
    public void SetEClass(ElementS<T> pElementS)
    {
        this.theElementS = pElementS;
    }    
}
public class GElementList<T>
{
    private ElementS<T> firstElement = new ElementS<T>() ;
    
    public void AddE(T pElement)
    {
        ElementS<T> newElementS = new ElementS<T>();
        newElementS.SetElement(pElement);
        newElementS.SetEClass(firstElement)  ;
        firstElement = newElementS  ;        
    }
    public string GetListData()
    {
        string returnValue = "" ;        
        
        do
        {
            returnValue =  firstElement.GetElement().ToString() + " "+returnValue   ;
            firstElement = firstElement.GetEClass(); ;
        } while (firstElement.GetEClass() != null);        return returnValue  ; 
    }
}
此程序我不懂的是它为什么用ADDE能加像集合类一样加入元素,用firstElement。GetEClass能取得下一个元素,我怎么看GElementList<T>也都只是一个普通类,而不像是一个集合类,请大家帮我解释一下

解决方案 »

  1.   

    注意这个方法实际上是在构建一个链表    public void AddE(T pElement) 
        { 
            ElementS <T> newElementS = new ElementS <T>(); 
            newElementS.SetElement(pElement); 
            newElementS.SetEClass(firstElement)  ; 
            firstElement = newElementS  ;        
        } 
      

  2.   

    链表依靠现在存储单元和指向下一个单元的引用或指针构造,不是一两句话能说清楚的这是基本的数据结构,需要牢固掌握的请google 数据结构 链表
    知之为知之,不知google之
      

  3.   

    c,c++我学过,那里面的链表是用指针,C#里面是没有的,也可以说是链表?这一套模式我不太明白
      

  4.   

     GElementList <int>  theGElementList =  new GElementList <int>()  ; 
    在这里GElementList <T>被实例化为int类型的,即T是一个泛型类,可以被实例化为任意的类型。
      

  5.   

    public void AddE(T pElement) 
        { 
            ElementS <T> newElementS = new ElementS <T>(); 
            newElementS.SetElement(pElement); 
            newElementS.SetEClass(firstElement)  ; 
            firstElement = newElementS  ;        
        } 
    看到上面这行代码了没?
    就是通过它把链表中的节点加入链表,实现链接~c#里面没有指针,取而代之相同作用的是引用,本质上就是指针