/* * Program to demonstrate pointer based linked lists. */ #include #include // for NULL #include // for bad_alloc #include "C4-ListP.h" // header file using namespace std; /* * Constructor 1 */ List::List() : size(0), head(NULL) { } /* * Constructor 1 */ List::List(const List& aList) : size(aList.size) { if (aList.head == NULL) head = NULL; // original list is empty else{ // copy first node head = new ListNode; head->item = aList.head->item; // copy rest of list ListNode *newPtr = head; // new list pointer // newPtr points to last node in new list // origPtr points to nodes in original list for (ListNode *origPtr = aList.head->next; origPtr != NULL; origPtr = origPtr->next){ newPtr->next = new ListNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL; } } /* * Destructor: Deallocate all object dynamic memory */ List::~List() { while (!isEmpty()) remove(1); } /* * Return true if list is empty */ bool List::isEmpty() const { return size == 0; } /* * Return the length of the linked list */ int List::getLength() const { return size; } /* * Return the node at a specific "index" in the linked list, if present */ List::ListNode *List::find(int index) const { if ((index < 1) || (index > getLength())) return NULL; else{ // count from the beginning of the list. ListNode *cur = head; for (int skip = 1; skip < index; ++skip) cur = cur->next; return cur; } } /* * Return the data at the node at a specific "index" in the list */ void List::retrieve(int index, int& dataItem) const { if ((index < 1) || (index > getLength())){ cout << "Error: Retrieve index out of range: " << index << endl; return; } else{ // get pointer to node, then data in node ListNode *cur = find(index); dataItem = cur->item; } } /* * Insert a new node at the specified index in the list */ void List::insert(int index, const int& newItem) { int newLength = getLength() + 1; if ((index < 1) || (index > newLength)){ cout << "Error: Insert index out of range: " << index << endl; return; } else{ // try to create new node and place newItem in it try{ ListNode *newPtr = new ListNode; size = newLength; newPtr->item = newItem; // attach new node to list if (index == 1){ // insert new node at beginning of list newPtr->next = head; head = newPtr; } else{ ListNode *prev = find(index-1); // insert new node after node // to which prev points newPtr->next = prev->next; prev->next = newPtr; } } catch (bad_alloc e){ cout << "Error: memory allocation failed on insert\n"; return; } } } /* * Remove a specified node and deallocate its dynamic memory */ void List::remove(int index) { ListNode *cur; if ((index < 1) || (index > getLength())){ cout << "Error: Remove index out of range: " << index << endl; return; } else{ --size; if (index == 1){ // delete the first node from the list cur = head; // save pointer to node head = head->next; } else{ ListNode *prev = find(index - 1); // delete the node after the node to which prev points cur = prev->next; // save pointer to node prev->next = cur->next; } // return node to system cur->next = NULL; delete cur; cur = NULL; } } /* * External function to print all elements of the list */ void printList(const List &argList) { int data; cout << "List length: " << argList.getLength() << endl; cout << "List elements are: "; for(int i=0 ; i