কম্পিউটার টিউটোরিয়াল

C++ STL-এ Forward List বাস্তবায়ন: সিঙ্গলি লিংকড লিস্ট

C++ STL-এ forward_list সিঙ্গলি লিংকড লিস্ট বাস্তবায়ন করে। সাধারণ list (ডাবলি লিংকড) এর সাথে পার্থক্য: forward_list শুধু পরবর্তী (next) নোডের ঠিকানা রাখে, পূর্ববর্তী (prev) নয়। ফলে মেমরি কম লাগে কিন্তু ব্যাকওয়ার্ড ট্রাভার্সাল সম্ভব নয় এবং র‍্যান্ডম অ্যাক্সেসও নেই।

প্রধান ফাংশন ও বর্ণনা:

  • fl.resize() – ফরওয়ার্ড লিস্টের আকার পরিবর্তন করে।
  • fl.push_front() – সামনে উপাদান যোগ করে (পিছনে যোগ করার ফাংশন নেই)।
  • fl.pop_front() – সামনের উপাদান মুছে ফেলে।
  • fl.remove(val) – নির্দিষ্ট মানের সব উপাদান মুছে ফেলে।
  • fl.unique() – সटी দুপ্লিকেট মুছে ফেলে (সর্টেড থাকলে সব ডুপ্লিকেট)۔
  • fl.reverse() – লিস্ট উল্টে দেয়।
  • fl.front() – সামনের উপাদান রিটার্ন করে।

উদাহরণ কোড: ইন্টারেক্টিভ ফরওয়ার্ড লিস্ট প্রোগ্রাম

#include <iostream>
#include <forward_list>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    forward_list<int> fl;
    forward_list<int>::iterator it;
    int choice, value;

    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 >> choice;

        switch (choice) {
            case 1:
                cout << "Enter value to be inserted at the front: ";
                cin >> value;
                fl.push_front(value);
                break;
            case 2:
                if (!fl.empty()) {
                    value = fl.front();
                    fl.pop_front();
                    cout << "Element " << value << " deleted" << endl;
                } else cout << "List is empty" << endl;
                break;
            case 3:
                if (!fl.empty()) cout << "Front Element: " << fl.front() << endl;
                else cout << "List is empty" << endl;
                break;
            case 4:
                cout << "Enter new size of Forward List: ";
                cin >> value;
                if (value <= (int)fl.max_size())
                    fl.resize(value);
                else
                    fl.resize(value, 0);
                break;
            case 5:
                cout << "Enter element to be deleted: ";
                cin >> value;
                fl.remove(value);
                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: 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 4 5 8
Enter your Choice: 9
Exit code: 1

নোট: resize(n) কল করলে আকার বড় হলে ০ দিয়ে ফিল করে, ছোট হলে শেষের উপাদানগুলো মুছে ফেলে। forward_list-এ push_back বা back() নেই কারণ টেইল পয়েন্টার রাখা হয় না।

  1. C++ STL-এ Set Union বাস্তবায়ন: দুটি সেটের ইউনিয়ন

  2. C++ STL-এ Set Symmetric Difference বাস্তবায়ন

  3. C++ STL-এ Set Intersection বাস্তবায়ন: দুটি সেটের অন্তঃসেট

  4. C++ STL-এ Set Difference বাস্তবায়ন: সেট বিয়োগ