Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class DynamicArray
  8. {
  9.     private:
  10.         int size; //make sure to change this as needed
  11.         T * array;
  12.     public:
  13.         DynamicArray(); //sets size to zero, array to nullptr
  14.         DynamicArray(DynamicArray const &a);
  15.         int getSize() const;
  16.         void addEntry(const T); //Adds an element at the end of the array
  17.         T getEntry(int) const; //Returns the element at that index
  18.         bool deleteEntry(T); //Deletes an element. if nothing is deleted, the function returns false
  19.         ~DynamicArray();
  20. };
  21.  
  22. template <typename T>
  23. DynamicArray<T>:: DynamicArray()
  24. {
  25.     size = 0;
  26.     array = new T[size];
  27. }
  28.  
  29. template <typename T>
  30. DynamicArray<T>:: DynamicArray (const DynamicArray<T> &a) //copy constructor
  31. {
  32.     size = a.getSize();
  33.     T *DynamicArray = new T[size];
  34.  
  35.     for (int i = 0; i < size; i++) //Copy over the elements
  36.     {
  37.         array[i] = a.getEntry(i);
  38.     }
  39. }
  40.  
  41. template <typename T>
  42. DynamicArray<T>:: ~DynamicArray() //Deletes the array and sets it's pointer to nullptr when the object is deleted
  43. {
  44.     delete [] array;
  45. }
  46.  
  47. template <typename T>
  48. int DynamicArray<T>:: getSize() const
  49. {
  50.     return size;
  51. }
  52.  
  53. template <typename T>
  54. void DynamicArray<T>:: addEntry(T const var)
  55. {
  56.     T *newArray = new T[size+1];
  57.     //Copy contents from old array into the new array
  58.     for (int i = 0; i < size; i++)
  59.     {
  60.         newArray[i] = array[i];
  61.     }
  62.  
  63.     newArray[size] = var; //initiallize the last element with the argument
  64.     size++; //resizing
  65.     delete[] array; //delete/clear the old array
  66.     array = newArray; //Setting to the new array
  67.     delete [] newArray; //Free memory
  68. }
  69.  
  70. template <typename T>
  71. T DynamicArray<T>:: getEntry(int index) const
  72. {
  73.     if (index >= size || index < 0) //Check if index in argument is also an existing index in the array
  74.     {
  75.         return 0;
  76.     }
  77.     else
  78.     {
  79.         return array[index];
  80.     }
  81. }
  82.  
  83. template <typename T>
  84. bool DynamicArray<T>:: deleteEntry(T var)
  85. {
  86.     for (int i = 0; i < size; ++i)
  87.     {
  88.         if (array[i] ==var)
  89.         {
  90.             //replace the var, and move everything down
  91.             for (int j = i; j < size-1; ++j)
  92.             {
  93.                 array[j] = array[(j+1)];
  94.             }
  95.  
  96.             T *newArray = new T[size-1];
  97.  
  98.             //Copy over the old array into the new array
  99.             //We do this because we want an array that doesn't have an "empty" index at the end
  100.             for(int j = 0; j < size-1; ++j)
  101.             {
  102.                     newArray[j] = array[j];
  103.             }
  104.  
  105.             array = newArray;
  106.  
  107.             delete [] newArray;
  108.  
  109.             newArray = nullptr;
  110.  
  111.             //decrease size since an element was taken out
  112.             size--;
  113.             return true;
  114.         }
  115.     }
  116.     return false;
  117. }
  118.  
  119. int main()
  120. {
  121.     DynamicArray<string> names;
  122.     // List of names
  123.     names.addEntry("Frank");
  124.     names.addEntry("Wiggum");
  125.     names.addEntry("Nahasapeemapetilon");
  126.     names.addEntry("Quimby");
  127.     names.addEntry("Flanders");
  128.  
  129.     // Output list
  130.     cout << "List of names:" << endl;
  131.     for (int i = 0; i < names.getSize(); i++)
  132.     cout << names.getEntry(i) << endl;
  133.     cout << endl;
  134.  
  135.     // Add and remove some names
  136.     names.addEntry("Spuckler");
  137.     cout << "After adding a name:" << endl;
  138.     for (int i = 0; i < names.getSize(); i++)
  139.     cout << names.getEntry(i) << endl;
  140.     cout << endl;
  141.  
  142.     names.deleteEntry("Nahasapeemapetilon");
  143.     cout << "After removing a name:" << endl;
  144.     for (int i = 0; i < names.getSize(); i++)
  145.     cout << names.getEntry(i) << endl;
  146.     cout << endl;
  147.  
  148.     names.deleteEntry("Skinner");
  149.     cout << "After removing a name that isn't on the list:" << endl;
  150.     for (int i = 0; i < names.getSize(); i++)
  151.     cout << names.getEntry(i) << endl;
  152.     cout << endl;
  153.  
  154.     names.addEntry("Muntz");
  155.     cout << "After adding another name:" << endl;
  156.     for (int i = 0; i < names.getSize(); i++)
  157.     cout << names.getEntry(i) << endl;
  158.     cout << endl;
  159.  
  160.     // Remove all of the names by repeatedly deleting the last one
  161.     while (names.getSize() > 0) {
  162.     names.deleteEntry(names.getEntry(names.getSize() - 1));
  163.     }
  164.  
  165.     cout << "After removing all of the names:" << endl;
  166.     for (int i = 0; i < names.getSize(); i++)
  167.     cout << names.getEntry(i) << endl;
  168.     cout << endl;
  169.  
  170.     names.addEntry("Olivia");
  171.     cout << "After adding a name:" << endl;
  172.     for (int i = 0; i < names.getSize(); i++)
  173.     cout << names.getEntry(i) << endl;
  174.     cout << endl;
  175.  
  176.     cout << "Testing copy constructor" << endl;
  177.     DynamicArray<string> names2(names);
  178.     // Remove Olivia from names
  179.     names.deleteEntry("Olivia");
  180.     cout << "Copied names:" << endl;
  181.     for (int i = 0; i < names2.getSize(); i++)
  182.     cout << names2.getEntry(i) << endl;
  183.     cout << endl;
  184.  
  185.     cout << "Testing assignment" << endl;
  186.     DynamicArray<string> names3 = names2;
  187.     // Remove Olivia from names2
  188.     names2.deleteEntry("Olivia");
  189.     cout << "Copied names:" << endl;
  190.     for (int i = 0; i < names3.getSize(); i++)
  191.     cout << names3.getEntry(i) << endl;
  192.     cout << endl;
  193.  
  194.     cout << "Testing dynamic array of ints" << endl;
  195.     DynamicArray<int> nums;
  196.     nums.addEntry(10);
  197.     nums.addEntry(20);
  198.     nums.addEntry(30);
  199.     for (int i = 0; i < nums.getSize(); i++)
  200.     cout << nums.getEntry(i) << endl;
  201.     cout << endl;
  202.  
  203.     cout << "Enter a character to exit." << endl;
  204.     char wait;
  205.     cin >> wait;
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement