Ejemplo n.º 1
0
        public static void Add_k_S(int x, int k)
        {
            Node_S Sp = new Node_S();

            Sp.Info = x;
            int i = 1;
            int l = Length_S();

            if (k < 1 || k > l + 1)
            {
                Console.WriteLine("Vi tri chen khong hop le");
            }
            else
            {
                if (k == 1)
                {
                    AddFirst_S(x);
                }
                else
                {
                    Node_S Sq = SL;
                    while (Sq.Next != null && i != k - 1)
                    {
                        i++;
                        Sq = Sq.Next;
                    }
                    Sp.Next = Sq.Next;
                    Sq.Next = Sp;
                }
            }
        }
Ejemplo n.º 2
0
        public static void Delete_k_S(int k)
        {
            Node_S Sq = SL;
            int    i  = 1;

            if (k < 1 || k > Length_S())
            {
                Console.WriteLine("Vi tri xoa khong hop le");
            }
            else
            {
                if (k == 1)
                {
                    DeleteFirst_S();
                }
                else
                {
                    while (Sq != null && i != k - 1)
                    {
                        Sq = Sq.Next;
                        i++;
                    }
                    Sq.Next = Sq.Next.Next;
                }
            }
        }
Ejemplo n.º 3
0
 public static void DeleteFirst_S()
 {
     if (SL != null)
     {
         SL = SL.Next;
     }
 }
Ejemplo n.º 4
0
        public static void Traverse_S()
        {
            Node_S Sq = SL;

            while (Sq != null)
            {
                Console.WriteLine("Info: {0}", Sq.Info);
                Sq = Sq.Next;
            }
        }
Ejemplo n.º 5
0
        public static Node_D RL = null; // Initial, RL (Right List) quản lý danh sách liên kết kép

        /// Danh sách liên kết đơn
        public static int Length_S()
        {
            Node_S Sp = SL;
            int    i  = 0;

            while (Sp != null)
            {
                i++;
                Sp = Sp.Next;
            }
            return(i);
        }
Ejemplo n.º 6
0
        // Bổ sung 1 nút vào đầu dslk đơn được trỏ bởi SL
        public static void AddFirst_S(int x)
        {
            Node_S Sp = new Node_S(); // Tạo một nút (không có tên) có cấu trúc Node_S được trỏ bởi Sp

            // (Nghĩa là: Địa chỉ của node vừa tạo nằm trong con trỏ Sp)
            // Địa chỉ của node là một số nguyên.
            // Sp là con trỏ, con trỏ là một biến đặc biệt, chứa địa chỉ của biến.
            // Một biến bất kỳ đều cần phải có địa chỉ.
            // Máy tính truy cập theo địa chỉ.
            // Để truy cập một biến hoặc dùng tên biến hoặc dùng địa chỉ của biến
            // Địa chỉ của biến chỉ có thể nằm trong con trỏ.

            Sp.Info = x; // Truy cập (gán = x) vào trường Info của nút vừa tạo bằng con trỏ Sp
            Sp.Next = SL;
            SL      = Sp;
        }
Ejemplo n.º 7
0
        public static Node_S Search(int x)
        {
            Node_S Sq = SL;

            while (Sq != null && Sq.Info != x)
            {
                Sq = Sq.Next;
            }
            if (Sq != null)
            {
                return(Sq);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 8
0
        public static int Search_S(int x)
        {
            Node_S Sq  = SL;
            int    pos = 1;

            while (Sq != null && Sq.Info != x)
            {
                Sq = Sq.Next;
                pos++;
            }
            if (Sq != null)
            {
                return(pos);
            }
            else
            {
                return(0);
            }
        }
Ejemplo n.º 9
0
 public static void DeleteLast_S()
 {
     if (SL == null)
     {
         SL = SL.Next;
     }
     else if (SL.Next == null)
     {
         SL = null;
     }
     else
     {
         Node_S Sq = SL;
         while (Sq.Next.Next != null)
         {
             Sq = Sq.Next;
         } // Sq trỏ vào áp chót
         Sq.Next = null;
     }
 }
Ejemplo n.º 10
0
        public static void AddLast_S(int x)
        {
            Node_S Sp = new Node_S();

            Sp.Next = null;
            Sp.Info = x;
            if (SL == null)
            {
                SL = Sp;
            }
            else // Tìm phần tử cuối
            {
                Node_S Sq = SL; // Tạo một con trỏ trung gian để duyệt danh sách SL.
                while (Sq.Next != null)
                {
                    Sq = Sq.Next;
                }
                // Sq sẽ trỏ vào phần tử cuối danh sách.
                Sq.Next = Sp;
            }
        }