C++ class ItemList { private: struct ListNode { int value; ListNode * next; }; L
ID: 3881650 • Letter: C
Question
C++
class ItemList
{
private:
struct ListNode
{
int value;
ListNode * next;
};
ListNode * head;
public:
ItemList();
// Post: List is the empty list.
bool IsThere(int item) const;
// Post: If item is in the list IsThere is
// True; False, otherwise.
void Insert(int item);
// Pre: item is not already in the list.
// Post: item is in the list.
void Delete(int item);
// Pre: item is in the list.
// Post: item is no longer in the list.
void Print() const;
// Post: Items on the list are printed on the screen.
int GetLength() const;
// Post: Length is equal to the number of items in the
// list.
~ItemList();
// Post: List has been destroyed.
};
Just need some help with excersie 2 and 3 please!!
Exercise 2: Write a driver program that reads values from file int.dat, inserts them into the list, print the length of the final list, and prints the items. Be sure that your driver checks for the preconditions on the member functions of class ItemList before calling them. How many items are in the list? If your answer is not 11, your driver did not adhere to the preconditions. Correct your driver and return your program.
Exercise 3: Add code to your driver to test the remaining member functions. Delete -47, 1926, and 2000 and print the list to confirm that they are gone.
Output
1492 is not in the list 776 is not in the list 860 is not in the list 23 is not in the list 1698 is not in the list 935 is not in the list 1926 is not in the list 953 is not in the list 1960 is not in the list 1492 is already in the list 1776 is already in the list 1860 is already in the list 23 is already in the list 1698 is already in the list 000 is not in the list 935 is already in the list 1926 is already in the list 953 is already in the list L960 is already in the list -4? is not in the list There are 11 items in the list. -47 23 1492 698 776 860 1926 1935 1953 1960 2000 After deletion, there are 8 itens in the list. 23 1492 698 776 1860 935 1953 960 Press any key to continue . .Explanation / Answer
#include <iostream>
#include<cstdlib>
#include <fstream>
using namespace std;
class ItemList
{
private:
struct ListNode
{
int value;
ListNode * next;
};
ListNode * head;
public:
ItemList();
// Post: List is the empty list.
bool IsThere(int item) const;
// Post: If item is in the list IsThere is
// True; False, otherwise.
void Insert(int item);
// Pre: item is not already in the list.
// Post: item is in the list.
void Delete(int item);
// Pre: item is in the list.
// Post: item is no longer in the list.
void Print() const;
// Post: Items on the list are printed on the screen.
int GetLength() const;
// Post: Length is equal to the number of items in the
// list.
~ItemList();
// Post: List has been destroyed.
};
ItemList::ItemList(){
head = NULL;
}
bool ItemList::IsThere(int item) const{
struct ListNode* current = head; // Initialize current
while (current != NULL)
{
if (current->value == item)
return true;
current = current->next;
}
return false;
}
void ItemList::Insert(int item){
// Pre: item is not already in the list.
if( !IsThere(item)){
// Create a temp pointer
struct ListNode* tmp = head;
/* allocate new node */
struct ListNode* new_node = (struct ListNode*) malloc(sizeof(struct ListNode));
new_node->value = item;
new_node->next = NULL;
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->next = new_node;
}
else {
// First node in the list
head = new_node;
}
}
// Post: item is in the list.
}
void ItemList::Delete(int item){
// Pre: item is in the list.
if( IsThere(item)){
// Create a temp pointer
struct ListNode* tmp = head;
struct ListNode* prev = NULL;
while(tmp != NULL){
if (tmp->value == item) { /* Found it. */
if (tmp == head) {
/* Fix beginning pointer. */
head = head->next;
} else {
/*
* Fix previous node's next to
* skip over the removed node.
*/
prev->next = tmp->next;
}
/* Deallocate the node. */
free(tmp);
/* Done searching. */
break;
}
prev = tmp;
tmp = tmp->next;
}
}
// Post: item is no longer in the list.
}
void ItemList::Print() const{
// Temp pointer
struct ListNode* tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->next == NULL ) {
cout << tmp->value;
cout <<endl;
}
else {
// Parse and print the list
do {
cout << tmp->value;
tmp = tmp->next;
cout <<endl;
}while ( tmp != NULL );
}
}
int ItemList::GetLength() const{
int count = 0; // Initialize count
struct ListNode* current = head; // Initialize current
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
ItemList::~ItemList(){
cout << "Destructor called" <<endl;
}
int main(){
cout << "Program starts "<< endl;
ItemList ItemListObject;
string input_file = "int.dat";
ifstream ifsInputFile(input_file.c_str());
if (ifsInputFile.is_open ()){
string strLine;
while (! ifsInputFile.eof() ){
int value ;
ifsInputFile >> value;
ItemListObject.Insert(value);
}
ifsInputFile.close();
}
ItemListObject.Print();
cout <<"The length of list is "<< ItemListObject.GetLength() <<endl;;
ItemListObject.Delete(-47);
ItemListObject.Delete(1926);
ItemListObject.Delete(2000);
cout <<" Ater Deletion The length of list is "<< ItemListObject.GetLength() <<endl ;
ItemListObject.Print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.