xiaxia

泛型實例-1


  1.  

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections.Generic;

    /// <summary>
    /// Summary description for testGenericList
    /// </summary>

    public class GenericList<T>
    {

        
    private class Node
        
    {      
            
    private Node next;
            
    public Node Next
            
    {
                
    get return next; }
                
    set { next = value; }
            }


            
    private T data;
            
    public T Data
            
    {
                
    get return data; }
                
    set { data = value; }
            }

            
    public Node(T t)
            
    {
                next 
    = null;
                data 
    = t;
            }

        }



        
    private Node head;

        
    public GenericList()
        
    {
            head 
    = null;
        }


        
    public void AddHead(T t)
        
    {

            Node n 
    = new Node(t);//New 一個新Node參數為 t
            n.Next = head;//新節點的Next賦值為Null-------------因為 head = null;
            head = n;//GenericList的Head節點插入新節點n
        }


        
    //迭代一個泛型集合
        public IEnumerator<T> GetEnumerator()
        
    {
            Node current 
    = head;

            
    while (current != null)
            
    {
                yield 
    return current.Data;
                current 
    = current.Next;
            }

        }


    }

     

posted on 2007-09-29 09:00  Array  阅读(201)  评论(0编辑  收藏  举报

导航