#include "node.h" node::node() :description("none"), quantity("none"), num(0), nextNode(NULL) {} node::node(int number,string descriptionInput, string quantityInput) :description(descriptionInput), quantity(quantityInput), num(number), nextNode(NULL) {} int node::getNumber() const { return num; } void node::setNumber(int number) { num = number; } string node::getDescription() const { return description; } void node::setDescription(string descriptionInput) { description = descriptionInput; } string node::getQuantity() const { return quantity; } void node::setQuantity(string quantityInput) { quantity = quantityInput; } node* node::getNextNode() const { return nextNode; } void node::setNextNode(node* input) { nextNode = input; } |
#include "linklist.h" linklist::linklist() :firstNode(NULL), lastNode(NULL), nodeCount(0) {} linklist::~linklist()// destructor { node* current = firstNode; while( current != 0 ) { node* temp = current->getNextNode(); delete current; current = temp; } firstNode = 0; } linklist::linklist(const linklist &originalList)// copy constructor { node * tmpPtr = new node; tmpPtr = originalList.firstNode; linklist(tmpPtr); while (tmpPtr != NULL) { node * newNode = new node; newNode = tmpPtr; tmpPtr = tmpPtr->getNextNode(); nodeCount++; } } linklist& linklist::operator=(const linklist& L)// overloading assignemnt operator { linklist* LL; node* temp = L.firstNode; while( temp != NULL ) { LL->getLast(); temp = temp -> getNextNode(); } return *this; } void linklist::push_front(node* input) { node* temp = getFirst(); input->setNextNode(temp); firstNode = input; nodeCount++; // Missing: if (!temp) lastNode = firstNode; } node* linklist::pop_front() { node* temp = getFirst(); firstNode = temp->getNextNode(); if (!firstNode) lastNode = 0; nodeCount--; return temp; } node* linklist::getFirst() { return firstNode; } node* linklist::getLast() { return lastNode; } void linklist::push_back(node* input) { node* temp = lastNode; if (temp) temp->setNextNode(input); else firstNode = input; lastNode = input; nodeCount++; } node* linklist::pop_back() { node* oldLast = lastNode; if (firstNode == lastNode) { firstNode = 0; lastNode = 0; } else { node* temp = firstNode; for (int i = 0; i < (nodeCount - 1); i++) { temp = temp->getNextNode(); } temp->setNextNode(NULL); lastNode = temp; } nodeCount--; return oldLast; } int linklist::getNodeCount() { return nodeCount; } |
#include <iostream> #include "linklist.h" using namespace std; // the printlist function // Purpose: Prints out the contents for each Node in the List // Parameter: A list object, by const reference // Returns: none void printList(linklist&); // the printFirstNode function // Purpose: Prints out the contents for the 1st Node in the List // Parameter: A list object, passed by value to test the copy constructor // Returns: none void printFirstNode(linklist); int main( ) { //=============================================== // PART ONE //=============================================== cout << "\nPart I: push_front and pop_front\n"; cout << "\n----------------------------------\n"; linklist groceries; // test push_back function groceries.push_front(new node(1, "gallon", "milk") ); groceries.push_front(new node(2, "loaves", "bread") ); groceries.push_front(new node(1, "dozen", "eggs" ) ); groceries.push_front(new node(1, "package", "bacon") ); cout << "\nThe original nodes in the List:\n"; printList(groceries); cout << "\n----------------------------------\n"; // test push_front function cout << "\nAdding to the front of the List:\n"; cout << "\n----------------------------------\n"; groceries.push_front(new node(2, "lbs", "hamburger") ); groceries.push_front(new node(1, "dozen", "hamburger buns") ); printList(groceries); cout << "\n----------------------------------\n"; // test pop-front cout << "\nRemoving the first node from the list.\n"; cout << "\n----------------------------------\n"; node* item = groceries.pop_front( ); cout << "\nPopped " << item->getDescription( ) << " from the list.\n\n"; printList(groceries); if (item != NULL) delete item; // =============================================== // PART TWO: Uncomment this block to test part two // =============================================== cout << "\n----------------------------------\n"; cout << "\nPart Two: Push_back and pop_back"; // test push_back groceries.push_back(new node(2, "cans", "orange juice") ); groceries.push_back(new node(1, "lb", "swiss cheese") ); cout << "\nAdding two nodes at the end\n"; cout << "\n----------------------------------\n"; printList(groceries); // test pop-back cout << "\n----------------------------------\n"; cout << "\nRemove last node from the list\n"; cout << "\n----------------------------------\n"; item = groceries.pop_back( ); cout << "\nPopped " << item->getDescription( ) << " from the list.\n\n"; printList(groceries); if (item != NULL) delete item; // ============================================ // end of part two // ============================================ // ================================================ // PART THREE: uncomment this block to test part three // ================================================ // create a second list to test assignment // cout << "\n\n--------------PART THREE------------------\n"; cout << "\n\n overloaded assignment operator\n"; cout << "The hardware list ...\n"; cout << "\n-------------------------------------------\n"; linklist hardware; hardware.push_back(new node(2, "lbs", "nails") ); hardware.push_back( new node(3, "gals", "white paint") ); hardware.push_back(new node(1, "piece", "plywood") ); printList(hardware); hardware = groceries; cout << "\n-------------------------------------------\n"; cout << "\nafter assignment"; cout << "\n-------------------------------------------\n"; printList(hardware); cout << "\n-------------------------------------------\n"; cout << "\nTest the copy constructor\n"; cout << "\n-------------------------------------------\n"; printFirstNode(hardware); // ============================================== // end of part 3 // ============================================== cout << "\n-------------------------------------------\n"; cout << "\nEnd of Test"; cout << "\n-------------------------------------------\n"; system("PAUSE"); return 0; } void printList( linklist& theList) { node* temp = theList.getFirst(); for (int i = 0; i < (theList.getNodeCount()); i++) { cout << temp->getQuantity() << " " <<temp->getNumber() << " " << temp->getDescription() << endl; temp = temp->getNextNode(); } } void printFirstNode(linklist theList) { node* first = theList.getFirst( ); cout << first->getQuantity( ) << ' '; cout << first->getQuantity( ) << ' '; cout << first->getDescription( ) << endl; } |