//************************************** // // link class implement file // // PROJECT NAME: LINK // // YOUR NAME: XYZ // YOUR ID: 1234 // YOUR EMAIL: xyz@cs.ucr.edu // //************************************** #include "link.h" //************************************************** // Implement functions in class "element" //************************************************** void element::setValue(element_type v) { value = v; }; element_type element::getValue() { return value; }; element::element() { //init value and next value = 0; next = NULL; }; element::element(element_type v) { element(); //init setValue(v); //set value }; element * element::getNext(){ return next ; } void element::makeNext(element *a){ next = a; //set next pointer to "a" }; // ~element(); element * element::print() { cout << getValue(); //print the value of current element return next; } void list::addElement(element_type val) { element *tmp; tmp = new element(val); // create a new element with the value equal to "val" tmp->makeNext(head); // link the "head" element as the next of the new element head = tmp; // change the "head" pointer to the new element }; element *list::popElement(){ element * tmp; tmp = head; // get the head element head = head->getNext(); // change the "head" pointer to the next element return tmp; } //************************************************** // Implement the functions in class list //************************************************** list::list() { head = NULL; }; // print out all the elements in the list void list::print(){ element *current; cout << "LIST " ; current = head; while( current != NULL) { current = current->print(); cout << " "; } cout << " END"; }