लिस्ट एक अनुक्रम कंटेनर है जो गैर-सन्निहित मेमोरी आवंटन की अनुमति देता है। वेक्टर की तुलना में लिस्ट में धीमा ट्रैवर्सल होता है, लेकिन एक बार स्थिति मिल जाने पर, सम्मिलन और विलोपन त्वरित होते हैं।
फंक्शन और विवरण:
main() से, हमने निम्नलिखित फंक्शन को कॉल किया है:
- l.resize() – लिस्ट का आकार बदलता है।
- l.push_front() – लिस्ट के सामने से तत्व डालने के लिए उपयोग किया जाता है।
- l.remove() – लिस्ट से तत्व हटाता है।
- l.unique() – लिस्ट से डुप्लिकेट तत्व हटाता है।
- l.reverse() – लिस्ट को उलट देता है।
- l.front() – लिस्ट का फ्रंट तत्व लौटाता है।
उदाहरण कोड
#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
...
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