读书人

C#怎样写链表解决思路

发布时间: 2012-01-14 20:02:35 作者: rapoo

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比较多。

读书人网 >C#

热点推荐