Implement a c++ \'List\' class to handle a list with general operations. That me
ID: 662962 • 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. 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.
Invariants: 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.
Note*: An array of pointers are needed for the program.
Explanation / Answer
Answer:
Program code to copy:
// ListClassImplementation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
const int CAPACITY = 20;
typedef int ElementType; //type of the element in the array
class listError{
};
//List class
class List
{
//class variables
int pos;
int asize;
ElementType elements [CAPACITY];
//private method to shift the elements
void shiftup (int n)
{
for (int i = asize; i >= n; i--)
elements[i+1] = elements[i];
}
public:
//default constructor
List()
{
asize = 0;
pos = -1;
}
//copy constructor
List (List &list)
{
for (int i = 0; i < list.asize; i++)
elements[i] = list.elements[i];
asize = list.asize;
pos = list.pos;
}
//empty returns true or false if list is empty or not.
bool empty()
{
return (asize == 0);
}
//front makes current position at the beginning of the list
void front()
{
if (asize > 0)
pos = 0;
}
//end makes current position at the end of a list.
void end()
{
if (asize > 0)
pos = asize - 1;
}
//prev places current position at the previous element in the list
void prev()
{
if (pos > 0)
pos--;
}
//next places current position at the next element in the list
void next()
{
if (pos < asize - 1)
pos++;
}
//getPos returns current position or where you are in the list
int getPos()
{
return pos;
}
//setPos(int) places current position in a certain position in the list
void setPos (int pos_in)
{
if (pos_in < 0 || pos_in >= asize)
throw listError();
else
pos = pos_in;
}
//insertBefore inserts a new element before the current position
void insertBefore (ElementType ele)
{
if (asize == 0)
{
elements[0] = ele;
pos = 0;
asize = 1;
}
else
{
if (asize >= CAPACITY)
throw listError();
shiftup (pos);
elements[pos] = ele;
asize++;
}
}
//insertAfter inserts a new element after the current position
void insertAfter (ElementType ele)
{
if (asize == 0)
{
elements[0] = ele;
pos = 0;
asize = 1;
}
else
{
if (asize >= CAPACITY) throw listError();
shiftup (pos+1);
elements[pos+1] = ele;
asize++;
}
}
//get Element returns the one element that current position is pointing to
ElementType getElement()
{
if (asize == 0)
throw listError();
else
return elements[pos];
}
//size returns the size of the list (number of elements in list)
int size()
{
return asize;
}
//replace(int) replace the current element with a new value
void replace(int newEle)
{
elements[pos] = newEle;
}
//clear makes the list an empty list
void clear()
{
asize = 0; pos = -1;
}
//erase deletes the current element
void erase()
{
for(int i = getPos(); i < asize; i++)
{
elements[i] = elements[i + 1];
}
asize--;
}
//reverse reverse elements in a list
void reverse()
{
for(int i = 0; i <asize; i++)
{
int tmp = elements[i];
elements[i] = elements[asize - i];
elements[asize - i] = tmp;
}
}
//swap swaps all the elements of one list with the another list.
void swap(List &secondList)
{
int temp = 0;
if(secondList.size() == this->size())
{
for(int i=0;i<this->size();i++)
{
temp = elements[i];
secondList.setPos(i);
setPos(i);
replace(secondList.getElement());
secondList.replace(temp);
}
}
else
throw listError();
}
//overload the operator ==
bool operator==(List &list)
{
if(list.size()==this->size())
{
for(int i=0;i<this->size();i++)
{
list.setPos(i);
if(list.getElement()!=elements[i])
return false;
}
return true;
}
else
return false;
}
//overload the operator =
void operator=(List &list)
{
for (int i = 0; i < list.asize; i++)
elements[i] = list.elements[i];
asize = list.asize;
pos = list.pos;
}
//overload the operator <<
friend ostream& operator<< (ostream &os, List &list);
};
//definition of overload operator <<
ostream& operator<< (ostream &os, List &list)
{
for (int i = 0; i < list.asize; i++)
os << list.elements[i] << endl;
return os;
}
//main function
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;
a.front();
endit = a.size();
cout<<"Sizes: "<<a.size()<<endl;
endit = 20-a.size();
for ( int i = 1; i < endit; a.next(), i++)
{
a.insertBefore(a.size()+a.getElement());
}
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;
cout << "List alist and b before swap " << endl;
cout << " " <<alist << endl;
cout << " " << b << endl;
alist.swap(b);
cout << "List alist and b after swap " << endl;
cout << " " << alist << endl;
cout << " " << b << endl;
system("pause");
return 0;
}
Sample output:
List a :
2
40
38
36
34
32
30
28
26
24
22
20
18
16
14
12
10
8
6
4
Number of elements in a - 20
List b :
40
38
36
34
32
30
28
26
24
22
20
18
16
14
12
10
8
6
4
2
Number of elements in b - 20
List a & b are Not equal
First elmenet in list a & b: 2, 40
Last elmenet in list a & b: 4, 2
Start of new stuff
a = 2
40
38
36
34
32
30
28
26
24
22
20
18
16
14
12
10
8
6
4
b = 40
38
36
34
32
30
28
26
24
22
20
18
16
14
12
10
8
6
4
2
New position in Obj 'a' middle position = 10
Modified Object 'a'
List a: 2
40
38
36
34
32
30
28
26
10
8
6
4
Modified Object 'b'
List b: 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
Object 'b' assigned to Object List c(b) Copy constructor
List b : 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
List c : 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
List c & b are equal
Object 'c' assigned to Object 'e' Assignment Statement:
List c : 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
List e : 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
List 'd' assign list 'a' Assignment Statement:
List a: 2
40
38
36
34
32
30
28
26
10
8
6
4
List d: 2
40
38
36
34
32
30
28
26
10
8
6
4
Results after some erases: Object d
List d : 2
38
34
30
26
8
4
Sizes: 13
Results after some weird stuff on list a
List a : 15
16
17
18
19
20
2
40
38
36
34
32
30
28
26
10
8
6
4
'alist' and b list are the same:
List alist : 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
List b : 40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
New List alist :
-1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Reverse New List alist :
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
List alist and b before swap
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
List alist and b after swap
40
1
4
9
16
25
36
49
24
22
20
18
16
14
12
10
8
6
4
2
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.