// filename: LLTest.cpp -- test driver code for HW#6 #include #include using namespace std; #include "llist.h" int main() { LinkedList lst; // empty list Node *n; // test printList() cout << " (1) Initial list: "; lst.printList(); cout << endl; // test lastNode() n = lst.lastNode(); assert(n == NULL); // should pass with not assertion error // load up some values for (int i = 5; i > 0; i--) lst.insertFront(i); cout << " (2) List contains: "; lst.printList(); cout << endl; // test lastNode() again n = lst.lastNode(); cout << " (3) lastNode() => " << n->element << endl; // test insertEnd() lst.insertEnd(2); cout << " (4) insertEnd(2): "; lst.printList(); cout << endl; // test insertAt() n = lst.find(2); lst.insertAt(9, n); cout << " (5) insertAt(9,2): "; lst.printList(); cout << endl; // test insertAt() again n = lst.find(1); lst.insertAt(7, n); cout << " (6) insertAt(7,1): "; lst.printList(); cout << endl; // test insertAt() again one more time -- should be no change n = lst.find(8); lst.insertAt(0, n); cout << " (7) insertAt(0,8): "; lst.printList(); cout << endl; // test removeAt() n = lst.find(3); lst.removeAt(n); cout << " (8) removeAt(3): "; lst.printList(); cout << endl; // test removeAt() again n = lst.find(7); lst.removeAt(n); cout << " (9) removeAt(7): "; lst.printList(); cout << endl; // test removeAt() again one more time lst.removeAt(lst.lastNode()); cout << "(10) removeAt(last): "; lst.printList(); cout << endl; // test lastNode() again n = lst.lastNode(); cout << "(11) lastNode() => " << n->element << endl; // test copyConstructor LinkedList lst2(lst); cout << "(12) The copied list "; lst2.printList(); cout << endl; // This line should NOT be printed. if (lst.lastNode() == lst2.lastNode()) cout << "\nERROR!!! SHALLOW COPY!!!\n"; // test remove() lst.remove(9); cout << "(13) remove(9): "; lst.printList(); cout << endl; lst.remove(1); cout << "(14) remove(1): "; lst.printList(); cout << endl; lst.remove(5); cout << "(15) remove(5): "; lst.printList(); cout << endl; lst.remove(8); cout << "(16) remove(8): "; lst.printList(); cout << endl; cout << "(17) The list size is " << lst.size() << endl; // Make sure the copied list remains the same. cout << "(18) The copied list "; lst2.printList(); cout << endl << endl; // Test remove with duplicate elements lst2.insertAt(1, lst2.lastNode()); lst2.insertAt(1, lst2.lastNode()); cout << "(19) More 1's added: "; lst2.printList(); cout << endl; lst2.remove(1); cout << "==> After remove(1): "; lst2.printList(); cout << endl << endl; // Invoke destructor by creating an dynamic LinkedList object and deleting, several times. // The code should not break by those operations! LinkedList* lptr; cout << "(19) Deleting"; lptr = new LinkedList(); cout << " once.."; delete lptr; lptr = new LinkedList(lst2); cout << " twice.."; delete lptr; lptr = new LinkedList(lst); cout << " three times.."; delete lptr; cout << " ALL DONE!!\n\n"; return 0; } /* Output of the program (1) Initial list: [] (2) List contains: [1, 2, 3, 4, 5] (3) lastNode() => 5 (4) insertEnd(2): [1, 2, 3, 4, 5, 2] (5) insertAt(9,2): [1, 9, 2, 3, 4, 5, 2] (6) insertAt(7,1): [7, 1, 9, 2, 3, 4, 5, 2] (7) insertAt(0,8): [7, 1, 9, 2, 3, 4, 5, 2] (8) removeAt(3): [7, 1, 9, 2, 4, 5, 2] (9) removeAt(7): [1, 9, 2, 4, 5, 2] (10) removeAt(last): [1, 9, 2, 4, 5] (11) lastNode() => 5 (12) The copied list [1, 9, 2, 4, 5] (13) remove(9): [1, 2, 4, 5] (14) remove(1): [2, 4, 5] (15) remove(5): [2, 4] (16) remove(8): [2, 4] (17) The list size is 2 (18) The copied list [1, 9, 2, 4, 5] (19) More 1's added: [1, 9, 2, 4, 1, 1, 5] ==> After remove(1): [9, 2, 4, 5] (20) Deleting once.. twice.. three times.. ALL DONE!! */