Example #1
0
 public static void DeleteFirst_D()
 {
     if (LL != null)
     {
         LL = LL.Rptr;
     }
 }
Example #2
0
        /// Danh sách liên kết kép
        public static int Length_D()
        {
            Node_D DL = LL; // Tạo con trỏ DL duyệt danh sách từ phía tay trái
            Node_D DR = RL; // Tạo con trỏ DR duyệt danh sách từ phía tay phải
            int    i  = 0;

            if (DL != null)
            {
                i = 1;
            }
            while (DL != null)
            {
                if (DL == DR)
                {
                    break;
                }
                DL = DL.Rptr;
                i++;
                if (DR == DL)
                {
                    break;
                }
                DR = DR.Lptr;
                i++;
            }
            return(i);
        }
Example #3
0
 public static void DeleteLast_D()
 {
     if (RL != null)
     {
         RL      = RL.Lptr;
         RL.Rptr = null;
     }
 }
Example #4
0
        public static void Traverse_D()
        {
            Node_D Dq = LL;

            while (Dq != RL.Rptr)
            {
                Console.WriteLine("Info: {0}", Dq.Info);
                Dq = Dq.Rptr;
            }
        }
Example #5
0
        public static void AddLast_D(int x)
        {
            Node_D Dp = new Node_D();

            Dp.Info = x;
            if (RL == null)
            {
                LL = Dp;
                RL = Dp;
            }
            else
            {
                RL.Rptr = Dp;
                Dp.Lptr = RL;
                RL      = Dp;
            }
        }
Example #6
0
        public static void AddFirst_D(int x)
        {
            Node_D Dp = new Node_D();

            Dp.Info = x;
            if (LL == null)
            {
                LL = Dp;
                RL = Dp;
            }
            else
            {
                Dp.Rptr = LL;
                //LL.Lptr = Dp;
                LL      = Dp;
                Dp.Lptr = null;
            }
        }
Example #7
0
        public static int Search_D(int x)
        {
            Node_D Dq  = LL;
            int    pos = 1;

            while (Dq != null && Dq.Info != x)
            {
                Dq = Dq.Rptr;
                pos++;
            }
            if (Dq != null)
            {
                return(pos);
            }
            else
            {
                return(0);
            }
        }
Example #8
0
        public static void Add_k_D(int x, int k)
        {
            Node_D DL = LL;
            Node_D DR;
            Node_D Dp = new Node_D();

            Dp.Info = x;
            int i = 1, l = Length_D();

            if (k < 1 || k > l + 1)
            {
                Console.WriteLine("Vi tri chen khong hop le");
            }
            else
            {
                if (k == 1)
                {
                    AddFirst_D(x);
                }
                else if (k == l)
                {
                    AddLast_D(x);
                }
                else
                {
                    while (DL != null && i != k - 1)
                    {
                        i++;
                        DL = DL.Rptr;
                    }
                    DR      = DL.Rptr;
                    Dp.Rptr = DR;
                    Dp.Lptr = DL;
                    DL.Rptr = Dp;
                    DR.Lptr = Dp;
                }
            }
        }
Example #9
0
        public static void Delete_k_D(int k)
        {
            Node_D DL = LL;
            Node_D DR;
            int    i = 1, l = Length_D();

            if (k < 1 || k > l)
            {
                Console.WriteLine("Vi tri xoa khong hop le");
            }
            else
            {
                if (k == 1)
                {
                    DeleteFirst_D();
                }
                else
                {
                    if (k == l)
                    {
                        DeleteLast_D();
                    }
                    else
                    {
                        while (DL != null && i != k - 1)
                        {
                            i++;
                            DL = DL.Rptr;
                        }
                        DR      = DL.Rptr.Rptr;
                        DL.Rptr = DR;
                        DR.Lptr = DL;
                    }
                }
            }
        }