コード例 #1
0
        /// <summary>
        /// 求单链表的长度
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            int length = 0;

            while (Head != null)
            {
                Head = Head.NextNode;
                length++;
            }
            return(length);
        }
コード例 #2
0
        /// <summary>
        /// 追加节点
        /// </summary>
        public void AddAtTail(T item)
        {
            LinkedNode <T> foot = new LinkedNode <T>(item);
            LinkedNode <T> head = new LinkedNode <T>();

            //头部为空
            if (Head == null)
            {
                Head = foot;
                return;
            }

            head = Head;
            while (head.NextNode != null)
            {
                head = head.NextNode;
            }
            head.NextNode = foot;
        }
コード例 #3
0
        /// <summary>
        /// 插入节点
        /// </summary>
        /// <param name="item"></param>
        /// <param name="index"></param>
        public void AddAtIndex(T item, int index)
        {
            int nodeLength = GetLength();

            if (index > nodeLength)
            {
                return;
            }
            //增加到头部前
            if (index <= 0)
            {
                LinkedNode <T> linkedNode = new LinkedNode <T>(item);
                linkedNode.NextNode = Head;
                Head = linkedNode;
                return;
            }

            //增加到末尾
            if (index == nodeLength)
            {
                AddAtTail(item);
            }

            LinkedNode <T> headNode = Head;

            for (int i = 1; i <= index; i++)
            {
                if (headNode.NextNode != null)
                {
                    headNode = headNode.NextNode;
                    if (i == index)
                    {
                        LinkedNode <T> node = new LinkedNode <T>(item);
                        node.NextNode     = headNode.NextNode;
                        headNode.NextNode = node;
                    }
                    else
                    {
                        headNode = headNode.NextNode;
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// 获取节点对应的值
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T Get(int index)
        {
            LinkedNode <T> node = Head;

            if (index == 0)
            {
                return(node.Data);
            }

            int i = 1;

            while (node.NextNode != null)
            {
                if (index == i)
                {
                    return(node.NextNode.Data);
                }

                i++;
            }

            return(default(T));
        }
コード例 #5
0
 public LinkedNode(T item)
 {
     this.Data     = item;
     this.NextNode = null;
 }
コード例 #6
0
 public LinkedNode()
 {
     this.Data     = default(T);
     this.NextNode = null;
 }
コード例 #7
0
 public SingleList()
 {
     Head = null;
 }