Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ program You are to implement a 'List' class to handle a list with general operations. That means you can insert and delete any element

c++ program

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 (one dimensional array). Your general list will store X Y coordinates (store x coordinate beside y coordinate), i.e. pairs of numbers in the array. The list has no order, except for the order you insert or delete. The methods you are to implement are as given:

use type_def

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

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(int,int) inserts a new element before the current position

insertAfter(int, int) inserts a new element after the current position

get X Element returns the one element that current position is pointing to

get Y 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, int) replace the current element with a new value X Y value

erase deletes the current element

clear makes the list an empty list

overload the operators: (at least) << output, ==, !=, +,+=, { =(assignment) maybe? }

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 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.

The main program to test the code is below.

int main() { List a,b; int endit;

for (int i=1;i<=10;i++) a.insertAfter(i,i*2); cout << "List a : " << endl; cout << " " << a << endl; cout << "Number of elements in a - " << a.size() << endl;

for (int i=1;i<=10;i++) b.insertBefore(i,i*2); cout << "List b : " << endl; cout << " " << b << endl; cout << "Number of elements in b - " << b.size() << endl;

if ( a == b ) cout << "a == b: List a & b are equal" << endl; else cout << "a == b: List a & b are Not equal" << endl;

a.first(); b.first(); cout << "First elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl; cout << "First elmenet in list b " << b.getXElement() << "," << b.getYElement() << endl; a.last(); b.last(); cout << "Last elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl; cout << "Last elmenet in list b " << b.getXElement() << "," << b.getYElement() << endl;

cout << endl << endl << " Start of new stuff" << endl;

b.erase(); cout << "b.erase(): Print Empty List b: " << b << endl;

if ( a != b ) cout << "a != b: List a & b are not equal" << endl; else cout << "a != b: List a & b are equal" << endl;

for (int i=1;i<=10;i++) b.insertBefore(i, -i*2); cout << "List b with neg y's" << endl; cout << "List b: " << b << endit;

a.setPos(5); b.first(); for ( int i=1; i<4; i++) { a.erase(); b.replace(i, i*3); b.next(); }

cout << "Modified Object 'a' (erase 3) " << endl; cout << "List a: " << a << endl; cout << "Modified Object 'b' (replace 3)" << endl; cout << "List b: " << b << endl;

List c(b); cout << "Copy Constructor c(b)" << endl; cout << "List b : " << b << endl; cout << "List c : " << c << endl;

List e; e = c; cout << "Object 'c' assigned to Object 'e':" << endl; cout << "List c : " << c << endl; cout << "List e : " << e << endl;

List d; d=a;

d.first(); endit = d.size()/2; for ( int i = 1; i < endit; d.next(), i++) { d.insertBefore(d.getXElement()*2,d.getYElement()*2); d.next(); } cout << "Results after some inserts at front of d " << endl; cout << "List d : " << d << endl;

a.first(); endit = a.size(); for ( int i = 1; i < endit; a.next(), i++) { a.replace(a.getPos()-a.getXElement(),a.getPos()-a.getYElement()); a.next(); } cout << "Results after adding pos & flipping signs on list a" << endl; cout << "List a : " << a << endl;

List alist(b); alist.clear(); for (int i=1;i<=5;i++) alist.insertAfter(i,i*5); alist.first(); cout << "New List alist with positions printed above: " << endl; for (int i=1;i<=5;i++) { cout << setw(8) << alist.getPos(); alist.next(); } cout << endl; alist.first(); for (int i=1;i<=5;i++) { cout << setw(6) << alist.getXElement() << "," << alist.getYElement(); alist.next(); } cout << endl;

cout << endl << " check out boundary conditions" << endl; List sq; cout << "number of elements in empty sq list = " << sq.size() << endl; cout << " print empty list sq: " << sq << endl; sq.first(); sq.erase(); sq.erase(); cout << "First elmenet in list a " << a.getXElement() << "," << a.getYElement() << endl; sq.setPos(5); cout << "empty sq values " << sq << endl; sq.insertBefore(333,444); cout << "sq list: " << sq << endl; sq.next(); sq.next(); cout << "sq.getElement() = " << sq.getXElement() << "," << sq.getYElement() << endl; cout << "sq list = " << sq << endl; sq.prev(); sq.prev(); cout << "sq.getElement() = " << sq.getXElement() << "," << sq.getYElement() << endl; cout << "sq list = " << sq << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions