Question: I NEED HELP WITH MY SYNTAX Create the function from part a. but for the LLUList structure (it should return a LLUList with elements in
I NEED HELP WITH MY SYNTAX
Create the function from part a. but for the LLUList structure (it should return a LLUList with elements in reverse order.
LLUList.cpp
// Implementation file for AUList #include #include "LLUList.h"
struct LNode { int item; LNode* next; };
LLUList::LLUList() { ListStart=NULL; }
LLUList::~LLUList() { LNode* tempPtr; while (ListStart != NULL) { tempPtr = ListStart; ListStart = ListStart->next; delete tempPtr; } }
bool LLUList::IsFull() const { LNode* testNode; try { testNode = new LNode; delete testNode; return false; } catch(std::bad_alloc exception) { return true; } }
int LLUList::GetLength() const { LNode* LenPos = ListStart; int length=0; while (LenPos != NULL) { LenPos=LenPos->next; length++; } return length; };
int LLUList::GetItem(int gitem) { //Procedure is similar to that of AUList, although the relevant data is stored different. //also, it's better for LLUList to proceed in order from the beginning int position=0; LNode* nodeIter; nodeIter=ListStart; while ((nodeIter != NULL) && (nodeIter->item !=gitem)) { nodeIter=nodeIter->next; position++; } if (nodeIter==NULL) return -1; else return position; }
void LLUList::MakeEmpty() { //Unlike the AUList implementation, we need to step through and delete EVERY node LNode* tempPtr; while (ListStart != NULL) { tempPtr = ListStart; ListStart = ListStart->next; delete tempPtr; } }
void LLUList::PutItem(int newitem) { LNode* newNode; //Get a pointer newNode = new LNode; //Create a new node. newNode->item = newitem; // Store the item in the node newNode->next = ListStart; // Store address of next node ListStart = newNode; // Redirect start of list }
void LLUList::DeleteItem(int ditem) { //A little bit tricky because we need to "scout ahead" and relink the object //before the deleted object to the one after. LNode *tmpNode, *nodeIter; if (ListStart->item == ditem) { tmpNode=ListStart; ListStart=ListStart->next; } else{ nodeIter=ListStart; while ((nodeIter->next->item !=ditem)) nodeIter=nodeIter->next; tmpNode=nodeIter->next; nodeIter->next=(nodeIter->next)->next; } delete(tmpNode); }
void LLUList::ResetList() { curPos=ListStart; }
int LLUList::GetNextItem() { int myitem; if (curPos == NULL) //The implemented option throw an errors if someone tries to access an item past the last one. throw(38); //EOS Exception myitem = curPos->item; curPos=curPos->next; return myitem; };
void LLUList::PrintList() { LNode* printPtr; printPtr=ListStart; std::cout<<"("; while (printPtr != NULL) { std::cout
________________________LLUList.h
struct LNode;
//Remember that header files should contain DECLARATIONS, but not DEFINITIONS for the associated class functions. class LLUList { public:
LLUList(); // Constructor
~LLUList(); // Destructor
void MakeEmpty(); // Returns the list to the empty state.
bool IsFull() const; //Determines whether list is full. (Precondition -- list has been initialized). Return value of "True" indicates class is full.
int GetLength() const; //Determines the number of elements in list.
int GetItem(int); //Retrieves position of list element matching input item (if present). If not present, -1 is returned.
void PutItem(int); //Adds the item to the list.
void DeleteItem(int); //Deletes the element whose key matches item's key.
void ResetList(); //Initializes iterator's current position for an iteration through the list.
void PrintList(); //Print all elements of the list in a readable format.
int GetNextItem(); //Gets the next element in the list. LLUList myReverseCopy = mycopy.Copyreverse();
private: LNode* ListStart; LNode* curPos; };
_____________________________________Main.cpp
#include #include "LLUList.h"
int main(int argc, char** argv) { LLUList TestList;
std::cout<<"Newly Created List: "; TestList.PrintList(); for (int i=0; i<10; i++) TestList.PutItem(100-i*10); std::cout<<"List after 'PutItem' calls: "; TestList.PrintList(); std::cout<<"Length after 'PutItem' calls: " < std::cout<<"IsFull after 'PutItem' calls? " < TestList.DeleteItem(50); std::cout<<"List after 'DeleteItem' call: "; TestList.PrintList(); std::cout<<"Length of List: "; std::cout< std::cout<<"IsFull after 'DeleteItem' call? " < std::cout<<"Index of value 80: "< std::cout<<"Index of value 25: "< LLUList reverse = TestList.reverse(); std::cout<< "Reverse List is: "; reverse.PrintList();
TestList.MakeEmpty(); std::cout<<"List after 'MakeEmpty': "; TestList.PrintList(); TestList.ResetList(); try { TestList.GetNextItem(); } catch (...) { std::cout<<"No items in list to iterate through."< } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
