Advertisement
fuadnafiz98

DynamicArray

Apr 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. struct ArrayList{
  7. int *a = NULL;
  8. int sz = 0,cap,ini_cap;
  9. void initialize(int _cap){
  10. a = new int [_cap];
  11. cap = ini_cap = _cap;
  12. }
  13. void push_front(int n){
  14. if(sz == cap){
  15. cap += ini_cap;
  16. a = (int *)realloc(a,cap * sizeof(int));
  17. }
  18. for(int i = sz; i > 0; i--) a[i] = a[i - 1];
  19. a[0] = n;
  20. sz++;
  21. }
  22. void push_back(int n){
  23. if(sz == cap){
  24. cap += ini_cap;
  25. a = (int *)realloc(a,cap * sizeof(int));
  26. }
  27. a[sz] = n;
  28. sz++;
  29. }
  30. bool valid(int pos){
  31. if(pos > 0 && pos <= sz + 1) return true;
  32. return false;
  33. }
  34. void push_at(int position){
  35. if(valid(position)){
  36. int in;
  37. cout << "Enter Value : ";
  38. cin >> in;
  39. if(sz == cap){
  40. cap += ini_cap;
  41. a = (int *)realloc(a,cap * sizeof(int));
  42. }
  43. for(int i=sz; i >= position; i--) a[i] = a[i - 1];
  44. a[position - 1] = in;
  45. sz++;
  46. }
  47. else cout << "Entered position is not a valid one!\n";
  48. }
  49. void del_front(){
  50. if(sz){
  51. for(int i=0; i < sz - 1; i++) a[i] = a[i + 1];
  52. sz--;
  53. if(cap - ini_cap - 1 == sz){
  54. cap -= ini_cap;
  55. a = (int *)realloc(a,cap * sizeof(int));
  56. }
  57. }
  58. else cout << "Array is Empty!\n";
  59. }
  60. void del_back(){
  61. if(sz){
  62. sz--;
  63. if(cap - ini_cap - 1 == sz){
  64. cap -= ini_cap;
  65. a = (int *)realloc(a,cap * sizeof(int));
  66. }
  67. }
  68. else cout << "Array is Empty!\n";
  69. }
  70. void del_at(int position){
  71. if( position > 0 && sz >= position){
  72. for(int i = position - 1; i < sz; i++) a[i] = a[i + 1];
  73. sz--;
  74. if(cap - ini_cap - 1 == sz){
  75. cap -= ini_cap;
  76. a = (int *)realloc(a,cap * sizeof(int));
  77. }
  78. }
  79. else cout << "Entered position is not a valid one!\n";
  80. }
  81. void up_value(int index){
  82. if(index > 0 && index <= sz){
  83. int in;
  84. cout << "Enter Value : ";
  85. cin >> in;
  86. a[index - 1] = in;
  87. }
  88. else
  89. cout << "Entered position is not a valid one!\n";
  90. }
  91. void del(){
  92. delete [] a;
  93. }
  94. void print(){
  95. for(int i = 0; i<sz; i++) cout << a[i] << ' ';
  96. cout << endl;
  97. }
  98. };
  99.  
  100.  
  101. int main(){
  102. ArrayList arr;
  103. int n,in;
  104. cout << "Enter the size of the array: ";
  105. cin >> n;
  106. arr.initialize(n);
  107. cout << "Enter the elements of the array: ";
  108. for(int i=0; i<n; i++){
  109. int c;
  110. cin >> c;
  111. arr.push_back(c);
  112. }
  113. cout << "The resultant array: ";
  114. arr.print();
  115. cout << "Choose one of the option:\n1. Insert element at the first position of the array\n2. Insert element at the last position of the array\n3. Insert element at any middle position of the array\n4. Delete an element from the first position of the array\n5. Delete an element from the last position of the array\n6. Delete an element from any position of the array\n7. Update any value of any position of the array\n8. Quit \n";
  116. while(1){
  117. int input;
  118. cout << "Enter Option: ";
  119. cin >> input;
  120. if(input == 1){
  121. cout << "Enter Value: ";
  122. cin >> in;
  123. arr.push_front(in);
  124. }
  125. else if(input == 2){
  126. cout << "Enter Value: ";
  127. cin >> in;
  128. arr.push_back(in);
  129. }
  130. else if(input == 3){
  131. cout << "Enter Position: ";
  132. cin >> in;
  133. arr.push_at(in);
  134. }
  135. else if(input == 4) arr.del_front();
  136. else if(input == 5) arr.del_back();
  137. else if(input == 6){
  138. cout << "Enter Position: ";
  139. cin >> in;
  140. arr.del_at(in);
  141. }
  142. else if(input == 7){
  143. cout << "Enter Position: ";
  144. cin >> in;
  145. arr.up_value(in);
  146. }
  147. else if( input == 8) return 0;
  148. else cout << "Error !\n";
  149. arr.print();
  150. }
  151. arr.del();
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement