C++ I have the code, but need comments added please. //main// #include <iostream
ID: 666372 • Letter: C
Question
C++
I have the code, but need comments added please.
//main//
#include <iostream>
#include "unorderedListType.h"
#include "arrayListType.h"
using namespace std;
int main()
{
int xxx;
unorderedListType myList(25);
int num;
cout << "Enter 8 integers: " << endl;
for(int i = 0; i < 8; i++)
{
cin >> num;
myList.insertEnd(num);
}
unorderedListType yourList(myList);
cout << "List values: " << endl;
yourList.print();
cout << "Enter a number to be deleted: " << endl;
cin >> num;
myList.remove(num);
cout << "After removing number ** " << num << " **, the New List is: " << endl;
myList.print();
cout << "Enter a number to be found: " << endl;
cin >> num;
if(myList.seqSearch(num) != -1)
cout << "Number " << num << " is found in New List." << endl;
else
cout << "Number " << num << " is mot in List." << endl;
cin >> xxx;
return 0;
}
//arrayListType.h//
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
using namespace std;
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int,int) const;
virtual void insertAt(int,int) = 0;
virtual void insertEnd(int) = 0;
void removeAt(int);
void retrieveAt(int,int&) const;
virtual void replaceAt(int,int) = 0;
void clearList();
virtual int seqSearch(int) const = 0;
virtual void remove(int) = 0;
arrayListType(int size = 100);
arrayListType(const arrayListType & otherList);
virtual ~arrayListType();
protected:
int *list;
int length;
int maxSize;
};
bool arrayListType::isEmpty() const
{
return (length == 0);
}
bool arrayListType::isFull() const
{
return (length == maxSize);
}
int arrayListType::listSize() const
{
return length;
}
int arrayListType::maxListSize() const
{
return maxSize;
}
void arrayListType::print() const
{
for(int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
bool arrayListType::isItemAtEqual(int location, int item) const
{
if(location < 0 || location >= length)
{
cout << "The location is out of range." << "This is an error, you shouldn't be here!" << endl;
return false;
}
else
return (list[location] == item);
}
void arrayListType::removeAt(int location)
{
if(location < 0 || location >= length)
cout << "The location is out of range."
<< "This is an error, you shouldn't be here!" << endl;
else
{
for(int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}
void arrayListType::retrieveAt(int location, int &retItem) const
{
if(location < 0 || location >= length)
cout << "The location is out of range."
<< "This is an error, you shouldn't be here!" << endl;
else
retItem = list[location];
}
void arrayListType::clearList()
{
length = 0;
}
arrayListType::arrayListType(int size)
{
if(size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array with default size of 100." << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int[maxSize];
}
arrayListType::~arrayListType()
{
delete [] list;
}
arrayListType::arrayListType(const arrayListType &otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new int[maxSize];
for(int j = 0; j < length; j++)
list[j] = otherList.list[j];
}
#endif
//unorderedListType.h//
#ifndef H_unorderedListType
#define H_unorderedListType
#include <iostream>
#include "arrayListType.h"
using namespace std;
class unorderedListType: public arrayListType
{
public:
void insertAt(int, int);
void insertEnd(int);
void replaceAt(int, int);
int seqSearch(int) const;
void remove(int);
unorderedListType(int size = 100);
};
void unorderedListType::insertAt(int location, int item)
{
if(location < 0 || location >= maxSize)
cout << "The index is out of range." << endl;
else if(length >= maxSize)
cout << "The list is full." << endl;
else
{
for(int i = length; i > location; i--)
list[i] = list[i - 1];
list[location] = item;
length++;
}
}
void unorderedListType::insertEnd(int item)
{
if(length >= maxSize)
cout << "The list is full." << endl;
else
{
list[length] = item;
length++;
}
}
int unorderedListType::seqSearch(int item) const
{
int loc = 0;
bool found = false;
for(loc = 0; loc < length; loc++)
if(list[loc] == item)
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
}
void unorderedListType::remove(int item)
{
int loc;
if(length == 0)
cout << "The list is empty." << endl;
else
{
loc = seqSearch(item);
if(loc != -1)
removeAt(loc);
else
cout << "The item is not in the list." << endl;
}
}
void unorderedListType::replaceAt(int location, int item)
{
if(location < 0 || location >= length)
cout << "The location is out of range." << endl;
else
list[location] = item;
}
unorderedListType::unorderedListType(int size):arrayListType(size)
{
}
#endif
Explanation / Answer
// main
// Include the header files
#include <iostream>
#include "unorderedListType.h"
#include "arrayListType.h"
using namespace std;
// main function starts
int main()
{
// declare the variables
int xxx;
// initialize a new unorderedlist named mylist
unorderedListType myList(25);
int num;
// prompt the user for the first 8 elements of the list
cout << "Enter 8 integers: " << endl;
// input the first 8 elements of the list
for(int i = 0; i < 8; i++)
{
cin >> num;
myList.insertEnd(num);
}
// initialize a new list as a copy of the previous list
unorderedListType yourList(myList);
// print the copy of the list using the function print()
cout << "List values: " << endl;
yourList.print();
// prompt the user for a number to be deleted from the list
cout << "Enter a number to be deleted: " << endl;
// input the number to be deleted from the list
cin >> num;
// remove the number from the original list
myList.remove(num);
// print the new list using the function print()
cout << "After removing number ** " << num << " **, the New List is: " << endl;
myList.print();
// Input a number to search from the list
cout << "Enter a number to be found: " << endl;
cin >> num;
// check whether the number is found in the list by using the function seqSearch()
if(myList.seqSearch(num) != -1)
cout << "Number " << num << " is found in New List." << endl;
else
cout << "Number " << num << " is mot in List." << endl;
cin >> xxx;
return 0;
}
// unorderedListType.h
// define the header
#ifndef H_unorderedListType
#define H_unorderedListType
#include <iostream>
#include "arrayListType.h"
using namespace std;
// declare the class structure
class unorderedListType: public arrayListType
{
public:
void insertAt(int, int);
void insertEnd(int);
void replaceAt(int, int);
int seqSearch(int) const;
void remove(int);
unorderedListType(int size = 100);
};
// method to insert a new element in the list
void unorderedListType::insertAt(int location, int item)
{
// if the list is not full and index is valid, insert the element in the list
if(location < 0 || location >= maxSize)
cout << "The index is out of range." << endl;
else if(length >= maxSize)
cout << "The list is full." << endl;
else
{
for(int i = length; i > location; i--)
list[i] = list[i - 1];
list[location] = item;
length++;
}
}
// insert an element at the end
void unorderedListType::insertEnd(int item)
{
// if the list is not full, insert the element in the list at the end
if(length >= maxSize)
cout << "The list is full." << endl;
else
{
list[length] = item;
length++;
}
}
// search for an element in the list
int unorderedListType::seqSearch(int item) const
{
int loc = 0;
bool found = false;
// iterate through each of the list element of the list
for(loc = 0; loc < length; loc++)
if(list[loc] == item)
{
// if element found, exit searching
found = true;
break;
}
// if found, return the index, else return -1
if(found)
return loc;
else
return -1;
}
// removes an element from the list
void unorderedListType::remove(int item)
{
int loc;
// if list is empty, print the message
if(length == 0)
cout << "The list is empty." << endl;
// do sequential search on the list to search for the element to delete
else
{
loc = seqSearch(item);
if(loc != -1)
removeAt(loc);
else
cout << "The item is not in the list." << endl;
}
}
// replace an element with a new one
void unorderedListType::replaceAt(int location, int item)
{
if(location < 0 || location >= length)
cout << "The location is out of range." << endl;
else
list[location] = item;
}
// constructor
unorderedListType::unorderedListType(int size):arrayListType(size)
{
}
#endif
// arrayListType.h
// define the header
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
using namespace std;
// declare a class named arrayListType
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int,int) const;
virtual void insertAt(int,int) = 0;
virtual void insertEnd(int) = 0;
void removeAt(int);
void retrieveAt(int,int&) const;
virtual void replaceAt(int,int) = 0;
void clearList();
virtual int seqSearch(int) const = 0;
virtual void remove(int) = 0;
arrayListType(int size = 100);
arrayListType(const arrayListType & otherList);
virtual ~arrayListType();
protected:
int *list;
int length;
int maxSize;
};
// method to check whether the list is empty or not
bool arrayListType::isEmpty() const
{
return (length == 0);
}
// check whether list is full
bool arrayListType::isFull() const
{
return (length == maxSize);
}
// method to return the current size of the list
int arrayListType::listSize() const
{
return length;
}
// method to return the maximum size of the list
int arrayListType::maxListSize() const
{
return maxSize;
}
// method to print the list
void arrayListType::print() const
{
for(int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
// method to check whether an item is located at a given location
bool arrayListType::isItemAtEqual(int location, int item) const
{
if(location < 0 || location >= length)
{
cout << "The location is out of range." << "This is an error, you shouldn't be here!" << endl;
return false;
}
else
return (list[location] == item);
}
// remove item at given location
void arrayListType::removeAt(int location)
{
if(location < 0 || location >= length)
cout << "The location is out of range."
<< "This is an error, you shouldn't be here!" << endl;
else
{
for(int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}
// get item at given location
void arrayListType::retrieveAt(int location, int &retItem) const
{
if(location < 0 || location >= length)
cout << "The location is out of range."
<< "This is an error, you shouldn't be here!" << endl;
else
retItem = list[location];
}
// empty the list
void arrayListType::clearList()
{
length = 0;
}
// constrivtoe to initialize the size of list
arrayListType::arrayListType(int size)
{
if(size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array with default size of 100." << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int[maxSize];
}
// destructor to delete the memory taken by list
arrayListType::~arrayListType()
{
delete [] list;
}
// constructor to copy the list
arrayListType::arrayListType(const arrayListType &otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new int[maxSize];
for(int j = 0; j < length; j++)
list[j] = otherList.list[j];
}
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.