fork download
  1. #include<stdio.h>
  2. #include<memory.h>
  3. #include<iostream>
  4. using namespace std;
  5. template<typename T>
  6. class MyDequeue{
  7. private:
  8. T *arr = new T[Capacity];
  9. size_t ArrSize;
  10. size_t Capacity;
  11. int frontptr;
  12. int rearptr;
  13.  
  14. void resize_arr(size_t new_capacity){
  15. T *arr1 = new T[new_capacity];
  16. for(int i = 0; i<ArrSize; i++){
  17. arr1[i] = arr[(frontptr + i)%Capacity];
  18. }
  19. delete [] arr;
  20. arr = arr1;
  21. frontptr = 0;
  22. rearptr = ArrSize;
  23. Capacity = new_capacity;
  24. }
  25.  
  26. public:
  27. MyDequeue(): ArrSize(0),frontptr(0), rearptr(0), Capacity(0), arr(nullptr){}
  28.  
  29. T& operator[](int index){
  30. if(ArrSize == 0 || Capacity == 0){
  31. perror("Index out of bounds");
  32. static T dum;
  33. return dum;
  34. }
  35. if(index > ArrSize-1 || index < -ArrSize + 1){
  36. perror("Index out of bounds");
  37. static T dum;
  38. return dum;
  39. }
  40.  
  41. if(index < 0){
  42. index += ArrSize;
  43. }
  44. return arr[(frontptr + index)% Capacity];
  45. }
  46.  
  47.  
  48. MyDequeue(size_t n): frontptr(0), rearptr(n-1), Capacity(n), ArrSize(n) {
  49. arr =new T[Capacity];
  50. for(int i = 0; i<ArrSize; i++){
  51. arr[i] = T();
  52. }
  53. }
  54.  
  55. MyDequeue(size_t n, T x): frontptr(0), rearptr(n-1), Capacity(n), ArrSize(n){
  56. arr = new T[Capacity];
  57. for(int i= 0; i<ArrSize; i++){
  58. arr[i] = x;
  59. }
  60. }
  61.  
  62.  
  63.  
  64. void push_front(T x){
  65. if(ArrSize == Capacity){
  66. resize_arr(Capacity == 0? 1: 2*Capacity);
  67. }
  68. frontptr = (frontptr - 1 + Capacity)%Capacity;
  69. arr[frontptr] = x;
  70. ArrSize++;
  71. }
  72.  
  73. void push_back(T x){
  74. if(ArrSize == Capacity){
  75. resize_arr(Capacity==0?1:2*Capacity);
  76. }
  77. arr[rearptr] = x;
  78. rearptr = (rearptr + 1)%Capacity;
  79. ArrSize++;
  80. }
  81.  
  82. void pop_front(){
  83. if(ArrSize == 0){
  84. perror("Underflow");
  85. return;
  86. }
  87. frontptr = (frontptr + 1)%Capacity;
  88. ArrSize--;
  89. }
  90.  
  91. void pop_back(){
  92. if(ArrSize == 0){
  93. perror("Underflow");
  94. return;
  95. }
  96. rearptr = (rearptr - 1 + Capacity)%Capacity;
  97. ArrSize--;
  98. }
  99.  
  100. T front(){
  101. if(ArrSize == 0){
  102. return T();
  103. }
  104. return arr[frontptr];
  105. }
  106.  
  107. T back(){
  108. if(ArrSize == 0){
  109. return T();
  110. }
  111. return arr[rearptr];
  112. }
  113.  
  114. bool empty(){
  115. return ArrSize == 0;
  116. }
  117.  
  118. int size(){
  119. return ArrSize;
  120. }
  121.  
  122. void resize(size_t n){
  123. if(Capacity < n){
  124. resize_arr(n);
  125. }
  126. if(ArrSize > n){
  127. ArrSize = n;
  128. }else{
  129. for(size_t i = ArrSize; i<n; i++)
  130. push_back(T());
  131. }
  132.  
  133. // rearptr = (rearptr + n%ArrSize)%ArrSize;
  134. // // int i = 0;
  135. // while(i<ArrSize){
  136. // arr1[i]= arr[i];
  137. // i++;
  138. // }
  139. // delete [] arr;
  140. // arr = arr1;
  141. }
  142.  
  143. void clear(){
  144. delete [] arr;
  145. ArrSize = 0;
  146. frontptr = 0;
  147. rearptr = 0;
  148. Capacity = 0;
  149. }
  150.  
  151. int capacity(){
  152. return Capacity;
  153. }
  154.  
  155.  
  156. };
  157. int main(){
  158. MyDequeue<int> a;
  159. a.push_front(30);
  160. // cout<<a.size()<<" ";
  161. // a.pop_front();
  162. // cout<<a.size();
  163. // // for(int i = 0; i<a.size(); i++){
  164. // cout<<a[i]<<"\n";
  165. // }
  166. //cout<<a[0]<<" ";
  167. // cout<<a.size()<<" ";
  168. // cout<<a[0]<<" ";
  169. // a.pop_back();
  170. // cout<<a[0];
  171. for(int i= 0; i<20; i++){
  172. a.push_back(i);
  173. }
  174.  
  175. for(int i= 0; i<a.size(); i++){
  176. cout<<a[i]<<" ";
  177. }
  178. cout<<a.front();
  179. return 0;
  180. }
Success #stdin #stdout #stderr 0.01s 5284KB
stdin
Standard input is empty
stdout
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30
stderr
Index out of bounds: Success
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument
Index out of bounds: Invalid argument