due March 20th The assignment will give you practice writing constructors and de
ID: 3861763 • Letter: D
Question
due March 20th
The assignment will give you practice writing constructors and destructors, overloaded operator functions, and implementing a linked list. You will also employ a technique called reference counting.
The Plan
The goal of the assignment is to track a list of various (fruit) "items". You will read and process a transaction file (partially displayed below). The transaction file contains 5 types of transactions. You are to store a count of the items in a sorted linked list.
Details
The transaction file contains slightly over 100 random transaction entries. The five transaction type entries are:
add <item> - add the item to the inventory, or increase the count for that item
remove <item> - remove the item from the inventory, or decrease the count for that item. If the item does not exist, print error message.
print inventory - print the contents of the linked list (in sorted order) as shown below
misspelled transactions (add, remove, or print may be misspelled) - print an error message, including the line number in the file
blank lines - skip over these (but count the lines)
Program Requirements
You must write your own linked list. You may not use any STL containers.
The linked list must be maintained in sorted order by the item number.
The linked list node must contain the item number and a count of the number of that item that are added to the list..
You must print out the contents of the linked list when a "print list" transaction record appears. See sample output below.
You must write at least 2 classes, a "node" class and a "linked list" class. Both classes must contain constructors and the "linked list" class must have a destructor.
You must include at least two overloaded operators as member functions.
The print function of your "linked list" class must be implemented as an overloaded insertion operator function.
Input File
This is the first 32 records of the input file.
Get input file here
Program Output
Here is some partial output from the program.
Output
This output shows the contents of the linked list after the first print list transaction (plus a few more lines).
add pear
add orange
add orange
add apple
add peach
add plum
ad plum
remove apple
add watermelon
add pear
add plum
reomve banana
remove pear
add apple
remove orange
remove plum
add watermelon
remove pear
add papaya
remove plum
add papaya
remove potato
add banana
add papaya
remove watermelon
print list
remove banana
remove watermelon
...
Explanation / Answer
#include<iostream>
using namespace std;
namespace NNode
{
class TList;
class TNode
{
private:
int _data;
TNode * _next;
public:
TNode(const int data = 0)
{
this->_data = data;
this->_next = NULL;
}
friend class TList;
};
class TList
{
private:
TNode *_head;
public:
TList(void)
{
this->_head = NULL;
}
bool Empty(void)
{
if (_head == NULL)
{
return true;
}
else
{
return false;
}
}
void AddFirst(int &data)
{
TNode * nodeptr = new TNode(data);
if (this->Empty())
{
this->_head = nodeptr;
nodeptr->_next = NULL;
}
else
{
nodeptr->_next = _head;
_head = nodeptr;
}
}
void AddLast(int &data)
{
TNode * nodeptr = new TNode(data);
if (this->Empty())
{
this->_head = nodeptr;
}
else
{
TNode *trav = this->_head;
while (trav->_next!=NULL)
{
trav = trav->_next;
}
trav->_next = nodeptr;
}
}
void AddAtPosition(int &data, int &pos)
{
int i;
if (_head == NULL || pos == 1)
AddFirst(data);
TNode*nodeptr = new TNode(data);
TNode*trav = this->_head;
for (i = 1; i < pos-1; i++)
trav = trav->_next;
nodeptr->_next = trav->_next;
trav->_next = nodeptr;
}
int Search(int &data)
{
TNode*trav = this->_head;
while (trav != NULL)
{
if (trav->_data = data)
return trav->_data;
trav = trav->_next;
}
return NULL;
}
void DeleteFirst()
{
if (_head != NULL)
{
TNode * temp = this->_head;
_head = _head->_next;
delete temp;
}
}
void DeleteAtPosition(int &pos)
{
int i;
if (pos == 1 || _head == NULL)
DeleteFirst();
else
{
TNode*trav = this->_head;
for (i = 1; i < pos-1; i++)
trav = trav->_next;
TNode*temp = trav->_next;
trav->_next = temp->_next;
delete temp;
}
}
void DisplayList()
{
TNode*trav = this->_head;
while (trav !=NULL)
{
cout << trav->_data << endl;
trav = trav->_next;
}
}
};
}
void AcceptRecord(int &data)
{
cout << "enter data : ";
cin >> data;
}
void AcceptRecordForPosition(int &data,int &pos)
{
cout << "enter data : ";
cin >> data;
cout << "enter position : ";
cin >> pos;
}
void AcceptRecordForPositionDelete(int &pos)
{
cout << "enter position : ";
cin >> pos;
}
int menu_list(void)
{
int choice;
cout << "0.Exit : " << endl;
cout << "1.Add First : " << endl;
cout << "2.Add Last : "<<endl;
cout << "3.Insert add position : "<<endl;
cout << "4.Search : "<<endl;
cout << "5.Delete First : "<<endl;
cout << "6.Delete At Position : "<<endl;
cout << "7.display : " << endl;
cout << "enter your choice : ";
cin >> choice;
return choice;
}
int main()
{
int choice, data,pos;
using namespace NNode;
TList list;
while ((choice = ::menu_list()) != 0)
{
switch (choice)
{
case 1:AcceptRecord(data);
list.AddFirst(data);
break;
case 2:AcceptRecord(data);
list.AddLast(data);
break;
case 3:AcceptRecordForPosition(data, pos);
list.AddAtPosition(data,pos);
break;
case 4:AcceptRecord(data);
list.Search(data);
break;
case 5:list.DeleteFirst();
break;
case 6:AcceptRecordForPositionDelete(pos);
list.DeleteAtPosition(data);
break;
case 7:list.DisplayList();
break;
}
}return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.