Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การเพิ่มโหนดใหม่ที่จุดเริ่มต้นของ LinkedList ใน C#

ภาพรวม

LinkedList<T>.AddFirst(T value) เพิ่มโหนดใหม่ที่จุดเริ่มต้น (head) ของลิสต์ ทำให้โหนดเดิมเลื่อนไปเป็นตัวที่สอง

ตัวอย่างที่ 1: เพิ่มข้อมูลหลายตัวแล้วเพิ่มที่หน้าสุด

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        LinkedList<string> list = new LinkedList<string>();
        list.AddLast("A");
        list.AddLast("B");
        list.AddLast("C");
        list.AddLast("D");
        list.AddLast("E");
        list.AddLast("F");

        Console.WriteLine("Count of nodes = " + list.Count);
        Console.WriteLine("Elements in LinkedList...");
        foreach (string res in list)
        {
            Console.WriteLine(res);
        }

        list.AddLast("G");
        list.AddLast("H");
        list.AddLast("I");

        Console.WriteLine("Count of nodes...UPDATED = " + list.Count);
        Console.WriteLine("Elements in LinkedList...UPDATED");
        foreach (string res in list)
        {
            Console.WriteLine(res);
        }

        list.AddFirst("AA");

        Console.WriteLine("\nWe added a node in the beginning...");
        Console.WriteLine("Count of nodes...UPDATED = " + list.Count);
        Console.WriteLine("Elements in LinkedList...UPDATED");
        foreach (string res in list)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

Count of nodes = 6
Elements in LinkedList...
A
B
C
D
E
F
Count of nodes...UPDATED = 9
Elements in LinkedList...UPDATED
A
B
C
D
E
F
G
H
I
We added a node in the beginning...
Count of nodes...UPDATED = 10
Elements in LinkedList...UPDATED
AA
A
B
C
D
E
F
G
H
I

ตัวอย่างที่ 2: เพิ่ม 2 โหนดที่หน้าสุดของ LinkedList ตัวเลข

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        LinkedList<int> list = new LinkedList<int>();
        list.AddLast(100);
        list.AddLast(200);
        list.AddLast(300);
        list.AddLast(400);

        Console.WriteLine("Count of nodes = " + list.Count);
        Console.WriteLine("Elements in LinkedList...");
        foreach (int res in list)
        {
            Console.WriteLine(res);
        }

        list.AddFirst(500);
        list.AddFirst(600);

        Console.WriteLine("\nWe added a node in the beginning...");
        Console.WriteLine("Count of nodes...UPDATED = " + list.Count);
        Console.WriteLine("Elements in LinkedList...UPDATED");
        foreach (int res in list)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

Count of nodes = 4
Elements in LinkedList...
100
200
300
400
We added a node in the beginning...
Count of nodes...UPDATED = 6
Elements in LinkedList...UPDATED
600
500
100
200
300
400

สรุป

  • AddFirst() เพิ่มที่ head, AddLast() เพิ่มที่ tail
  • ยังมี AddBefore() และ AddAfter() สำหรับเพิ่มหน้า/หลังโหนดที่ระบุ
  • LinkedList เหมาะสำหรับการแทรก/ลบที่หน้า/กลาง/ท้าย บ่อยครั้ง