डबल एंडेड कतार एक कतार डेटा संरचना है जिसमें सम्मिलन और विलोपन ऑपरेशन दोनों सिरों (फ्रंट और रियर) पर किए जाते हैं। डेटा को दोनों फ्रंट और रियर स्थितियों में डाला जा सकता है और दोनों फ्रंट और रियर स्थितियों से हटाया जा सकता है।
एल्गोरिदम
Begin
डीक्यू वेक्टर और इटरेटर घोषित करें।
पसंद के अनुसार इनपुट लें।
स्विच ऑपरेशन के भीतर फंक्शन कॉल करें:
d.size() = कतार का आकार लौटाता है।
d.push_back() = डीक्यू के पीछे से तत्व डालने के लिए उपयोग किया जाता है।
d.push_front() = डीक्यू के सामने से तत्व डालने के लिए उपयोग किया जाता है।
d.pop_back() = डीक्यू के पीछे से तत्व हटाने के लिए उपयोग किया जाता है।
d.pop_front() = डीक्यू के सामने से तत्व हटाने के लिए उपयोग किया जाता है।
d.front() = डीक्यू के फ्रंट तत्व लौटाता है।
d.back() = डीक्यू के बैक तत्व लौटाता है।
डीक्यू के तत्वों को प्रिंट करें।
End.
उदाहरण कोड
#include <iostream>
#include <deque>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
deque<int> d;
deque<int>::iterator it;
int c, item;
while (1) {
cout << "1.Size of the Deque" << endl;
cout << "2.Insert Element at the End" << endl;
cout << "3.Insert Element at the Front" << endl;
cout << "4.Delete Element at the End" << endl;
cout << "5.Delete Element at the Front" << endl;
cout << "6.Front Element at Deque" << endl;
cout << "7.Last Element at Deque" << endl;
cout << "8.Display Deque" << endl;
cout << "9.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch(c) {
case 1:
cout << "Size of the Deque: " << d.size() << endl;
break;
case 2:
cout << "Enter value to be inserted at the end: ";
cin >> item;
d.push_back(item);
break;
case 3:
cout << "Enter value to be inserted at the front: ";
cin >> item;
d.push_front(item);
break;
case 4:
item = d.back();
d.pop_back();
cout << "Element " << item << " deleted" << endl;
break;
case 5:
item = d.front();
d.pop_front();
cout << "Element " << item << " deleted" << endl;
break;
case 6:
cout << "Front Element of the Deque: ";
cout << d.front() << endl;
break;
case 7:
cout << "Back Element of the Deque: ";
cout << d.back() << endl;
break;
case 8:
cout << "Elements of Deque: ";
for (it = d.begin(); it != d.end(); it++)
cout << *it << " ";
cout << endl;
break;
case 9:
exit(1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
आउटपुट
1.Size of the Deque
2.Insert Element at the End
3.Insert Element at the Front
4.Delete Element at the End
5.Delete Element at the Front
6.Front Element at Deque
7.Last Element at Deque
8.Display Deque
9.Exit
Enter your Choice: 1
Size of the Deque: 0
...
Enter your Choice: 2
Enter value to be inserted at the end: 1
...
Enter your Choice: 3
Enter value to be inserted at the front: 2
...
Enter your Choice: 6
Front Element of the Deque: 2
...
Enter your Choice: 8
Elements of Deque: 5 2 1 4
...
Enter your Choice: 9