Below are 2 files already created. Im having trouble with how to work the rest o
ID: 3629277 • Letter: B
Question
Below are 2 files already created. Im having trouble with how to work the rest of the code. Please Help! Belowe are the expectations.
You may not change the contents of GroceryList.h or GroceryListTest.cpp. Implement your code in a file named GroceryList.cpp.
Implement the constructor by setting the top pointer to null. Here, top is the head of the list.
Part 2 – empty method
Implement the empty method. It should return true when the list is empty, false otherwise.
Part 3 – Destructor
Implement the list destructor. It should delete all of the nodes in the grocery list and leave the top pointer null.
Part 4 – print
Implement the print method. The items in the list will be printed one at a time, with each item printed on a single line. If you implement the insert method correctly (see below) the items in the list will be printed in alphabetical order.
Part 5 – inList
Implement the inList method. This method returns true if the item passed to the method is in the list;
it returns false otherwise. Consider the following cases when implementing this method:
The list is empty
The list is not empty and you must check each node. You can stop if you find the item or you reach the end of the list.
Part 6 – remove
This method must remove an item from the list, if the item is in the list. Consider the following cases in your implementation:
The list is empty
The item is not in the list (the inList method will be helpful here)
The item is the first item in the list
The item is in the list, but is not the first item
Part 7 – insert
This method inserts an item in the list if it is not already in the list. The list must maintain an alphabetical ordering of the items in the list, so you must be careful about where you place the item. This is the most complicated method due to the various cases you must consider:
The list is empty
The item is already in the list (again, use the inList method)
The item should be inserted at the front of the list
The item should be inserted somewhere beyond the beginning of the list.
Consider that the item might belong at the end of the list.
Hints
When inserting, you should insert a copy of the string passed to the insert function. You can get a copy of the string using the string class’s assign method. Google "C++ string assign" for details. Do not use the string class’s copy method to do this.
To compare the alphabetical order of strings or to check for string equality, use the string class’s compare method. Google "C++ string compare" for details.
Don’t forget to use the namespace defined in GroceryList.h in your .cpp file.
Explanation / Answer
#include <iostream>
#include <string>
#include "GroceryList.h"
using namespace std;
using namespace CS151GroceryList;
// default constructor initializes an empty list
GroceryList::GroceryList()
{
top=NULL;
}
// destructor - destroys the list and returns all memory to the heap
GroceryList::~GroceryList()
{
ListNode *n = top->link;
while(n != NULL)
{
top->link = n->link;
delete n;
n=top->link;
}
}
// returns true if the list is empty; false otherwise
bool GroceryList::empty()
{
return (top->link==NULL);
}
// check to see if an item is in the list. If so, return true. If not, return false.
bool GroceryList::inList(const string& an_item)
{
string str;
str.assign(an_item);
for(ListNode *n = top->link; n != NULL; n = n->link) {
if (str.compare(n->item)==0){
return true;
}
}
return false;
}
// prints all of the items in the list. If insert is implemented correctly, the contents
// of the list will be printed in alphabetical order with no repeats.
void GroceryList::print()
{
for(ListNode *n = top->link; n != NULL; n = n->link)
std::cout << "The value is: " << n->item << ' ';
}
// if an_item is found, remove it from the list
void GroceryList::remove(const string& an_item)
{
ListNode *tmp=top;
string str;
str.assign(an_item);
for(ListNode *n = top->link; n != NULL; n = n->link) {
if (str.compare(n->item)==0){
tmp->link=n->link;
}
tmp=n;
}
}
// put a new item into the list. The item should be placed into the correct
// position in the list. If the item is already in the list, no change is
// made to the list. Note, you should put A COPY of an_item into the list
void GroceryList::insert(const string& an_item)
{
bool isAdded=false;
ListNode *tmp=new ListNode();
tmp->item.assign( an_item);
tmp->link=NULL;
if (top==NULL){
top=new ListNode();
top->link=tmp;
} else {
if (!inList(an_item)) {
ListNode *n;
ListNode *prev;
prev=top;
for(n = top->link; n != NULL; n = n->link) {
if (n->item.compare(tmp->item)==1){
prev->link=tmp;
tmp->link=n;
isAdded=true;
break;
}
prev=n;
}
if (!isAdded){
prev->link=tmp;
tmp->link=NULL;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.