日期:2014-05-18  浏览次数:20855 次

C#怎样写链表
链表应该用指针写吧

------解决方案--------------------
系统有array之类的可以直接用嘛 能不能用object来模拟指针 然后将下一个节点封箱 然后boject来指向他
------解决方案--------------------
如果项目中要使用,没有必要自己写,用IList就可以了
------解决方案--------------------
大概方法如下(使用泛型),具体的接口,自己实现即口
C# code

    public class ListNode<T>
    {
        public T Data;
        public T Next;
        public T Previous;
        
        public ListNode(T data,T nextobj,T preobj)
        {
            Data = data;
            Next = nextobj;
            Previous = preobj;
        }
    }
    public class LinkedList<T>:IList<ListNode<T>>
    {
        #region IList<ListNode<T>> Members

        public int IndexOf(ListNode<T> item)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, ListNode<T> item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public ListNode<T> this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection<ListNode<T>> Members

        public void Add(ListNode<T> item)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(ListNode<T> item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(ListNode<T>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(ListNode<T> item)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<ListNode<T>> Members

        public IEnumerator<ListNode<T>> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

------解决方案--------------------
LinkedList
------解决方案--------------------
呵,没注意,在.net中已经实现了链表LinkedList,在System.Collections.Generic命名空间下.
当然初学时作为练习自己写一个,也是可以的.

------解决方案--------------------
如果是项目中需要的话,建议使用.net中自带的类。
如果是自己练习的话,使用类就可以实现
class A{
 public A a;
 public string stra;
 A(A a1){
this.a = a1;
 }
 setA(A a2){
this.a = a2;
}
}
没有环境,基本上是这个样子

------解决方案--------------------
四楼正解~

如果要自己写的话,用List<T>。
------解决方案--------------------
框架里有ArrayList类可以直接用,自己写当然也可以,我还是喜欢直接用,自己写bug比较多。