Forward List ใน STL ใช้งานเป็น singly linked list (ลิงก์ลิสต์แบบเดียว) ต่างจาก list ที่เป็น doubly linked list (คู่) ซึ่ง list เก็บ pointer ไปทั้ง next และ previous แต่ forward_list เก็บแค่ next เท่านั้น ทำให้ประหยัดหน่วยความจำมากกว่า
ข้อจำกัด: ไม่สามารถเข้าถึงองค์ประกอบแบบสุ่ม (random access) ได้ และไม่สามารถวนลูปย้อนหลัง (backwards iteration) ได้
ฟังก์ชันและคำอธิบาย
- fl.resize() – ปรับขนาดของ forward_list (เติมค่า 0 หากขยาย)
- fl.push_front() – แทรกองค์ประกอบจากหน้า (ไม่มี push_back)
- fl.pop_front() – ลบองค์ประกอบจากหน้า
- fl.remove() – ลบองค์ประกอบที่มีค่าเท่ากับที่กำหนด
- fl.unique() – ลบองค์ประกอบซ้ำที่อยู่ติดกัน
- fl.reverse() – กลับลำดับ forward_list
- fl.front() – คืนองค์ประกอบหน้าสุด
- fl.max_size() – คืนขนาดสูงสุดที่เป็นไปได้
โค้ดตัวอย่าง (โปรแกรมโต้ตอบแบบเมนู)
#include <iostream>
#include <forward_list>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
forward_list<int> fl;
forward_list<int>::iterator it;
int c, n;
while (true) {
cout << "1.Insert Element at the Front" << endl;
cout << "2.Delete Element at the Front" << endl;
cout << "3.Front Element of Forward List" << endl;
cout << "4.Resize Forward List" << endl;
cout << "5.Remove Elements with Specific Values" << endl;
cout << "6.Remove Duplicate Values" << endl;
cout << "7.Reverse the order of elements" << endl;
cout << "8.Display Forward List" << endl;
cout << "9.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch(c) {
case 1:
cout << "Enter value to be inserted at the front: ";
cin >> n;
fl.push_front(n);
break;
case 2:
if (!fl.empty()) {
n = fl.front();
fl.pop_front();
cout << "Element " << n << " deleted" << endl;
} else cout << "Forward List is empty" << endl;
break;
case 3:
if (!fl.empty()) cout << "Front Element of the Forward List: " << fl.front() << endl;
else cout << "Forward List is empty" << endl;
break;
case 4:
cout << "Enter new size of Forward List: ";
cin >> n;
if (n <= (int)fl.max_size()) fl.resize(n);
else fl.resize(n, 0);
break;
case 5:
cout << "Enter element to be deleted: ";
cin >> n;
fl.remove(n);
break;
case 6:
fl.unique();
cout << "Duplicate Items Deleted" << endl;
break;
case 7:
fl.reverse();
cout << "Forward List reversed" << endl;
break;
case 8:
cout << "Elements of Forward List: ";
for (it = fl.begin(); it != fl.end(); it++) cout << *it << " ";
cout << endl;
break;
case 9:
exit(0);
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
ตัวอย่างผลลัพธ์ (บางส่วน)
Enter your Choice: 1 Enter value to be inserted at the front: 1 Enter your Choice: 1 Enter value to be inserted at the front: 2 Enter your Choice: 1 Enter value to be inserted at the front: 3 Enter your Choice: 3 Front Element of the Forward List: 3 Enter your Choice: 4 Enter new size of Forward List: 6 Enter your Choice: 8 Elements of Forward List: 3 2 0 0 0 Enter your Choice: 7 Forward List reversed Enter your Choice: 8 Elements of Forward List: 0 0 0 2 3 Enter your Choice: 9 Exit code: 0
สรุป: ใช้ forward_list เมื่อต้องการ linked list แบบเบา (memory efficient) และต้องการแทรก/ลบที่หน้าบ่อย ไม่ต้องการ random access หรือ backward iteration