**Please make sure to use Visual Studio(C++). Thank you.** ListArray Class Descr
ID: 3890653 • Letter: #
Question
**Please make sure to use Visual Studio(C++). Thank you.**
ListArray Class
Description: You will implement your own List ADT, the ListArray class
#ifndef LISTARRAY_CPP
#define LISTARRAY_CPP
using namespace std;
#include
#include
#include "ListArray.h"
template < typename DataType >
List::List ( int maxNumber )
// Creates an empty list. Allocates enough memory for maxNumber
// data items (defaults to defMaxListSize).
{
}
template < typename DataType >
List::List ( const List& source )
// Copy constructor. Creates a deep-copy list copied from
// the provided model object, source.
{
}
template < typename DataType >
List& List::operator= ( const List& source )
// Overloaded assignment operator. Resets a list object to contain a
// deep-copy of the provided model object, source.
{
return *this;
}
template < typename DataType >
// Frees the memory used by a list.
List::~List ()
{
}
template < typename DataType >
void List::insert ( const DataType& newDataItem )
throw ( logic_error )
// Inserts newDataItem after the cursor. If the list is empty, then
// newDataItem is inserted as the first (and only) data items in the
// list. In either case, moves the cursor to newDataItem.
{
}
template < typename DataType >
void List::remove () throw ( logic_error )
// Removes the data items marked by the cursor from a list. Moves the
// cursor to the next data item item in the list. Assumes that the
// first list data items "follows" the last list data item.
{
}
template < typename DataType >
void List::replace ( const DataType& newDataItem )
throw ( logic_error )
// Replaces the item marked by the cursor with newDataItem and
// leaves the cursor at newDataItem.
{
}
template < typename DataType >
void List::clear ()
// Removes all the data items from a list.
{
}
template < typename DataType >
bool List::isEmpty () const
// Returns 1 if a list is empty. Otherwise, returns 0.
{
return false;
}
template < typename DataType >
bool List::isFull () const
// Returns 1 if a list is full. Otherwise, returns 0.
{
return false;
}
template < typename DataType >
void List::gotoBeginning ()
throw ( logic_error )
// Moves the cursor to the beginning of the list.
{
}
template < typename DataType >
void List::gotoEnd ()
throw ( logic_error )
// Moves the cursor to the end of the list.
{
}
template < typename DataType >
bool List::gotoNext ()
throw ( logic_error )
// If the cursor is not at the end of a list, then moves the
// cursor to the next item in the list and returns true. Otherwise,
// returns false.
{
return false;
}
template < typename DataType >
bool List::gotoPrior ()
throw ( logic_error )
// If the cursor is not at the beginning of a list, then moves the
// cursor to the preceeding item in the list and returns true.
// Otherwise, returns false.
{
return false;
}
template < typename DataType >
DataType List::getCursor () const
throw ( logic_error )
// Returns the item marked by the cursor.
{
DataType t;
return t;
}
#include "show3.cpp"
template < typename DataType >
void List::moveToNth ( int n )
throw ( logic_error )
// Removes the data item marked by the cursor from a list and
// reinserts it as the nth data item in the list -- where the
// data items are numbered from beginning to end, starting with 0.
// Moves the cursor to the new position of the moved data
// item.
{
}
template < typename DataType >
bool List::find ( const DataType& searchDataItem )
throw ( logic_error )
// Searches a list for searchDataItem. Begins the search with the
// data items marked by the cursor. Moves the cursor through the list
// until either searchDataItem is found (returns true) or the end of the
// list is reached (returns false).
{
return false;
}
#endif // ifndef LISTARRAY_CPP
__________________________________________________________________________________
***LIST ARRAY.H***
#ifndef LISTARRAY_H
#define LISTARRAY_H
#include <stdexcept>
#include <iostream>
using namespace std;
#pragma warning( disable : 4290 )
template < typename DataType >
class List
{
public:
static const int MAX_LIST_SIZE = 10; // Default maximum list size
// Constructors
List ( int maxNumber = MAX_LIST_SIZE ); // Default constructor
List ( const List& source ); // Copy constructor
// Overloaded assignment operator
List& operator= ( const List& source );
// Destructor
virtual ~List ();
// List manipulation operations
virtual void insert ( const DataType& newDataItem ) // Insert after cursor
throw ( logic_error );
void remove () throw ( logic_error ); // Remove data item
virtual void replace ( const DataType& newDataItem ) // Replace data item
throw ( logic_error );
void clear (); // Clear list
// List status operations
bool isEmpty () const; // List is empty
bool isFull () const; // List is full
// List iteration operations
void gotoBeginning () // Go to beginning
throw ( logic_error );
void gotoEnd () // Go to end
throw ( logic_error );
bool gotoNext () // Go to next data item
throw ( logic_error );
bool gotoPrior () // Go to prior data item
throw ( logic_error );
DataType getCursor () const
throw ( logic_error ); // Return data item
// Output the list structure -- used in testing/debugging
virtual void showStructure () const;
// In-lab operations
void moveToNth ( int n ) // Move data item to pos. n
throw ( logic_error );
bool find ( const DataType& searchDataItem ) // Find data item
throw ( logic_error );
protected:
// Data members
int maxSize,
size, // Actual number of data item in the list
cursor; // Cursor array index
DataType *dataItems; // Array containing the list data item
};
#endif
Explanation / Answer
Answer:
File Name: ListArray.h
#ifndef LISTARRAY_H
#define LISTARRAY_H
#include <stdexcept>
#include <iostream>
using namespace std;
#pragma warning( disable : 4290 )
template < typename DataType >
class List
{
public:
static const int MAX_LIST_SIZE = 10; // Default maximum list size
// Constructors
List ( int maxNumber = MAX_LIST_SIZE ); // Default constructor
List ( const List& source ); // Copy constructor
// Overloaded assignment operator
List& operator= ( const List& source );
// Destructor
virtual ~List ();
// List manipulation operations
virtual void insert ( const DataType& newDataItem ) // Insert after cursor
throw ( logic_error );
void remove () throw ( logic_error ); // Remove data item
virtual void replace ( const DataType& newDataItem ) // Replace data item
throw ( logic_error );
void clear (); // Clear list
// List status operations
bool isEmpty () const; // List is empty
bool isFull () const; // List is full
// List iteration operations
void gotoBeginning () // Go to beginning
throw ( logic_error );
void gotoEnd () // Go to end
throw ( logic_error );
bool gotoNext () // Go to next data item
throw ( logic_error );
bool gotoPrior () // Go to prior data item
throw ( logic_error );
DataType getCursor () const
throw ( logic_error ); // Return data item
// Output the list structure -- used in testing/debugging
virtual void showStructure () const;
// In-lab operations
void moveToNth ( int n ) // Move data item to pos. n
throw ( logic_error );
bool find ( const DataType& searchDataItem ) // Find data item
throw ( logic_error );
protected:
// Data members
int maxSize,
size, // Actual number of data item in the list
cursor; // Cursor array index
DataType *dataItems; // Array containing the list data item
};
#endif
File Name: ListArray.cpp
#ifndef LISTARRAY_CPP
#define LISTARRAY_CPP
using namespace std;
#include <iostream>
#include <string>
#include <stdexcept>
#include "ListArray.h"
template < typename DataType >
List<DataType >::List ( int maxNumber )
{
size=0;
maxSize=maxNumber;
dataItems= new DataType[maxSize];
cursor = -1;
}
template<typename DataType >
List<DataType >::List ( const List& source )
{
size = source.size;
maxSize = source.maxSize;
dataItems= new DataType[maxSize];
cursor = source.cursor;
for(int kk=0;kk<size;kk++)
dataItems[kk]=source.dataItems[kk];
}
template < typename DataType >
List<DataType >& List<DataType >::operator= ( const List& source )
{
if(this!= &source)
{
maxSize = source.maxSize;
delete[] dataItems;
dataItems= new DataType[maxSize];
size = source.size;
cursor = source.cursor;
for(int kk=0;kk<size;kk++)
dataItems[kk]=source.dataItems[kk];
}
return *this;
}
template < typename DataType >
List<DataType >::~List ()
{
delete[] dataItems;
size=0;
}
template < typename DataType >
void List<DataType >::insert ( const DataType& newDataItem )
throw ( logic_error )
{
//check condition
if(isFull())
//error
throw logic_error("FULL");
else
{
if(cursor==-1)
cursor =0;
else
{
for(int kk=size-2;kk>cursor;kk--)
dataItems[kk+1]=dataItems[kk];
cursor++;
}
dataItems[cursor]=newDataItem;
size++;
}
}
template < typename DataType >
void List<DataType >::remove () throw ( logic_error )
{
//check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
{
if(cursor==size-1)
{
size--;
gotoBeginning();
}
else
{
for(int kk=cursor;kk<size;kk++)
dataItems[kk]=dataItems[kk+1];
size--;
}
}
}
template < typename DataType >
void List<DataType >::replace ( const DataType& newDataItem )
throw ( logic_error )
{
//check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
dataItems[cursor]=newDataItem;
}
template < typename DataType >
void List<DataType >::clear ()
{
cursor = -1;
delete[] dataItems;
size=0;
}
template < typename DataType >
bool List<DataType >::isEmpty () const
{
//check condition
if(size==0)
//return
return true;
return false;
}
template < typename DataType >
bool List<DataType >::isFull () const
{
//check condition
if(size==maxSize)
//return
return true;
return false;
}
template < typename DataType >
void List<DataType >::gotoBeginning ()
throw ( logic_error )
{
//check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
cursor =0;
}
template < typename DataType >
void List<DataType >::gotoEnd ()
throw ( logic_error )
{
//check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
cursor = size -1;
}
template < typename DataType >
bool List<DataType >::gotoNext ()
throw ( logic_error )
{
////check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
{
if(cursor<size-1)
{
cursor++;
return true;
}
}
return false;
}
template < typename DataType >
bool List<DataType >::gotoPrior ()
throw ( logic_error )
{
//check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
{
if(cursor!=0)
{
cursor--;
return true;
}
}
return false;
}
template < typename DataType >
DataType List<DataType >::getCursor () const
throw ( logic_error )
{
//check condition
if(isEmpty())
//check condition
throw logic_error("EMPTY");
else
return dataItems[cursor];
}
template <typename DataType>
void List<DataType>:: showStructure () const
{
int kk;
if ( size == 0 )
cout << "EMPTY" << endl;
else
{
cout << "size = " << size
<< " cursor = " << cursor << endl;
for ( kk = 0 ; kk < maxSize ; kk++ )
cout << kk << " ";
cout << endl;
for ( kk = 0 ; kk < size ; kk++ ) {
if( kk == cursor ) {
cout << "[";
cout << dataItems[kk];
cout << "]";
cout << " ";
}
else
cout << dataItems[kk]<< " ";
}
cout << endl;
}
}
template < typename DataType >
void List<DataType >::moveToNth ( int n )
throw ( logic_error )
{
//check condition
if(size<n+1)
//error
throw logic_error("EMPTY");
else
{
DataType tp;
tp=getCursor();
remove();
gotoBeginning();
if(n==0)
{
size++;
for(int kk=size-1;kk>=cursor;kk--)
dataItems[kk+1]=dataItems[kk];
dataItems[0]=tp;
}
else
{
for(int kk=0;kk<n-1;kk++)
gotoNext();
insert(tp);
}
}
}
template < typename DataType >
bool List<DataType >::find ( const DataType& searchDataItem )
throw ( logic_error )
{
////check condition
if(isEmpty())
//error
throw logic_error("EMPTY");
else
{
for(int kk=cursor;kk<size;kk++)
if(dataItems[kk]==searchDataItem)
{
cursor = kk;
return true;
}
cursor = size - 1;
}
return false;
}
#endif
File Name: main.cpp
#include <iostream>
#include <string>
#include <stdexcept>
#include "ListArray.cpp"
//#include "ListArray.cpp"
using namespace std;
int main()
{
List<int> mylist(10);
mylist.insert(1);
mylist.insert(2);
mylist.insert(3);
mylist.insert(4);
mylist.insert(5);
mylist.insert(6);
mylist.insert(7);
mylist.insert(8);
mylist.insert(9);
mylist.insert(10);
mylist.showStructure();
system("pause");
return 0;
}
Sample output:
size = 10 cursor = 9
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 [10]
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.