Deque क्या है
Deque डबल एंडेड कतारें हैं जो अनुक्रम कंटेनर हैं जो दोनों छोरों पर विस्तार और संकुचन की कार्यक्षमता प्रदान करती हैं। एक कतार डेटा संरचना उपयोगकर्ता को केवल अंत में डेटा डालने और सामने से डेटा हटाने की अनुमति देती है। बस स्टॉप पर कतारों की सादृश्यता लें जहां व्यक्ति को केवल अंत से कतार में डाला जा सकता है और सामने खड़ा व्यक्ति सबसे पहले हटाया जाता है, जबकि डबल एंडेड कतार में डेटा का सम्मिलन और विलोपन दोनों छोरों पर संभव है।
deque resize() फ़ंक्शन क्या है
deque resize() फ़ंक्शन का उपयोग deque का आकार बदलने के लिए किया जाता है। यदि आकार वर्तमान आकार से बड़ा है, तो नए तत्व deque के अंत में डाले जाते हैं। यदि दिया गया आकार वर्तमान आकार से छोटा है, तो अतिरिक्त तत्व हटा दिए जाते हैं।
सिं्टैक्स
dequename.resize(n)
dequename.resize(n)
n: यह deque का आकार परिभाषित करता है
उदाहरण
इनपुट वर्तमान आकार - 5
Deque - 12 13 14 15 16
आउटपुट आकार बदलने के बाद आकार - 7
नया Deque - 11 12 13 14 15 16 17
इनपुट वर्तमान आकार - 5
Deque - F O R C E
आउटपुट आकार बदलने के बाद आकार - 4
नया Deque - F O R C
अनुसरण किया जाने वाला दृष्टिकोण
- पहले हम deque घोषित करते हैं।
- फिर हम deque का आकार जांचते हैं।
- फिर हम deque प्रिंट करते हैं।
- फिर हम resize() फ़ंक्शन परिभाषित करते हैं
- फिर हम आकार बदलने के बाद नया deque प्रिंट करते हैं।
उपरोक्त दृष्टिकोण का उपयोग करके हम deque का आकार बदल सकते हैं।
उदाहरण
/ / C++ code to demonstrate the working of deque resize( ) function #include <iostream.h> #include<deque.h> Using namespace std; int main ( ){ // initializing the deque Deque<int> deque = { 85, 87, 88, 89, 90 }; cout<< " Size of deque" << deque.size( )<< "\n"; // print the deque cout<< " Deque: "; for( auto x = deque.begin( ); x != deque.end( ); ++x) cout<< *x << " "; // defining the resize( ) function deque.resize(7); // printing deque after resize cout<< "Deque after resize" << deque.size( ) <<"\n"; cout<< " New Deque:"; for( x = deque.begin( ) ; x != deque.end( ); ++x) cout<< " " <<*x; return 0; }आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा
Input - Size of deque: 5 Deque: 85 87 88 89 90 Output - Deque after resize: 7 New Deque: 85 87 88 89 90 0 0
उदाहरण
/ / C++ code to demonstrate the working of deque resize( ) function #include <iostream.h> #include<deque.h> Using namespace std; int main( ){ / / initializing deque deque<int> deque ={ 14, 15, 16, 17, 18, 19, 20 }; cout<< " Size of deque" << deque.size( )<< "\n"; / / print the deque cout<< " Deque: "; for( auto x = deque.begin( ); x != deque.end( ); ++x) cout<< *x << " "; / / defining the resize( ) function deque.resize(5); / / printing deque after resize cout<< "Deque after resize" << deque.size( ) <<"\n"; cout<< " New Deque:"; for( x = deque.begin( ) ; x != deque.end( ); ++x) cout<< " " <<*x; return 0; }आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा
Input: Size of deque: 7 Deque:14 15 16 17 18 19 20 Output: Deque after size: 5 New Deque: 14 15 16 17 18