Question: Use the the code provided below Link Based Description of the problem. 3. Develop a program to maintain a list of homework assignments. When an
Use the the code provided below Link Based
Description of the problem.
3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: Add a new assignment. Remove an assignment. Provide a list of the assignments in the order they were assigned. Find the assignment(s) with the earliest due date.
4. We can represent a polynomial as an ordered list of terms, where the terms are ordered by their exponents. To add two polynomials, you traverse both lists and examine the two terms at the current iterator position. If the exponent of one is smaller than the exponent of the other, then insert this one into the result and advance that list's iterator. If the exponents are equal, then add a new term with that exponent and the sum of the coefficients, and advance both iterators. For example: 3x4 + 2x2 + 3x + 7 added to 2x3 + 4x + 5 is 3x4 + 2x3 + 2x2 + 7x + 12 add a program to read and add polynomials. You should define a class Term that contains the exponent and coefficient. This class should implement operator< by comparing the values of the exponents.
C++ Program /** ADT bag: Link-based implementation. @file LinkedBag.h Listing 4-3 */ #ifndef _LINKED_BAG #define _LINKED_BAG #include using namespace std; #include "BagInterface.h" #include "Node.h" template class LinkedBag : public BagInterface { private: Node* headPtr; // Pointer to first node int itemCount; // Current count of bag items // Returns either a pointer to the node containing a given entry // or the null pointer if the entry is not in the bag. Node* getPointerTo(const ItemType& target) const; public: LinkedBag(); LinkedBag(const LinkedBag& aBag); // Copy constructor virtual ~LinkedBag(); // Destructor should be virtual int getCurrentSize() const; bool isEmpty() const; bool add(const ItemType& newEntry); bool remove(const ItemType& anEntry); void clear(); bool contains(const ItemType& anEntry) const; int getFrequencyOf(const ItemType& anEntry) const; void deleteEntry(int index); void print() const; void addAtTail(const ItemType & anItem); vector toVector() const; }; // end LinkedBag // Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** ADT bag: Link-based implementation. @file LinkedBag.cpp */ template LinkedBag::LinkedBag() : headPtr(nullptr), itemCount(0) { } // end default constructor template LinkedBag::LinkedBag(const LinkedBag& aBag) { itemCount = aBag.itemCount; Node* origChainPtr = aBag.headPtr; // Points to nodes in original chain if (origChainPtr == nullptr) headPtr = nullptr; // Original bag is empty else { // Copy first node headPtr = new Node(); headPtr->setItem(origChainPtr->getItem()); // Copy remaining nodes Node* newChainPtr = headPtr; // Points to last node in new chain origChainPtr = origChainPtr->getNext(); // Advance original-chain pointer while (origChainPtr != nullptr) { // Get next item from original chain ItemType nextItem = origChainPtr->getItem(); // add a new node containing the next item Node* newNodePtr = new Node(nextItem); // Link new node to end of new chain newChainPtr->setNext(newNodePtr); // Advance pointer to new last node newChainPtr = newChainPtr->getNext(); // Advance original-chain pointer origChainPtr = origChainPtr->getNext(); } // end while newChainPtr->setNext(nullptr); // Flag end of chain } // end if } // end copy constructor template LinkedBag::~LinkedBag() { clear(); } // end destructor template bool LinkedBag::isEmpty() const { return itemCount == 0; } // end isEmpty template int LinkedBag::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool LinkedBag::add(const ItemType& newEntry) { // Add to beginning of chain: new node references rest of chain; // (headPtr is null if chain is empty) Node* nextNodePtr = new Node(); nextNodePtr->setItem(newEntry); nextNodePtr->setNext(headPtr); // New node points to chain // Node* nextNodePtr = new Node(newEntry, headPtr); // alternate code headPtr = nextNodePtr; // New node is now first node itemCount++; return true; } // end add template vector LinkedBag::toVector() const { vector bagContents; Node* curPtr = headPtr; int counter = 0; while ((curPtr != nullptr) && (counter < itemCount)) { bagContents.push_back(curPtr->getItem()); curPtr = curPtr->getNext(); counter++; } // end while return bagContents; } // end toVector template bool LinkedBag::remove(const ItemType& anEntry) { Node* entryNodePtr = getPointerTo(anEntry); bool canRemoveItem = !isEmpty() && (entryNodePtr != nullptr); if (canRemoveItem) { // Copy data from first node to located node entryNodePtr->setItem(headPtr->getItem()); // Delete first node Node* nodeToDeletePtr = headPtr; headPtr = headPtr->getNext(); // Return node to the system nodeToDeletePtr->setNext(nullptr); delete nodeToDeletePtr; nodeToDeletePtr = nullptr; itemCount--; } // end if return canRemoveItem; } // end remove template void LinkedBag::clear() { Node* nodeToDeletePtr = headPtr; while (headPtr != nullptr) { headPtr = headPtr->getNext(); // Return node to the system nodeToDeletePtr->setNext(nullptr); delete nodeToDeletePtr; nodeToDeletePtr = headPtr; } // end while // headPtr is nullptr; nodeToDeletePtr is nullptr itemCount = 0; } // end clear template int LinkedBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int counter = 0; Node* curPtr = headPtr; while ((curPtr != nullptr) && (counter < itemCount)) { if (anEntry == curPtr->getItem()) { frequency++; } // end if counter++; curPtr = curPtr->getNext(); } // end while return frequency; } // end getFrequencyOf template bool LinkedBag::contains(const ItemType& anEntry) const { return (getPointerTo(anEntry) != nullptr); } // end contains // private // Returns either a pointer to the node containing a given entry // or the null pointer if the entry is not in the bag. template Node* LinkedBag::getPointerTo(const ItemType& anEntry) const { bool found = false; Node* curPtr = headPtr; while (!found && (curPtr != nullptr)) { if (anEntry == curPtr->getItem()) found = true; else curPtr = curPtr->getNext(); } // end while return curPtr; } // end getPointerTo template void LinkedBag::addAtTail(const ItemType & anItem) { Node * newNodePtr = new Node(); newNodePtr->setItem(anItem); newNodePtr->setNext(nullptr); Node* curPtr = headPtr; if (headPtr == nullptr){ headPtr = newNodePtr; } else{ while (curPtr->getNext() != nullptr) { curPtr = curPtr->getNext(); } curPtr->setNext(newNodePtr); } itemCount++; } /* template void LinkedBag::deleteEntry(int index) { Node* curPtr = headPtr; if (index == 1) { headPtr = curPtr->getNext(); delete curPtr; return; } else if { for (int count = 0; count < n - 2; count++) { curPtr = curPtr->getNext(); } Node * temp1 = curPtr->getNext(); curPtr->getNext() = temp1->getNext(); delete temp1; } } */ template void LinkedBag::print() const { Node* curPtr = headPtr; while (curPtr !=nullptr) { cout << curPtr->getItem() << endl; curPtr = curPtr->getNext(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
