Question: This problem deals with the Array and Linked List implementations of the Unsorted List ( AUList and LLUList ). Your solution must begin with the

This problem deals with the Array and Linked List implementations of the Unsorted List (AUList and LLUList). Your solution must begin with the base code available

Create a function for the LLUList structure (it should return a LLUList with elements in reverse order).

_________________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: " <

__________________________________________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<item; if (printPtr->next!=NULL) std::cout<<", "; printPtr=printPtr->next; } 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.

private: LNode* ListStart; LNode* curPos; };

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!