Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ArrayList
#include <iostream> #include <string> using namespace std; int _new_count_ = 0; #define NEW(a) (new a); _new_count_++ #define DELETE(a) (delete a); _new_count_-- #define MEM_COUNT (cout << "Outstanding memory references = " << _new_count_ << endl) template<class ItemType> class SimpleList { private: static const int DEFAULT_CAPACITY = 2; // Default capacity of the list ItemType *items; // Array of list items (ignore items[0] int itemCount; // Current count of list items int maxItems; // Maximum capacity of the list public: SimpleList(); ~SimpleList(); // destructor SimpleList(const SimpleList<ItemType>& aList); // Copy constructor SimpleList<ItemType>& operator=(const SimpleList<ItemType>& rhs); // Assignment operator bool isEmpty() const {return (itemCount==0);}; int getLength() const {return (itemCount);}; bool insert(const ItemType& newEntry); bool remove(); void clear() {itemCount = 0;}; void printList(); }; // end SimpleList template<class ItemType> SimpleList<ItemType>::SimpleList() : itemCount(0), maxItems(DEFAULT_CAPACITY) { items = NEW(ItemType[maxItems]); } // end default constructor template<class ItemType> SimpleList<ItemType>::~SimpleList() { cout << "In destructor " << endl; DELETE( [] items); } // end default constructor template<class ItemType> SimpleList<ItemType>::SimpleList(const SimpleList<ItemType>& aList) { itemCount = aList.itemCount; maxItems = aList.maxItems; items = aList.items; } template<class ItemType> SimpleList<ItemType>& SimpleList<ItemType>::operator=(const SimpleList<ItemType>& rhs) { itemCount = rhs.itemCount; maxItems = rhs.maxItems; if (items != nullptr)DELETE([] items); items = NEW(ItemType[maxItems]); for (int i = 0; i<itemCount; i++) { items[i] = rhs.items[i]; } // can also use mem copy return *this; // dereference pointer } template<class ItemType> bool SimpleList<ItemType>::insert(const ItemType& newEntry) { if(itemCount == maxItems) { maxItems *= 2; ItemType *temp = NEW(ItemType[maxItems]); for (int i=0; i<itemCount; i++) { temp[i] = items[i]; } DELETE([] items); items = temp; temp = nullptr; } items[itemCount] = newEntry; itemCount++; return true; } template<class ItemType> bool SimpleList<ItemType>::remove() { bool ableToRemove = (itemCount > 0); if (ableToRemove) { itemCount--; } return ableToRemove; } // end remove template<class ItemType> void SimpleList<ItemType>::printList() { for (int i=0; i<itemCount; i++) { cout << "List[" << i << "]=" << items[i] << "; "; } cout << endl; } // end remove int main() { { // set scope of a SimpleList<int> a; int testSize = 10; bool success; for (int i=0; i<testSize; i++) { success = a.insert(i); } a.printList(); SimpleList<int> b; b = a; b.insert(-1); b.printList(); a = a; } MEM_COUNT; return 0; }
run
|
edit
|
history
|
help
0
GraphBase
vector flavors....
Balanced Insert Heap Example
SceneGraph Interviewee Task
ljblblljkl
You can't erase a std::unordered_map::local_iterator
CS1428 SI Tuesday
10226
Apple is not convertible to itself (clang 3.8.0)
Own initialization