// filename: llist.cpp #include using namespace std; #include "node.h" #include "llist.h" LinkedList::LinkedList() { header = NULL; elementCount = 0; } bool LinkedList::isEmpty() const { return (header == NULL); // or (elementCount == 0) } // size() -- returns the number of elements stored in this list. int LinkedList::size() const { return elementCount; } Node* LinkedList::find(int x) const { Node* ptr; for (ptr = header; ptr != NULL; ptr = ptr->next) { if (ptr->element == x) return ptr; // early exit } return ptr; } void LinkedList::insertFront(int x) { // One line would do, because even if the list is empty // (i.e., header == NULL), that value is good for the // value for the new node. header = new Node(x, header); elementCount++; } void LinkedList::insertAfter(int x, Node* p) { if (p != NULL) { p->next = new Node(x, p->next); elementCount++; } } void LinkedList::removeAfter(Node* p) { if (p != NULL) { Node* temp = p->next; if (temp == NULL) { cout << "ERROR: no node after p\n"; return; } else { p->next = temp->next; delete temp; elementCount--; } } }