#include #include using namespace std; template struct box { DataType data; box *next; box (DataType d, box *n):data(d), next (n){} //{data = d; next = n} }; template class LList { public: box *first; void removeAll () { while (first != NULL) pop(); } ~LList () { removeAll (); } LList& operator = (const LList &l) { if (&l != this) { this->removeAll(); this->copy (l); } return *this; } void copy (const LList &l) { box *otherCrr = l.first; int i =0; while (otherCrr != NULL) { this->insertAt (otherCrr->data,i); otherCrr = otherCrr->next; i++; } } LList (const LList &l) { this->first = NULL; copy (l); } LList ():first(NULL) {}//{first = NULL;} void push (DataType newData) { box *newBox = new box (newData,NULL); newBox->next = first; first = newBox; } void print () { box *crr = first; while (crr != NULL) { cout << crr->data << endl; crr = crr->next; } //current==? } void insertAt (DataType newData, int ix) { if (ix == 0) { push (newData); return; } box *crr = findElementAt (ix-1); assert (crr != NULL); box *newBox = new box (newData,crr->next); crr->next = newBox; } box *findElementAt (int ix) { box *crr = first; for (int i = 0; i < ix && crr != NULL; i++) crr = crr->next; return crr; } void pop () { assert (first != NULL); box *save = first; first = first->next; delete save; } void removeAt ( int ix) { if (ix == 0) { pop(); return; } box *crr = findElementAt (ix-1); assert (crr != NULL); assert (crr->next != NULL); box *save = crr->next; crr->next = crr->next->next; delete save; } }; void f (LList l2) { l2.pop(); l2.print(); } int main () { LList l, l1; l.push (0); l.push (1); l1.push (2); l1.push (3); l = l; l.print(); cout << "------\n"; }