I have a project due for my Datastructures class tomorrow. I\'m supposed to use
ID: 3561172 • Letter: I
Question
I have a project due for my Datastructures class tomorrow. I'm supposed to use C++ to do the following program. Please help and explain/comment as much as possible as to what each step means. Please be as detailed as possible. Thanks!
In this project, you will implement an unsorted list based on linked structure. The program consists of one class, UnsortedType. (If you feel you need more classes, you can create your own class.) The logical description of the class is as follows:
1. UnsortedType
(a) Struct NodeType includes two members, int data and NodeType* next. "data" is a number (integer type) that we want to insert into the list and "next" is an pointer that points to the next Node in the list.
(b) Operations
(i) void PutItem(int item)
*This function puts new data (item) into the list.
*The new item should be inserted at the end of the list.
*If the requested data is already in the list, your program should report an error message. This doesn't mean that your program halts; rather it should continue until all inputted operations are executed.
(ii) boolean CheckItem(int item)
*This function checks if the requested data (item) is in the list.
(iii) void DeleteItem(int item)
*This function deletes the requested data (item) from the list.
*If it doesn't exist in the list, your program should report an error message. This doesn't mean that your program halts; rather it should continue until all inputted operations are executed.
(iv) void MakeEmpty()
*Make the entire list empty. (all items have to be deallocated.)
(v) void PrintList() const
*This function is used to print all of the contents of the list.
To run your program, there will be an input file that contains the sequence of operations. When your program gets started, it should ask its user the input file name so that your program reads the contents of that input file. The following is input-output examples.
inputfile.txt outputfile.txt
PutItem 24 PutItem 24
24
PutItem 31 PutItem 31
24 31
PutItem 1 PutItem 1
24 31 1
PutItem 3 PutItem 3
24 31 3
PutItem 5 PutItem 5
24 31 1 3 5
PutItem 13 PutItem 13
24 31 1 3 5 13
PutItem 18 PutItem 18
24 31 1 3 5 13 18
DeleteItem 17 DeleteItem 17
Requested data "17" is not in the list.
24 31 1 3 5 13 18
DeleteItem 31 DeleteItem 31
24 1 3 5 13 18
PutItem 24 PutItem 24
Requested data "24" is already in the list.
24 1 3 5 13 18
Explanation / Answer
input.txt:
PutItem 24
PutItem 31
PutItem 1
PutItem 3
PutItem 5
PutItem 13
PutItem 18
DeleteItem 17
DeleteItem 31
PutItem 24
// list.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class UnsortedType
{
public:
UnsortedType();
~UnsortedType();
void PutItem(int item);
bool CheckItem(int item);
void DeleteItem(int item);
void MakeEmpty();
void PrintList();
private:
struct NodeType
{
int data;
NodeType* next;
};
NodeType* head;
NodeType* tail;
};
UnsortedType::UnsortedType()
{
head = NULL;
tail = NULL;
}
UnsortedType::~UnsortedType()
{
MakeEmpty();
}
void UnsortedType::PutItem(int item)
{
if (CheckItem(item))
{
cout << "Requested data "" << item << "" is already in the list."
<< endl;
return;
}
NodeType* p = new NodeType();
p->next = NULL;
p->data = item;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
bool UnsortedType::CheckItem(int item)
{
NodeType* p = head;
while (p != NULL)
{
if (p->data == item)
return true;
p = p->next;
}
return false;
}
void UnsortedType::DeleteItem(int item)
{
NodeType* prev = NULL;
NodeType* p = head;
while (p != NULL && p->data != item)
{
prev = p;
p = p->next;
}
if (p == NULL)
{
cout << "Requested data "" << item << "" is not in the list."
<< endl;
return;
}
if (prev == NULL)
head = p->next;
else
prev->next = p->next;
if (p == tail)
tail = prev;
delete p;
}
void UnsortedType::MakeEmpty()
{
while (head != NULL)
{
NodeType* p = head;
head = head->next;
delete p;
}
tail = NULL;
}
void UnsortedType::PrintList()
{
NodeType* p = head;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
char filename[100];
string method;
int item;
cout << "Enter input file name: ";
cin.getline(filename, sizeof(filename));
ifstream file(filename);
UnsortedType theList;
if (!file.is_open())
{
cout << "Failed to open file." << endl;
return 0;
}
while (file >> method)
{
if (method == "PutItem")
{
file >> item;
cout << method << " " << item << endl;
theList.PutItem(item);
}
else if (method == "DeleteItem")
{
file >> item;
cout << method << " " << item << endl;
theList.DeleteItem(item);
}
else if (method == "MakeEmpty")
{
cout << method << endl;
theList.MakeEmpty();
}
theList.PrintList();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.