C ++程序在STL中实现双端队列
双端队列是一种队列数据结构,其中在两端(前端和后端)都执行插入和删除操作。可以在前后位置插入数据,也可以从前后位置删除数据。
算法
Begin Declare deque vector and iterator. Take the input as per choice. Call the functions within switch operation: d.size() = Returns the size of queue. d.push_back() = It is used to push elements into a deque from the back. d.push_front() = It is used to push elements into a deque from the front. d.pop_back() = It is used to pop or remove elements from a deque from the back. d.pop_front() = It is used to pop or remove elements from a deque from the front. d.front() = Returns the front elements of deque. d.back() = Returns the back elements of deque. Print the elements of deque. 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 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: 2 Enter value to be inserted at the end: 1 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: 3 Enter value to be inserted at the front: 2 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: 6 Front Element of the Deque: 2 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: 7 Back Element of the Deque: 1 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: 2 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: 8 Elements of Deque: 2 1 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: 2 Enter value to be inserted at the end: 4 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: 3 Enter value to be inserted at the front: 5 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: 8 Elements of Deque: 5 2 1 4 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: 4 Element 4 deleted 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: 5 Element 5 deleted 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: 8 Elements of Deque: 2 1 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: 9
热门推荐
10 分别发的祝福语简短
11 大外甥高考祝福语简短
12 小学毕业校长简短祝福语
13 搞工程发财祝福语简短
14 冬季祝福语男生文案简短
15 同事生子问候祝福语简短
16 退休文案寄语简短祝福语
17 送给自己的祝福语简短
18 简短祝福语毛笔字