// linklist.cpp #include "linklist.h" // data structures: // node for a linked list class LLNode { public: int val; // the value at this node LLNode *next; // pointer to the next node }; //=========================================================================== LinkedList::LinkedList(void) { root = NULL; } //=========================================================================== LinkedList::LinkedList(const LinkedList &llist) { // the copy constructor root = NULL; LLNode *temp_ptr = llist.root; while (temp_ptr) { Insert(temp_ptr->val); temp_ptr = temp_ptr->next; } } //=========================================================================== LinkedList &LinkedList::operator = (const LinkedList &llist) { root = NULL; LLNode *temp_ptr = llist.root; while (temp_ptr) { Insert(temp_ptr->val); temp_ptr = temp_ptr->next; } return *this; } //============================================================================ LinkedList::~LinkedList(void) { if (root) { LLNode *temp_ptr = root->next, *next_temp_ptr; delete root; while (temp_ptr) { next_temp_ptr = temp_ptr->next; delete temp_ptr; temp_ptr = next_temp_ptr; } } } //=========================================================================== // returns 1 if there was no copy, 0 otherwise int LinkedList::Insert(int val) { if (!root) { root = new LLNode; root->val = val; root->next = NULL; return 1; } else { if (!Search(val)) { // only put in if it is not already in - this is ineff. LLNode temp_node; temp_node.val = root->val; temp_node.next = root->next; root->val = val; root->next = new LLNode; root->next->val = temp_node.val; root->next->next = temp_node.next; return 1; } } return 0; } //=========================================================================== int LinkedList::Search(int val) { LLNode *curr_ptr = root; while (curr_ptr) { if (curr_ptr->val == val) return 1; else curr_ptr = curr_ptr->next; } return 0; } //=========================================================================== void LinkedList::Write(ostream &s) { LLNode *curr_ptr = root; while (curr_ptr) { s<val<<" "; curr_ptr = curr_ptr->next; } } //============================================================================= ostream &operator<<(ostream &s, LinkedList &ll) { ll.Write(s); return s; } //=========================================================================== /* // this is for testing the linked list // something similar should be done for all data structures // using the Test func on the base class, we can test all descendants with // those methods #include "arrays.h" void Test(LinkedList &list) { list.Insert(10); list.Insert(5); list.Insert(3); list.Insert(5); list.Insert(7); list.Insert(26); list.Insert(13); list.Insert(26); cout< &larray) { for (int i = 0; i < larray.Size(); i++) Test(larray[i]); } #include void main(void) { ifstream inf; Array ll(200); TestArray(ll); ll[0].Insert(999); cout<