c++ just one file for all of it.
You are to implement a 'list' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. your list will be an array. 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.
first makes current position at the beginning of the list
last makes current position at the end of a list
prev places current position at the previous 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
getelement 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
overload the operators: <<output, ==, !=, +, +=, =(assignment)
you are to implement the list class using an array. the array can be 20 in size.
you will need to use the 'element type' 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 are to be left justified with in the array. 'pos' will point to a valid location in the array (ie where data is located.
on insert, 'pos' should point to the element just inserted on insert operations. 'pos' should never be outside of element list.
test the code with a main program.
PROGRAM
#include<iostream>
#include<stdlib.h>
#include <iomanip>
using namespace std;
const int CAPACITY = 20;
template<class T>
class List
{
//private members
private:
T arr[CAPACITY];
int num_used;
int cur_pos;
public:
//default constructor
List()
{
num_used = 0;
cur_pos = -1;
}
//copy constructor
List(const List& list)
{
num_used = list.num_used;
for(int i=0; i<num_used;
i++)
{
arr[i] =
list.arr[i];
}
}
//returns true or false if list is empty or not.
bool empty()
{
return
((num_used==0)?true:false);
}
//makes current position at the beginning of the
list
void first()
{
cur_pos = 0;
}
//makes current position at the end of a list
void last()
{
cur_pos = num_used - 1;
}
//places current position at the previous element in
the list
void prev()
{
cur_pos--;
}
//returns current position or where you are in the
list
int getpos()
{
return cur_pos;
}
//places current position in a certain position in the
list
void setpos(int i)
{
if(i>=0 &&
i<num_used)
cur_pos =
i;
}
//inserts a new element before the current
position
void insertbefore(int item)
{
for(int i=num_used; i>=cur_pos;
i--)
arr[i+1] =
arr[i];
arr[cur_pos] = item;
num_used++;
}
//inserts a new element after the current
position
void insertafter(int item)
{
cur_pos++;
for(int i=num_used; i>=cur_pos;
i--)
arr[i+1] =
arr[i];
arr[cur_pos] = item;
num_used++;
}
//returns the one element that current position is
pointing to
T getelement()
{
return arr[cur_pos];
}
//returns the size of the list (number of elements in
list)
int size()
{
return num_used;
}
//replace the current element with a new value
void replace(T item)
{
arr[cur_pos] = item;
}
//deletes the current element
T erase()
{
T item = arr[cur_pos];
for(int i=cur_pos; i<num_used-1;
i++)
arr[i] =
arr[i+1];
num_used--;
return item;
}
//makes the list an empty list
void clear()
{
num_used = 0;
cur_pos = -1;
}
// << operator overloading
friend ostream& operator<<(ostream &out,
const List &list)
{
out<<endl<<"[";
for(int i=0; i<list.num_used;
i++)
out<<list.arr[i]<<" ";
out<<"]"<<endl;
return out;
}
// == operator overloading
bool operator==(const List &list)
{
if(num_used!=list.num_used)
return
false;
for(int i=0; i<num_used;
i++)
{
if(arr[i]!=list.arr[i])
return false;
}
return true;
}
// != operator overloading
bool operator!=(const List &list)
{
if(num_used!=list.num_used)
return
true;
for(int i=0; i<num_used;
i++)
{
if(arr[i]!=list.arr[i])
return true;
}
return false;
}
// = operator overloading
List& operator=(const List &list)
{
num_used = list.num_used;
for(int i=0; i<num_used;
i++)
{
arr[i] =
list.arr[i];
}
return *this;
}
// + operator overloading
List operator+(const List &b)
{
List a;
a.num_used = num_used +
b.num_used;
for(int i=0; i<num_used;
i++)
{
a.arr[i] =
arr[i];
}
for(int k=0; k<b.num_used;
k++)
{
a.arr[num_used +
k] = b.arr[k];
}
return a;
}
// += operator overloading
List& operator+=(const List &b)
{
for(int k=0; k<b.num_used;
k++)
{
arr[num_used +
k] = b.arr[k];
}
num_used = num_used +
b.num_used;
return *this;
}
};
int main()
{
//create objects
List<int>a;
List<int> b;
List<int> c;
//insert elements
a.insertafter(10);
a.insertafter(20);
a.insertafter(30);
a.insertafter(40);
a.insertafter(50);
//print a
cout<<a;
//insert elements
b.insertafter(60);
b.insertafter(70);
b.insertafter(80);
//print b
cout<<b;
//add two list
c = a + b;
//print c
cout<<c;
//clear c
c.clear();
//assign a to c
c = a;
//print c
cout<<c;
//set current position
c.setpos(2);
//delete an element
c.erase();
//print c
cout<<c;
/* Testing */
List<int> d(a);
cout<<d;
cout<<boolalpha<<d.empty()<<endl;
d.first();
cout<<d.getpos()<<endl;
cout<<d.getelement()<<endl;
d.last();
cout<<d.getpos()<<endl;
cout<<d.getelement()<<endl;
d.prev();
cout<<d.getpos()<<endl;
cout<<d.getelement()<<endl;
d.insertbefore(90);
cout<<d;
d.replace(55);
cout<<d;
cout<<d.erase()<<endl;
cout<<d;
cout<<(a==d)<<endl;
cout<<(a!=d)<<endl;
d+=c;
cout<<d;
return 0;
}
Sample Output:
[10 20 30 40 50 ]
[60 70 80 ]
[10 20 30 40 50 60 70 80 ]
[10 20 30 40 50 ]
[10 20 40 50 ]
[10 20 30 40 50 ]
false
0
10
4
50
3
40
[10 20 30 90 40 50 ]
[10 20 30 55 40 50 ]
55
[10 20 30 40 50 ]
true
false
[10 20 30 40 50 10 20 40 50 ]
--------------------------------
Process exited after 0.248 seconds with return value 0
Press any key to continue . . .
Get Answers For Free
Most questions answered within 1 hours.