Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class ArrayEx{
  4. private:
  5. string msg;
  6. public:
  7. ArrayEx(string msg){
  8. this->msg = msg ;
  9. }
  10. string getMsg()const{
  11. return msg ;
  12. }
  13. };
  14. class Array {
  15. private:
  16. int *ar ;
  17. int count ;
  18. int countIndex ;
  19. public:
  20. Array(int size){
  21. ar = new int[size];
  22. count = size ;
  23. countIndex = -1 ;
  24. }
  25. void push_back(int val){
  26. ar[++countIndex] = val;
  27. if(countIndex + 1 > count ){
  28. throw ArrayEx("Array is Full");
  29. }
  30. }
  31. // 0 1 2 3 4
  32. void insert(int val , int index){
  33. countIndex++;
  34. for (int i = countIndex ; i>=index-1 ; --i)
  35. {
  36. ar[i+1] = ar[i];
  37. }
  38. ar[index-1] = val;
  39. if(countIndex +1 > count ){
  40. throw ArrayEx("Array is Full");
  41. }
  42. }
  43. void remove(int index){
  44. for (int i = index-1 ; i <= countIndex; ++i)
  45. {
  46. ar[i] = ar[i+1];
  47. }
  48. countIndex--;
  49. if(countIndex < -1){
  50. throw ArrayEx("Array is Empty");
  51. }
  52. }
  53. int search(int val){
  54.  
  55. for (int i = 0; i < countIndex ; ++i)
  56. {
  57. if(ar[i]==val){
  58. return i+1 ;
  59. }
  60. }
  61. return -1; //throw ArrayEx("Item Not Found");
  62. }
  63. bool empty(){
  64. return countIndex == -1 ;
  65. }
  66. void display(){
  67. for (int i = 0; i <= countIndex ; ++i)
  68. {
  69. cout << ar[i] << endl;
  70. }
  71. }
  72. };
  73. int main(){
  74. Array a(6);
  75. a.push_back(1);
  76. a.push_back(2);
  77. a.push_back(3);
  78. a.push_back(4);
  79. a.remove(1);
  80. a.display();
  81. if(a.empty()){
  82. cout << "YES" << endl ;
  83. }else{
  84. cout << "NO" << endl ;
  85. }
  86. return 0 ;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement