List একটি সিকোয়েন্স কন্টেইনার যা নন-কন্টিগুয়াস মেমরি অ্যালোকেশন অনুমতি দেয়। এটি ডাবলি লিংকড লিস্ট হিসেবে বাস্তবায়িত। ভেক্টরের চেয়ে ট্রাভার্সাল ধীর, কিন্তু একবার পজিশন পাওয়া গেলে ইনসারশন এবং ডিলিশন খুব দ্রুত (O(1))।
প্রধান ফাংশন ও বর্ণনা:
- l.resize() – লিস্টের আকার পরিবর্তন করে।
- l.push_front() – লিস্টের সামনে উপাদান যোগ করে।
- l.push_back() – লিস্টের শেষে উপাদান যোগ করে।
- l.pop_front() – সামনের উপাদান মুছে ফেলে।
- l.pop_back() – পিছনের উপাদান মুছে ফেলে।
- l.front() – সামনের উপাদান রিটার্ন করে।
- l.back() – পিছনের উপাদান রিটার্ন করে।
- l.remove(val) – নির্দিষ্ট মানের সব উপাদান মুছে ফেলে।
- l.unique() – সুনির্দিষ্ট আকারে ডুপ্লিকেট উপাদান মুছে ফেলে (লিস্ট সর্টেড থাকতে হয়)।
- l.reverse() – লিস্ট উল্টে দেয়।
উদাহরণ কোড: পূর্ণাঙ্গ ইন্টারেক্টিভ লিস্ট প্রোগ্রাম
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
list<int> l;
list<int>::iterator it;
int choice, value;
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 >> choice;
switch (choice) {
case 1:
cout << "Enter value to be inserted at the front: ";
cin >> value;
l.push_front(value);
break;
case 2:
cout << "Enter value to be inserted at the end: ";
cin >> value;
l.push_back(value);
break;
case 3:
if (!l.empty()) {
value = l.front();
l.pop_front();
cout << "Element " << value << " deleted" << endl;
} else cout << "List is empty" << endl;
break;
case 4:
if (!l.empty()) {
value = l.back();
l.pop_back();
cout << "Element " << value << " deleted" << endl;
} else cout << "List is empty" << endl;
break;
case 5:
if (!l.empty()) cout << "Front Element: " << l.front() << endl;
else cout << "List is empty" << endl;
break;
case 6:
if (!l.empty()) cout << "Last Element: " << 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 >> value;
if (value <= (int)l.size())
l.resize(value);
else
l.resize(value, 0); // নতুন জায়গা 0 দিয়ে ফিল
break;
case 9:
cout << "Enter element to be deleted: ";
cin >> value;
l.remove(value);
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: 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 5 4 Enter your Choice: 13
কবে লিস্ট ব্যবহার করবেন: যাবতীয় ইনসারশন/ডিলিশন যেকোনো পজিশনে (O(1)) এবং র্যান্ডম অ্যাক্সেস দরকার নেই। ভেক্টর থেকে ভিন্ন, লিস্টে ইন্ডেক্স অ্যাক্সেস নেই (O(n))।