รายการคือคอนเทนเนอร์ลำดับที่อนุญาตการจัดสรรหน่วยความจำที่ไม่ต่อเนื่องกัน รายการมีการข้ามผ่านช้าเมื่อเทียบกับเวกเตอร์ แต่เมื่อพบตำแหน่งแล้ว การแทรกและการลบจะทำได้อย่างรวดเร็ว
ฟังก์ชันและคำอธิบาย:
From main(), we have called following functions: fl.resize() = Returns the resize of list. fl.push_front() = It is used to push elements into a list from the front. fl.remove() = Deletes elements from list. fl.unique() = Deletes duplicate elements from list. fl.reverse() = Reverses the list. fl.front() = Returns the front element of 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 (1) { 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: i= l.front(); l.pop_front(); cout<<"Element "<<i<<" deleted"<<endl; break; case 4: i= l.back(); l.pop_back(); cout<<"Element "<<i<<" deleted"<<endl; break; case 5: cout<<"Front Element of the List: "; cout<<l.front()<<endl; break; case 6: cout<<"Last Element of the List: "; cout<<l.back()<<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 <= 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(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
ผลลัพธ์
1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 1 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 2 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 3 Element 2 deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 4 Element 1 deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 2 Enter value to be inserted at the end: 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 6 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 7 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 1 Enter value to be inserted at the front: 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 4 5 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 10 Duplicate Items Deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 5 Front Element of the List: 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 4 5 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 11 List reversed 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 12 Elements of the List: 5 6 7 5 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit Enter your Choice: 13