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

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

ขั้นแรก ให้ประกาศ LinkedList และเพิ่มโหนด:

int[] num = {92, 98, 110, 130, 145, 170, 230, 240, 250, 300, 330};
LinkedList<int> myList = new LinkedList<int>(num);

ลบโหนดสุดท้ายจาก LinkedList โดยใช้เมธอด RemoveLast():

myList.RemoveLast();

ตัวอย่าง

using System;
using System.Collections.Generic;

class Demo
{
    static void Main()
    {
        int[] num = {92, 98, 110, 130, 145, 170, 230, 240, 250, 300, 330};
        LinkedList<int> myList = new LinkedList<int>(num);
        
        foreach (var n in myList)
        {
            Console.WriteLine(n);
        }
        
        // ลบโหนดสุดท้าย
        myList.RemoveLast();
        Console.WriteLine("LinkedList after removing the last node...");
        
        foreach (var n in myList)
        {
            Console.WriteLine(n);
        }
    }
}

ผลลัพธ์

92
98
110
130
145
170
230
240
250
300
330
LinkedList after removing the last node...
92
98
110
130
145
170
230
240
250
300