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

เมธอด RemoveFirst() ของ LinkedList ใน C#

สมมติว่านี่คือ LinkedList ของเราที่มีโหนดเป็นจำนวนเต็ม:

int[] num = {29, 40, 67, 89, 198, 234};
LinkedList<int> myList = new LinkedList<int>(num);

ตอนนี้ หากคุณต้องการลบองค์ประกอบแรกออกจากลิสต์ ให้ใช้เมธอด RemoveFirst():

myList.RemoveFirst();

ตัวอย่าง

using System;
using System.Collections.Generic;

class Demo
{
    static void Main()
    {
        int[] num = {29, 40, 67, 89, 198, 234};
        LinkedList<int> myList = new LinkedList<int>(num);
        
        foreach (var n in myList)
        {
            Console.WriteLine(n);
        }
        
        // ลบโหนดแรก
        myList.RemoveFirst();
        Console.WriteLine("LinkedList after removing the first node...");
        
        foreach (var n in myList)
        {
            Console.WriteLine(n);
        }
    }
}

ผลลัพธ์

29
40
67
89
198
234
LinkedList after removing the first node...
40
67
89
198
234