Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Does this code compile and meet the requirements? USLList.h file #pragma once #include Node.h #include ItemType.h class USLList { public: USLList(); ~USLList(); bool isEmpty() const;

Does this code compile and meet the requirements? USLList.h file #pragma once #include "Node.h" #include "ItemType.h" class USLList { public: USLList(); ~USLList(); bool isEmpty() const; int getLength() const; void insertItem(ItemType &item); void deleteItem(ItemType &item); bool searchItem(ItemType &item) const; void printList() const; void reverseList(); private: Node *head; int length; }; USLList.cpp file #include "USLList.h" #include USLList::USLList() { head = nullptr; length = 0; } USLList::~USLList() { Node *curr = head; while (curr != nullptr) { Node *temp = curr; curr = curr->getNext(); delete temp; } } bool USLList::isEmpty() const { return head == nullptr; } int USLList::getLength() const { return length; } void USLList::insertItem(ItemType &item) { Node *newNode = new Node(); newNode->setItem(item); newNode->setNext(head); head = newNode; length++; } void USLList::deleteItem(ItemType &item) { Node *curr = head; Node *prev = nullptr; while (curr != nullptr && !(curr->getItem().compareTo(item))) { prev = curr; curr = curr->getNext(); } if (curr == nullptr) { std::cout << "Item not found in the list." << std::endl; return; } if (prev == nullptr) { head = curr->getNext(); } else { prev->setNext(curr->getNext()); } delete curr; length--; } bool USLList::searchItem(ItemType &item) const { Node *curr = head; while (curr != nullptr && !(curr->getItem().compareTo(item))) { curr = curr->getNext(); } return curr != nullptr; } void USLList::printList() const { Node *curr = head; while (curr != nullptr) { std::cout << curr->getItem().getData() << " "; curr = curr->getNext(); } std::cout << std::endl; } void USLList::reverseList() { Node *prev = nullptr; Node *curr = head; Node *next = nullptr; while (curr != nullptr) { next = curr->getNext(); curr->setNext(prev); prev = curr; curr = next; } head = prev; }; Node.h file #pragma once #include "ItemType.h" class Node { public: Node(); ~Node(); void setItem(ItemType &item); ItemType getItem() const; void setNext(Node *node); Node* getNext() const; private: ItemType item; Node *next; }; Node.cpp file #include "Node.h" Node::Node() { next = nullptr; } Node::~Node() { } void Node::setItem(ItemType &item) { this->item = item; } ItemType Node::getItem() const { return item; } void Node::setNext(Node *node) { next = node; } Node* Node::getNext() const { return next; }; ItemType.h file #pragma once class ItemType { public: ItemType(); ItemType(int data); ~ItemType(); void setData(int data); }; ItemType.cpp File #include "ItemType.h" ItemType::ItemType() { value = 0; } ItemType::ItemType(int value) { this->value = value; } int ItemType::getValue() const { return value; } void ItemType::setValue(int value) { this->value = value; } bool ItemType::operator==(const ItemType& other) const { return value == other.value; }; CODE REQUIREMENTS You will write an unsorted Linked List class (LLC) with the general data and methods we have discussed in class and shown in your book (note that the requirements here do not exactly match what is in your book). The list that your LLC will maintain is a list of Nodes which, in turn, will contain the item data we are maintaining. The real data is integer values, but it will be wrapped in an ItemType class that will hold the data and have the ability to provide comparisons between its own data and other data provided along with setting and returning its data. 1. The LLC will be named USLList and will contain a linked list of Nodes and sevral standard public functions appropriate to operate a linked list. 2. The Node class will be named Node and contain member data ItemType and a next pointer to another Node. It will provide getters and setters for the member items. 3. The ItemType class will hold the single integer member data. It will provide methods to set the member data, retrieve the member data, and to compare other data to its own. You will be delivering 3 classes named USLList, Node and ItemType, with .h and .cpp files for each. Your file names will be identical to that of the classes. You will submit the following files: 1. USLList.h 2. USLList.cpp 3. Node.h 4. Node.cpp 5. ItemType.h 6. ItemType.cpp Testing code You will write your own test driver, but not submit it to me (nothing you submit to me will have a main() function). I will have my own. A small sample of mine is show. Output is shown below (the output is created by the PutItem(), PrintList() and MakeEmpty(), DeleteItem() and GetLength classes) Sample output (from more test code than shown above) Grade Priority Order: 1. Compiles: for example: Has ALL the functions specified, correctly spelled with the correct interfaces (return types, argument types and order). Has no syntax errors or under/over specified functions. Has no #include of cpp files, headers have #pragma once at the top. 2. Operates correctly with the neat cases (no tester malfeasance) 3. Handles nasty input and tough cases 4. Style guidelines (see end of this document) followed and general instructions followed, file names, header comments and.. 5. This falls in the 5% heading for following instructions: Outputs to the screen in a neat fashion (as suggested in the light blue sections of Specific requirements function descriptions). This will serve as a help to you for development (why do you think I came up with it??). USLList* lList = new USLList(); ItemType* item1 = new ItemType(1); // several of these lList->PutItem(item1); lList->PrintList(); // lList->DeleteItem(item3); // Delete from several // positions on the list //and much much more... delete lList; lList = NULL; Specific Requirements 1. ItemType Class This class is named ItemType and will hold the actual data being managed, which is an integer (all this effort for an integer! ) Member data: int data Functions Default Constructor Constructor that takes in one int value and sets member variable data to it. Destructor Other public functions to provide: Function name Return type Argument(s) Description Compare() enum or string your choice integer Compares the integer argument to the integer member variable and returns the string / enum result of that comparison. GetData() integer None returns the member integer variable SetData() void integer Sets the member integer variable to the argument provided. 2. Node class This class is named Node and will hold a member variable of type ItemType and a pointer of type Node. Member data: ItemType* item // A pointer to our ItemType Node* next //A pointer to the next node (or NULL) Functions Default Constructor Constructor (ItemType newItem) // Takes ItemType argument and sets the member pointer to it. Destructor Other public functions to provide: Function name Return type Argument(s) Description SetNext() void Node* Receives a Node pointer and sets the next member variable to it. GetItem() ItemType None returns the member Itemtype Next() Node* None Returns the member pointer next. 3. USLList Class This class is named USLList and will hold and maintain a list of Nodes, instantiating, deleting, traversing and finding them as needed. Member data: Node* head Node* curPos Functions Default Constructor // only the default constructor for this class Destructor Other public functions to provide: Function name Return type Argument(s) Description PutItem() bool ItemType* Will place the new item in FRONT (at the head) of the existing list provided it doesnt already exist (suggest use of GetItem() for that). Return true if added, false if not. Screen output: If item is not already present, print added and the item data value and the new nodes address to the screen include spacing to make it neat but not lengthy. If it is already present, print short statement to that effect. See sample output in the overview. GetItem() ItemType* ItemType* bool &found Will search for the provided item and return it if found and return NULL if not found. The bool found is passed by reference and populated with the correct answer Screen output: Print Searching... then print each integer value searched for until it is found progressing across the screen. Then print found and or endl; DeleteItem() bool ItemType* Will search for the provided item and, if found, delete it, restoring the connections between the two items surrounding the deleted one. Returns bool found. Screen output: Print deleted and the item data value and the deleted items (former) address to the screen include spacing to make it neat but not lengthy. See sample output in the overview. ResetList() void None Restores the curPos pointer to the top of the list. MakeEmpty() void None Traverses the list, deleting each item, maintaining the pointers necessary to reach every item. After running, head and curPos are both NULL. Screen output: Print a message indicating when the end of the list is reached. PrintList() void None Traverses the list printing the data of interest (as produced by ItemType) for each. Include the node address for each as well. Make sure the output is neat. GetNext() Node* None Iterator returns the next item, determined by the curPos next pointer. Increments the curPos pointer. GetLength() int None Returns the length of the list. This can be done by traversing the list each time this function is called or by keeping a (private) length member variable your choice. This is internal and should not affect my tester as long as it is correct. Style Guidelines: Type Naming Example definition Local Variables lowerCamelCase int myVar1 = 0; Local functions lowerCamelCase int myFunc(string name); const ALL CAPS const float PI = 3.14f; Public variables Rare to be public, see Public Functions (getters and setters Public functions UpperCamelCase int GetArea(); Style (Its about readability and quality) Matching curly braces in the same column Indent inside curly braces Whitespace use enough to help readability, but no more Use curly braces for code blocks, even if only one line/ NEVER use single letter variable names (except maaaaybe as a loop index even then avoid) Use meaningful variable names, but not too long Comments are good, but often meaningful variable names reduce the need. Use enough comments to clarify, but not clutter Clean code (clutter creates errors) Delete unused variables, includes, using statements, etc. Avoid misspelling a meaningful variable name Avoid unnecessary use of global variables MOSTLY have only one return statement in a function.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions