You are going to maintain a list of products using linked lists. I am making several requirements to make sure you understand pointers. Ask questions early. Don't sit and spin.

Besides the program, the other requirements are:

Make a Fruit class. To concentrate on specific concepts, I am making several requirements.

You are required to have only two private fields:

char *name; // allocate memory (use new) - zero-terminated
char code[CODE_LEN]; // NOT zero-terminated (be careful!)

where: const int CODE_LEN = 4; and const int MAX_NAME_LEN = 30;

You are required to have the following public methods:

Default Constructor which initializes name to NULL.

Big 3: destructor, copy constructor, operator=

operator< : returns true if name is lexicographically (dictionary order) less than the name of the passed Fruit, false otherwise

operator== : returns true if name is the same as the name of the passed Fruit, false otherwise

operator!= : returns true if name is the not the same as the name of the passed Fruit, false otherwise (This is a one liner!)

friend operator<< : writes the name, left-justified, in a field of 30, then a space, then the PLU code. The PLU code number is not zero-terminated, so you must print it out a character at a time. -4 if not done. To print left justified to the output stream os: os << setiosflags(ios::left) << setw(MAX_NAME_LEN) << ...

friend operator>> : Reads, assuming the name and PLU code will be strings of characters and they will be separated by white-space. Assume that the name is at most 30 characters long, with no embedded spaces; however, you must allocate just the right amount of space for the name (remember the 0). So first read the name into a temporary buffer, then allocate memory! The PLU code number is not zero-terminated, so you must read it in one character at a time.

Only have these methods, no others. You must practice const correctness! Only one-liners can be defined in the class declaration.

Next, you are going to make an ordered singly linked list class. But instead of just storing objects, it will store pointers to objects! This is a little tricky the first time you try it, so be very careful to understand when you need the pointer and when you need the object!

I am REQUIRING you use the following declaration (so you CAN'T use the implementation in the book!).

Only one-liners can be defined in the class declaration, except the Node constructor.

// Your comments must indicate that InfoType must have operators:
// < == != = <<
// and that the list is maintained as an ordered list using them
// and that pointers to InfoType are stored in the list.
// When a pointer is passed to Insert, the object becomes the property
// of the list - the application must not delete it - this list does.
// REMOVE THESE COMMENTS -4 IF NOT DONE.

Don't forget the #ifndef guard You need to include iostream

#include "Fruit.h"
typedef Fruit InfoType;

class LList
{

public:
LList() { list = NULL; }
~LList(); // delete all the nodes
bool IsEmpty() const;
bool Insert ( InfoType * x_ptr );
bool Delete ( const InfoType & x );
void Display ( ostream & out_stream ) const;

private:
struct Node
{
Node ( InfoType * x, Node * p = NULL ) { infoPtr = x; next = p; }
~Node() { delete infoPtr; }
InfoType * infoPtr;
Node * next;
};

Node * list;
LList ( const LList & copyFrom ); // Don't allow!
LList & operator= ( const LList & assignFrom ); // Don't allow!
};

A few comments to help you along (this is tricky stuff!):

Insert takes a pointer to an InfoType object. It tries to insert an item into the list, in it's proper place. Recall the list is an ordered list! It returns a bool which is true if the item was inserted, false if the item was already in the list.

Note: Since the list "owns" the pointers passed to it, if it is passed an InfoType pointer and the object isn't inserted into the list, it must delete it!

Delete tries to delete an item from the list. It returns a bool which is true if the item was deleted, false if the item wasn't in the list. Remember to give back the node!

Display outputs the elements of the list, one per line. Note that you must dereference the pointers before "<<" to an ostream. It does not output any "header". Why do you think that is??

The definition of a Node is put right in the class. The user shouldn't know about it anyway! Note that it stores "InfoType *" , that is, pointers to InfoType. When writing the methods, be careful to distinguish when you need a InfoType * and when you need a InfoType (and must dereference the pointer).

I have put the copy constructor and operator= in the private section. You DON'T make bodies for them. Putting them here disallows the user from passing a LList by value or using the assignment operator! Thus we have the BIG 3, but only the destructor is ever used (and it is never called by the user - it's called automatically)! No copying allowed!

Your comment for this LList class should not say anything about Fruit or fruit. It should be written and commented generically. Remember, it is not being written specifically for any type - it is generic, working for any type which has operators: < == != = <<

Make a class called Catalog that creates and maintains a "Fruit Product Catalog" in ALPHABETICAL order. Make appropriate methods.

Have a single public method: void Run();

Optionally, it can have a public default constructor and/or destructor. There should be several private methods. Don't put everything in Run!

The maintenance of the list will be command driven. The commands you are to implement are:

I - Insert a name alphabetically into the list
D - Delete a name from the list
P - Print out the list
S - Stop - End of the commands

Below is a description of the input and output. Refer to the sample input/output for the exact wordings.

INPUT:

The first part of the input file contains the current database.
1. Line one contains the number of fruit types.
2. For each fruit type, there will be one line containing name/plu-code pairs.
The rest of the input file will include the commands:

I < name> < code>
D < name> < code>
P
S

OUTPUT:

Output the number of fruit types in the original list, followed by their names and numbers as you insert them into the list.

The following should be output for each command:

I(nsert):

Added to list: < name> < code>
or Already in list: < name> < code>

Note: If the name matches, assume the fruit type is already in the list

D(elete):

Deleted from list: < name> < code>
or Wasn't in list: < name> < code>

P(rint): Output the names and PLU code of every fruit type on the list, one per line.

S(top): Normal Termination of Program 2!

Below is sample input and output for this program.

Sample Input:

10
LEMONS_SMALL 4033
PINEAPPLE_LARGE 4430
GUAVA 4299
PLUMS_BLACK_LARGE 4040
PEARS_WHITE 4406
APPLES_SMALL 3001
MELON 3624
GRAPES_RED_SEEDED 4637
LEMONS_LARGE 4053
ORANGES 3370
P
D GUAVA 4299
I KIWIFRUIT_GOLDEN 3279
P
I MELON 3624
I TANGERINES_SMALL 3383
P
D TANGERINES_SMALL 3383
P
B
S
SHOULDN'T READ THIS - YOU HIT THE STOP IT!
IF YOU DO, FIX YOUR PROGRAM!

Sample Output corresponding to the above input:

There are 10 types of fruits initially in the list.
They will be read in and inserted in the list in order.
Added to list: LEMONS_SMALL 4033
Added to list: PINEAPPLE_LARGE 4430
Added to list: GUAVA 4299
Added to list: PLUMS_BLACK_LARGE 4040
Added to list: PEARS_WHITE 4406
Added to list: APPLES_SMALL 3001
Added to list: MELON 3624
Added to list: GRAPES_RED_SEEDED 4637
Added to list: LEMONS_LARGE 4053
Added to list: ORANGES 3370

Below are the fruits currently in the list
APPLES_SMALL 3001
GRAPES_RED_SEEDED 4637
GUAVA 4299
LEMONS_LARGE 4053
LEMONS_SMALL 4033
MELON 3624
ORANGES 3370
PEARS_WHITE 4406
PINEAPPLE_LARGE 4430
PLUMS_BLACK_LARGE 4040

Deleted from list: GUAVA 4299

Added to list: KIWIFRUIT_GOLDEN 3279

Below are the fruits currently in the list
APPLES_SMALL 3001
GRAPES_RED_SEEDED 4637
KIWIFRUIT_GOLDEN 3279
LEMONS_LARGE 4053
LEMONS_SMALL 4033
MELON 3624
ORANGES 3370
PEARS_WHITE 4406
PINEAPPLE_LARGE 4430
PLUMS_BLACK_LARGE 4040

Already in list: MELON 3624

Added to list: TANGERINES_SMALL 3383

Below are the fruits currently in the list
APPLES_SMALL 3001
GRAPES_RED_SEEDED 4637
KIWIFRUIT_GOLDEN 3279
LEMONS_LARGE 4053
LEMONS_SMALL 4033
MELON 3624
ORANGES 3370
PEARS_WHITE 4406
PINEAPPLE_LARGE 4430
PLUMS_BLACK_LARGE 4040
TANGERINES_SMALL 3383

Deleted from list: TANGERINES_SMALL 3383

Below are the fruits currently in the list
APPLES_SMALL 3001
GRAPES_RED_SEEDED 4637
KIWIFRUIT_GOLDEN 3279
LEMONS_LARGE 4053
LEMONS_SMALL 4033
MELON 3624
ORANGES 3370
PEARS_WHITE 4406
PINEAPPLE_LARGE 4430
PLUMS_BLACK_LARGE 4040

Invalid command!

Normal Termination of program 2!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.