Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ STL में Forward_List का कार्यान्वयन

STL में फॉरवर्ड लिस्ट सिंगली लिंक्ड लिस्ट को लागू करती है। लिस्ट फॉरवर्ड_लिस्ट से अलग है क्योंकि लिस्ट अगले और पिछले दोनों तत्वों का ट्रैक रखती है।

जबकि फॉरवर्ड लिस्ट केवल अगले तत्वों के स्थान का ट्रैक रखती है, इस प्रकार प्रत्येक तत्व को संग्रहीत करने के लिए आवश्यक स्टोरेज स्पेस बढ़ जाता है। फॉरवर्ड_लिस्ट का नुकसान यह है कि व्यक्तिगत तत्वों को सीधे एक्सेस नहीं किया जा सकता और इसे पीछे की ओर पुनरावृत्त नहीं किया जा सकता।

फंक्शन और विवरण:

main() से, हमने निम्नलिखित फंक्शन को कॉल किया है:

  • fl.resize() – फॉरवर्ड_लिस्ट का आकार बदलता है।
  • fl.push_front() – फॉरवर्ड_लिस्ट के सामने से तत्व डालने के लिए उपयोग किया जाता है।
  • fl.remove() – फॉरवर्ड_लिस्ट से तत्व हटाता है।
  • 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 c, n;

    while (1) {
        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:
                n = fl.front();
                fl.pop_front();
                cout << "Element " << n << " deleted" << endl;
                break;
            case 3:
                cout << "Front Element of the Forward List: ";
                cout << fl.front() << endl;
                break;
            case 4:
                cout << "Enter new size of Forward List: ";
                cin >> n;
                if (n <= 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(1);
                break;
            default:
                cout << "Wrong Choice" << endl;
        }
    }
    return 0;
}

आउटपुट

1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Display Forward List
9.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 1
...
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 4 5 8
...
Enter your Choice: 9
Exit code: 1
  1. C++ STL में Set_Symmetric_Difference का कार्यान्वयन C++ STL में Set_Symmetric_Difference का कार्यान्वयन

    यह C++ प्रोग्राम set_symmetric_difference को लागू करने के लिए है। दो सेट का सिमेट्रिक डिफरेंस उन तत्वों द्वारा बनाया जाता है जो एक सेट में मौजूद होते हैं, लेकिन दूसरे में नहीं। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या Exclusive-OR Set Difference या Subtraction

  1. C++ STL में Set_Intersection का कार्यान्वयन C++ STL में Set_Intersection का कार्यान्वयन

    दो सेट का इंटरसेक्शन केवल उन तत्वों द्वारा बनता है जो दोनों सेट में कॉमन होते हैं। फंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेट में तत्व पहले से क्रमबद्ध होने चाहिए। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या Exclusive-OR Set

  1. C++ STL में Set_Difference का कार्यान्वयन C++ STL में Set_Difference का कार्यान्वयन

    दो सेट का डिफरेंस केवल उन तत्वों द्वारा बनता है जो पहले सेट में मौजूद होते हैं, दूसरे सेट में नहीं। फंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेट में तत्व पहले से क्रमबद्ध होने चाहिए। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या