Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức AddAfter() của LinkedList trong C#

LinkedList<T>.AddAfter(node, value) thêm node mới ngay sau node tham chiếu chỉ định. Trả về node mới tạo.

Ví dụ minh họa

using System;
using System.Collections.Generic;

class Demo
{
    static void Main()
    {
        int[] num = { 1, 2, 3, 4, 5 };
        LinkedList<int> list = new LinkedList<int>(num);

        foreach (var n in list)
            Console.WriteLine(n);

        var newNode = list.AddLast(20);
        list.AddAfter(newNode, 30);

        Console.WriteLine("LinkedList sau khi thêm node...");
        foreach (var n in list)
            Console.WriteLine(n);
    }
}

Kết quả đầu ra

1
2
3
4
5
LinkedList sau khi thêm node...
1
2
3
4
5
20
30

Phương thức liên quan: AddBefore(node, value) (thêm trước), AddFirst(value), AddLast(value).