List เป็นคอนเทนเนอร์แบบ sequence ที่อนุญาตการจัดสรรหน่วยความจำแบบไม่ต่อเนื่อง (non-contiguous) List มีการวนลูปช้ากว่า Vector แต่เมื่อเจอตำแหน่งแล้ว การแทรกและลบจะรวดเร็ว (O(1))
ฟังก์ชันและคำอธิบาย
- l.resize() – ปรับขนาดของ list
- l.push_front() – แทรกองค์ประกอบเข้า list จากหน้า
- l.push_back() – แทรกองค์ประกอบเข้า list จากท้าย
- l.pop_front() – ลบองค์ประกอบจากหน้า
- l.pop_back() – ลบองค์ประกอบจากท้าย
- l.remove() – ลบองค์ประกอบที่มีค่าเท่ากับที่กำหนด (ลบทุกตัวที่ตรง)
- l.unique() – ลบองค์ประกอบซ้ำที่อยู่ติดกัน (ต้อง sort ก่อนถ้าต้องการลบซ้ำทั้งหมด)
- l.reverse() – กลับลำดับ list
- l.front() – คืนองค์ประกอบหน้าสุด
- l.back() – คืนองค์ประกอบท้ายสุด
- l.size() – คืนขนาดของ list
โค้ดตัวอย่าง (โปรแกรมโต้ตอบแบบเมนูครบครัน)
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
list<int> l;
list<int>::iterator it;
int c, i;
while (true) {
cout << "1.Insert Element at the Front" << endl;
cout << "2.Insert Element at the End" << endl;
cout << "3.Delete Element at the Front" << endl;
cout << "4.Delete Element at the End" << endl;
cout << "5.Front Element of List" << endl;
cout << "6.Last Element of the List" << endl;
cout << "7.Size of the List" << endl;
cout << "8.Resize List" << endl;
cout << "9.Remove Elements with Specific Values" << endl;
cout << "10.Remove Duplicate Values" << endl;
cout << "11.Reverse the order of elements" << endl;
cout << "12.Display the List" << endl;
cout << "13.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch(c) {
case 1:
cout << "Enter value to be inserted at the front: ";
cin >> i;
l.push_front(i);
break;
case 2:
cout << "Enter value to be inserted at the end: ";
cin >> i;
l.push_back(i);
break;
case 3:
if (!l.empty()) {
i = l.front();
l.pop_front();
cout << "Element " << i << " deleted" << endl;
} else cout << "List is empty" << endl;
break;
case 4:
if (!l.empty()) {
i = l.back();
l.pop_back();
cout << "Element " << i << " deleted" << endl;
} else cout << "List is empty" << endl;
break;
case 5:
if (!l.empty()) cout << "Front Element of the List: " << l.front() << endl;
else cout << "List is empty" << endl;
break;
case 6:
if (!l.empty()) cout << "Last Element of the List: " << l.back() << endl;
else cout << "List is empty" << endl;
break;
case 7:
cout << "Size of the List: " << l.size() << endl;
break;
case 8:
cout << "Enter new size of the List: ";
cin >> i;
if (i <= (int)l.size()) l.resize(i);
else l.resize(i, 0);
break;
case 9:
cout << "Enter element to be deleted: ";
cin >> i;
l.remove(i);
break;
case 10:
l.unique();
cout << "Duplicate Items Deleted" << endl;
break;
case 11:
l.reverse();
cout << "List reversed" << endl;
break;
case 12:
cout << "Elements of the List: ";
for (it = l.begin(); it != l.end(); it++) cout << *it << " ";
cout << endl;
break;
case 13:
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: 3 Element 2 deleted Enter your Choice: 2 Enter value to be inserted at the end: 5 Enter your Choice: 1 Enter value to be inserted at the front: 6 Enter your Choice: 1 Enter value to be inserted at the front: 7 Enter your Choice: 12 Elements of the List: 7 6 5 Enter your Choice: 10 Duplicate Items Deleted Enter your Choice: 11 List reversed Enter your Choice: 12 Elements of the List: 5 6 7 Enter your Choice: 13
หมายเหตุ: unique() ลบเฉพาะค่าที่ซ้ำและอยู่ติดกัน หากต้องการลบซ้ำทั้งหมด ต้อง sort() ก่อน แล้วค่อย unique()