List::List(const List &other): first(nullptr), last(nullptr), currentSize(0), def(0) { for (ListItem *item = other.first; item != nullptr; item = item->getNext()) { appendElement(item->getContent()); } }
List::List(const List& other): first(nullptr), last(nullptr), currentSize(0) { // Copy list item-wise to avoid having two lists pointing at the same list items for (ListItem *item = other.first; item != nullptr; item = item->getNext()) { appendElement(item->getContent()); } }
int& List::getNthElement(int n) { int index = n; ListItem* p = first; // iterate over elements while (index-- > 0) { p = p->getNext(); } return p->getContent(); }
int &List::getNthElement(int n) { if (currentSize >= n + 1 && n >= 0) { ListItem* tmp = first; for (int i = 0; i < n; i++) { tmp = tmp->getNext(); } return tmp->getContent(); } else throw std::out_of_range("List index out of range"); }
int List::deleteFirst() { if (currentSize != 0) { int tmp = first->getContent(); ListItem *del = first; first = del->getNext(); delete del; currentSize--; return tmp; } else return 0; }
void List::insertElementAt(int i, int pos) { if (pos <= 0) prependElement(i); else if (pos >= getSize()) appendElement(i); else { ListItem *tmp = first; while (pos-- > 0) { tmp = tmp->getNext(); } new ListItem(tmp->getPrevious(), tmp, i); currentSize++; } }
int List::deleteAt(int pos) { if (pos <= 0) return deleteFirst(); else if (pos >= getSize()) return deleteLast(); else { ListItem *del = first; while (pos-- > 0) { del = del->getNext(); } int val = del->getContent(); delete del; currentSize--; return val; } }
void List::insertElementAt(int i, int pos) { if (pos <= 0) { prependElement(i); } else if (pos >= getSize()) { appendElement(i); } else { ListItem* p = first; // iterate over elements while (pos-- > 0) { p = p->getNext(); } new ListItem(p->getPrevious(), p, i); ++currentSize; } }
int List::deleteAt(int pos) { if (pos <= 0) { return deleteFirst(); } else if (pos >= currentSize - 1) { return deleteLast(); } else { int index = pos; ListItem* p = first; // iterate over elements while (index-- > 0) { p = p->getNext(); } int content = p->getContent(); delete p; --currentSize; return content; } }
void inheritanceClassDemo() { ListItem *item; /* Lets see how the items are created. Please use step into the * see how the next instruction creates an element. */ IntListItem intItem0(0); /* Now lets see it create a different type of item. Please use * step into the see this go to the FloatListItem constructor. */ FloatListItem floatItem0(1.0); /* Now we will start adding items to lists. We will create one * list that may contain either Integers or Floats. */ List linkedList; /* Let's start adding to the list. Items will be added to the end * of the list. */ linkedList.add(intItem0); linkedList.add(intItem1); linkedList.add(intItem2); linkedList.add(floatItem0); linkedList.add(floatItem1); linkedList.add(floatItem2); /* Display the contents of the linked list. Notice a mixture of integers * and floating numbers are inserted into the list. The "displayValue" * function for these items will be resolved at runtime since "item" is a * pointer to the base class ListItem. */ for (item = linkedList.getFirst(); item != 0; item = item->getNext()) { item->displayValue(); } /* Remove the items from the list. */ while (linkedList.getFirst() != 0) { linkedList.remove(*(linkedList.getFirst())); } /* If you stepped into during the adds, you would have seen how * that the member function "add" for List was called. Now we will * create another list that is sorted using SortedList which * derives from List, but changes the add function to sort the * list. * * NOTE: We will be using the overridded member function "compare" * in the ListItem for each of the derived classes * "IntListItem" and "FloatListItem". Since these cannot be * compared between each other we will create two classes; * one for integers and one for floats. */ SortedList intList; SortedList floatList; /* Add the first items. Please step into the next instructions to * see how the "compare" member function get called for * IntListItem and FloatListItem. Also notice how the "add" member * function get called in the SortedList Class. */ intList.add(intItem1); floatList.add(floatItem1); /* Now lets add something to the front of the list. */ intList.add(intItem0); floatList.add(floatItem0); /* Now to the end of the list. */ intList.add(intItem3); floatList.add(floatItem3); /* Now in the middle. */ intList.add(intItem2); floatList.add(floatItem2); /* Display the contents of the sorted integer linked list. */ for (item = intList.getFirst(); item != 0; item = item->getNext()) { item->displayValue(); } /* Display the contents of the sorted floating linked list. */ for (item = floatList.getFirst(); item != 0; item = item->getNext()) { item->displayValue(); } }