//linklist.cpp //implementation of linklist #include"linklist.h" #include"node.h" linklist::linklist(): pHead(0) {cout << "Making a linklist" << endl;} //After debugging, replace with {;} //***************************************************** linklist::~linklist() {//must get rid of all the links Node * pCurrent=pHead; cout << "In the linklist destructor." << endl; //Remove above line after debugging if (pCurrent == 0) { cout << "Empty list destroyed." << endl; //Remove above return; } while (pCurrent != 0) { pCurrent= pCurrent->getNext(); delete pHead; pHead = pCurrent; } cout << "The list has been destroyed." << endl; //Remove above return; } //***************************************************** void linklist::insert(int i) { Node * pCurrent=pHead; if (pCurrent == 0) //empty list { pHead = new Node(i); //Creates a //Node with the required data //and pHead pointing to it. return;} while (pCurrent->getNext() != 0) //if list not empty //move to the end of the list {pCurrent = pCurrent->getNext();} //pCurrent is now pointing to the last node //of the list Node * temp = new Node(i); //creates //a Node with temp pointing to it pCurrent->setNext(temp); //attaches new node //to the list. return; }// end insert //***************************************************** //notice friend function not member //so doesn't have linklist:: ostream& operator<<(ostream& out, linklist & li) { Node * pCurrent=li.pHead; if (pCurrent == 0) { out << "Displaying empty list." << endl; return out; } while (pCurrent != 0) { out << *pCurrent << endl; //Prints the Node //uses operator<< for the Node class. pCurrent= pCurrent->getNext(); //moves on to next node. } return out; } //***************************************************** //use -1,000,000 as an end of list marker. istream& operator>>(istream& in,linklist & li) { const int eolist=-1000000; int m; in >> m; while (m != eolist) { li.insert(m); in >> m; } return in; }