Implement a C++ \'List\' class to handle a list with general operations. That me
ID: 668311 • Letter: I
Question
Implement a C++ 'List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. The list has no order, except for the order you insert or delete. Below is the main.cpp file to be used to incorporate all methods. The methods you are to implement are as given:
Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)' )
empty returns true or false if list is empty or not.
front makes current position at the beginning of the list
end makes current position at the end of a list.
prev places current position at the previous element in the list
next places current position at the next element in the list
getPos returns current position or where you are in the list
setPos(int) places current position in a certain position in the list
insertBefore inserts a new element before the current position
insertAfter inserts a new element after the current position
get Element returns the one element that current position is pointing to
size returns the size of the list (number of elements in list)
replace(int) replace the current element with a new value
erase deletes the current element
clear makes the list an empty list
reverse reverse elements in a list
swap swaps all the elements of one list with the another list.
overload the operators: (at least) << output, == , + , = (assignment)
You are to implement the List class using an array. For now the array can be 20 in size.
You will need to use the 'ElementType' for 'typing' your data.
You will need to use CAPACITY constant for the size of the array, in case we need to change the array size.
The elements in the list should be left justified. ‘pos’ should point to the element just inserted. ‘pos’ should never be outside of element list.
Main.cpp File:
int main()
{
List a,b; int endit;
for (int i=1;i<=20;i++)
a.insertAfter(i*2);
cout << "List a : " << endl;
cout << " " << a << endl;
cout << "Number of elements in a - " << a.size() << endl;
for (int i=1;i<=20;i++)
b.insertBefore(i*2);
cout << "List b : " << endl;
cout << " " << b << endl;
cout << "Number of elements in b - " << b.size() << endl;
if ( a == b )
cout << "List a & b are equal" << endl;
else
cout << "List a & b are Not equal" << endl;
a.front();
b.front();
cout << "First elmenet in list a & b: " << a.getElement() << ", "
<< b.getElement() << endl;
for ( int i = 0; i < a.size(); a.next(),i++);
for ( int i = 0; i < b.size(); b.next(),i++);
cout << "Last elmenet in list a & b: " << a.getElement() << ", "
<< b.getElement() << endl;
cout << endl << endl << " Start of new stuff" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
a.front();
b.front();
endit = a.size()/2;
for ( int i = 1; i<endit; i++)
{ a.next();
b.next();
}
cout << "New position in Obj 'a' middle position = " << a.size()/2 << endl;
for ( int i=1; i<8; i++)
{
a.erase();
b.setPos(i);
b.replace(i*i);
}
cout << "Modified Object 'a' " << endl;
cout << "List a: " << a << endl;
cout << "Modified Object 'b' " << endl;
cout << "List b: " << b << endl;
List c(b);
cout << "Object 'b' assigned to Object List c(b) Copy constructor" << endl;
cout << "List b : " << b << endl;
cout << "List c : " << c << endl;
if ( c == b )
cout << "List c & b are equal" << endl;
else
cout << "List c & b are Not equal" << endl;
List e;
e = c;
cout << "Object 'c' assigned to Object 'e' Assignment Statement:" << endl;
cout << "List c : " << c << endl;
cout << "List e : " << e << endl;
List d;
d=a;
cout << "List 'd' assign list 'a' Assignment Statement: " << endl;
cout << "List a: " << a << endl;
cout << "List d: " << d << endl;
d.front();
endit = d.size()/2;
for (int i=1; i<=endit; i++)
{
d.next();
d.erase();
}
cout << "Results after some erases: Object d " << endl;
cout << "List d : " << d << endl;
d.front();
endit = d.size();
for ( int i = 1; i < endit; d.next(), i++)
{
d.insertBefore(d.getElement()*2);
d.next();
}
cout << "Results after some Replaces on d " << endl;
cout << "List d : " << d << endl;
a.front();
endit = a.size();
for ( int i = 1; i < endit; a.next(), i++)
{
a.insertBefore(a.size()+a.getElement());
a.next();
}
cout << "Results after some weird stuff on list a" << endl;
cout << "List a : " << a << endl;
List alist(b);
cout << "'alist' and b list are the same: " << endl;
cout << "List alist : " << alist << endl;
cout << "List b : " << b << endl;
alist.clear();
for (int i=1;i<=20;i++)
alist.insertAfter( alist.getPos() );
cout << "New List alist : " << endl;
cout << " " << alist << endl;
alist.reverse();
cout << "Reverse New List alist : " << endl;
cout << " " << alist << endl;
List newa;
for (int i=1;i<=20;i++)
newa.insertAfter(i*3);
cout << "List alist and newa before swap " << endl;
cout << " " << alist << endl;
cout << " " << newa << endl;
alist.swap(newa);
cout << "List alist and newa after swap " << endl;
cout << " " << alist << endl;
cout << " " << newa << endl;
cout << endl << " check out boundary conditions" << endl;
List bounds;
cout << "number of elements in bounds = " << bounds.size() << endl;
bounds.front();
bounds.erase();
cout << "bounds list: " << bounds << endl;
bounds.insertBefore(9999);
cout << "bounds list: " << bounds << endl;
bounds.next(); bounds.next();
cout << "bounds.getElement() = " << bounds.getElement() << endl;
bounds.erase();
cout << "bounds list: " << bounds << endl;
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
const int CAPACITY = 20;
typedef int ElementType; // whatever you want
class listFiverror {
};
class List
{
int pos;
int size;
ElementType elem [CAPACITY];
void shiftup (int n) {
for (int i = size; i >= n; i--)
elem[i+1] = elem[i];
}
public:
List() { size = 0; pos = -1; }
List (List &list) {
for (int i = 0; i < list.size; i++)
elem[i] = list.elem[i];
size = list.size;
pos = list.pos;
}
bool empty() { return (size == 0); }
void first() { if (size > 0) pos = 0; }
void last() { if (size > 0) pos = size - 1; }
void prev() { if (pos > 0) pos--; }
void next() { if (pos < size - 1) pos++; }
int getPos() { return pos; }
void setPos (int pos_in) {
if (pos_in < 0 || pos_in >= size)
throw listFiverror();
else
pos = pos_in;
}
ElementType getElement() {
if (size == 0) throw listFiverror();
else return elem[pos];
}
void insertBefore (ElementType e) {
if (size == 0) {
elem[0] = e;
pos = 0;
size = 1;
}
else {
if (size >= CAPACITY) throw listFiverror();
shiftup (pos);
elem[pos] = e;
size++;
}
}
void insertAfter (ElementType e) {
if (size == 0) {
elem[0] = e;
pos = 0;
size = 1;
}
else {
if (size >= CAPACITY) throw listFiverror();
shiftup (pos+1);
elem[pos+1] = e;
size++;
}
}
void clear() {
size = 0; pos = -1;
}
friend ostream& operator<< (ostream &os, List &list);
};
ostream& operator<< (ostream &os, List &list) {
for (int i = 0; i < list.size; i++)
os << list.elem[i] << endl;
return os;
}
int main()
{
List listMain, listTwo;
int const Max=20;
int val;
cout << " Pos" << listMain.getPos() << endl;
cout << " Empty? " << listMain.empty() << endl;
cout << "To Start: listMain Empty " << listMain << endl;
for ( int k = 1; k <Max; k++ )
{
listMain.insertAfter(k);
listMain.next();
}
cout << "listMain " << listMain << endl;
for ( int k = 1; k <Max; k++ )
{
listTwo.insertBefore(k);
}
cout << "listTwo " << listTwo << endl;
listTwo.clear();
for ( int k = 0; k <Max/2; k++ )
{
listTwo.last();
listTwo.insertAfter(k);
listTwo.first();
listTwo.insertBefore(k);
}
cout << "listTwo " << listTwo << endl;
listTwo.clear();
ElementType> listTwo.insertBefore( one );
for ( int k = 1; k <Max; k++ )
{
val = listTwo.getElement() * 2;
listTwo.insertAfter(val);
listTwo.next();
}
cout << "listTwo " << listTwo << endl;
List listFive;
ElementType two = 2;
listFive.insertAfter(one);
listFive.next();
listFive.insertAfter(two);
for ( int k = 2; k <Max; k++ )
{
val = listFive.getElement();
listFive.next();
val = val + listFive.getElement();
listFive.insertAfter(val);
}
cout << "listFive " << listFive << endl;
List listThree(listMain), listFour;
listFour = listTwo;
cout << " listThree " << listThree << endl;
cout << " listFour " << listFour << endl;
listFour.clear();
cout << " Cleared listFour " << listFour << endl;
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.