Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SimpleList Example
#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) { return *this; } 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(); } MEM_COUNT; return 0; }
run
|
edit
|
history
|
help
0
Member inheritance
Heap: insert and deleteMin() Implementations
numeric_limits
Error
pointer to pointer
mine
vector destruction - clang
Linker error while taking the address of a constexpr variable
le regime keto aviss
Делим на Ноль