Insert Table Chart Text Shape Media Comment Description: For this assignment, yo
ID: 3587092 • Letter: I
Question
Insert Table Chart Text Shape Media Comment Description: For this assignment, you are going to write an ordered linked list to implement a grocery list. You will ask the user for information and modify the list as the user indicates. All of the information will be written to a file. Just as in the first program, you are to check to see if the file exists. If it does, open the file and populate the list. When the program terminates, print the information to a file. Call the file grocerylist.txt.The first entry in the list is to be the number in the list. You will modify the Node class that you wrote in the last program. You will create a structure that contains the main data and the Node class will contain a variable of that structure and the link. You will write an ordered link list class that will insert, delete, traverse, and retrieve items from the list. The list will be ordered on the UPC code. You will not enter duplicate items. If the item already exists, then you need to indicate that to the user and not enter it in the list. Structure data: UPC code-number of the item ltem description - description of item that must be able to include spaces Quantity-number of item to purchase Cost-cost per item Aisle -number of aisle at store Node class data: Structure - will be a declaration of the structure defined above - the information of the structure must be private to the class, but can be returned from the class. Link-pointer to next item in the list Node class member functions Default constructor Constructor that takes UPC, item, quantity, cost, and aisle Mutator function to set next pointer Accessor function to return next pointer Accessor function to return UPC codeExplanation / Answer
#include <iostream>
using namespace std;
struct Sdata
{
int upcCode;
string itemDes;
int quantity;
int cost;
int aisle;
};
// Node class
class Node {
Sdata data;
Node* next;
public:
Node() {};
void SetData(Sdata aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
Sdata Data() { return data; };
Node* Next() { return next; };
};
// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Print();
void Append(Sdata data);
void Delete(Sdata data);
};
/**
* Print the contents of the list
*/
void List::Print() {
// Temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL ) {
Sdata data1= tmp->Data();
cout<<data1.upcCode<<endl;
cout<<data1.itemDes<<endl;
cout<<data1.quantity<<endl;
cout<<data1.aisle<<endl;
cout << " --> ";
cout << "NULL" << endl;
}
else {
// Parse and print the list
do {
Sdata data1= tmp->Data();
cout<<data1.upcCode<<endl;
cout<<data1.itemDes<<endl;
cout<<data1.quantity<<endl;
cout<<data1.aisle<<endl;
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
/**
* Append a node to the linked list
*/
void List::Append(Sdata data) {
// Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
// Create a temp pointer
Node *tmp = head;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
head = newNode;
}
}
/**
* Delete a node from the list
*/
void List::Delete(Sdata data) {
// Create a temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data ) break; //here we to overload"=="operator ,here we have to compare unq ele
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->SetNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
int main()
{
// New list
List list;
// Append nodes to the list
Sdata data1;
cin>>data1.upcCode;
cin>>data1.itemDes;
cin>>data1.quantity;
cin>>data1.aisle;
list.Append(data1);
list.Print();
Sdata data2;
cin>>data2.upcCode;
cin>>data2.itemDes;
cin>>data2.quantity;
cin>>data2.aisle;
list.Append(data2);
list.Print();
Sdata data3;
cin>>data3.upcCode;
cin>>data3.itemDes;
cin>>data3.quantity;
cin>>data3.aisle;
list.Append(data3);
list.Print();
Sdata data4;
cin>>data4.upcCode;
cin>>data4.itemDes;
cin>>data4.quantity;
cin>>data4.aisle;
list.Append(data4);
list.Print();
Sdata data5;
cin>>data5.upcCode;
cin>>data5.itemDes;
cin>>data5.quantity;
cin>>data5.aisle;
list.Append(data5);
list.Print();
// Delete nodes from the list
list.Delete(data4);
list.Print();
list.Delete(data3);
list.Print();
list.Delete(data2);
list.Print();
list.Delete(data5);
list.Print();
list.Delete(data1);
list.Print();
}
I hope above code will help you in solving your problem.Here for comparing structure object you have to overload "=" operator through your own function.
For any help in understanding the above code,kindly comment.
Thanks !!