« Double Ended Queue

1class Deque {
2 int* deq;
3 int front;
4 int rear;
5 int si;
6
7 public:
8
9 Deque(int size){
10 deq=new int[size];
11 si=size;
12 front=-1;
13 rear=-1;
14 }
15
16 void insertFront(int ele){
17
18 if(front==-1 && rear==-1)
19 {
20 front=0;
21 rear=0;
22 deq[front]=ele;
23 return;
24 }
25 if((front+1)%si==rear){
26 cout<<(-1)<<endl;
27 return;
28 }
29 front=(front+1)%si;
30 deq[front]=ele;
31
32 }
33
34 void insertRear(int ele){
35 if(front==-1 && rear==-1)
36 {
37 front=0;
38 rear=0;
39 deq[rear]=ele;
40 return;
41 }
42 if(front==rear-1 || rear==0 && front==si-1){
43 cout<<(-1)<<endl;
44 return;
45 }
46 if(rear==0)
47 rear=si-1;
48 else
49 rear--;
50 deq[rear]=ele;
51
52 }
53 void deleteFront(){
54 if(front==-1 && rear==-1){
55 cout<<(-1)<<endl;
56 return;
57 }
58 if(rear==front){
59 rear=-1;
60 front=-1;
61 return;
62 }
63 if(front==0)
64 front=si-1;
65 else
66 front--;
67 }
68
69 void deleteRear(){
70 if(front==-1 && rear==-1){
71 cout<<(-1)<<endl;
72 return;
73 }
74 if(rear==front){
75 rear=-1;
76 front=-1;
77 return;
78 }
79 rear=(rear+1)%si;
80 }
81
82 int getFront(){
83 if(front==-1 && rear==-1){
84 return -1;
85 }
86 return deq[front];
87 }
88 int getRear(){
89 if(front==-1 && rear==-1){
90 return -1;
91 }
92 return deq[rear];
93 }
94
95};