#include "MyLinkedBag.h" //#include //using namespace std; // Constructor MyLinkedBag::MyLinkedBag() : head(NULL) { } // Destructor MyLinkedBag::~MyLinkedBag() { while( head!=NULL ) { BagNode *hold = head->next; delete head; head = hold; } } // Mutator methods: void MyLinkedBag::add(ListType x) // adds at front { BagNode *newPtr = new BagNode; newPtr->data = x; newPtr->next = head; head = newPtr; } bool MyLinkedBag::remove(ListType x) { if( head==NULL ) return false; if( head->data==x ) { BagNode *hold = head; delete head; head = hold; return true; } // so list not empty and not first node to be deleted BagNode *cursor = head; while( cursor->next != NULL && cursor->next->data != x ) cursor = cursor->next; if(cursor->next == NULL) return false; else { BagNode *hold = cursor->next->next; delete ( cursor->next ); cursor->next = hold; return true; } } // Accessor methods: bool MyLinkedBag::isEmpty() const // Returns: whether the list is empty. { return (head==NULL); } ostream & operator<< ( ostream & out, const MyLinkedBag & other) { for( BagNode *cursor = other.head; cursor; cursor=cursor->next) out << cursor->data << " "; out << endl; }