Example #1
0
        public DoubleLinkList Delete(DoubleLinkList node)
        {
            if (node != null)
            {
                DoubleLinkList p = this;

                // 当p不是最后一个节点,p也不是目标节点
                // 我们就继续往后扫描
                while (p.Next != null && p != node)
                {
                    p = p.Next;
                }

                if (p == node)
                {
                    // 待删除的节点是第一个节点
                    // 需要特别处理
                    if (p == this)
                    {
                        // 这个时候表明该算法的签名有问题哈,我们需要把它改成有返回值的
                        // 否则我们不能删除自己

                        p      = p.Next;
                        p.Last = null;

                        return(p);
                    }
                    else
                    {
                        p.Last.Next = p.Next;

                        if (p.Next != null)
                        {
                            p.Next.Last = p.Last;
                        }

                        //if (p.Next != null)
                        //{
                        //    p.Last.Next = p.Next;
                        //    p.Next.Last = p.Last;
                        //}
                        //else
                        //{
                        //    p.Last.Next = p.Next;
                        //}
                    }
                }
            }
            return(this);
        }
Example #2
0
        public void InsertAtEnd(DoubleLinkList node)
        {
            if (node != null)
            {
                DoubleLinkList p = this;

                while (p.Next != null)
                {
                    p = p.Next;
                }

                p.Next    = node;
                node.Last = p;
                node.Next = null;
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            DoubleLinkList node1 = new DoubleLinkList(1);

            node1.InsertAtEnd(new DoubleLinkList(2));
            node1.InsertAtEnd(new DoubleLinkList(3));
            DoubleLinkList node4 = new DoubleLinkList(4);

            node1.InsertAtEnd(node4);
            node1.InsertAtEnd(new DoubleLinkList(5));
            node1.InsertAtEnd(new DoubleLinkList(6));

            DoubleLinkList node7 = new DoubleLinkList(7);

            node1.InsertAtEnd(node7);

            DoubleLinkList node = node1.Delete(node4);

            node.Delete(node7);
        }