The sequence in C++ Assignment Sample
The C++ programming assignment is to write a class that performs like a Java ArrayList. There should be a constructor that takes a size of elements, and also one that acts as a copy constructor. You can access the elements using [] or at(). You can also insert elements directly and move the following elements with insert(position, value). You can use pop_back() to remove the last entry or erase(start, count) to remove a number of entries.
Solution:
sequence.cpp
#include “Sequence.h”
#include
/*
*
*NAME:
*SECTION:
*INSTRUCTOR:
*DESCRIPTION: Sequence method implementations.
*
*/
const Sequence::value_type not_defined=999999;
Sequence::Sequence( size_type sz )
{
structure=new value_type[sz];
numElts = sz;
for(int i=0;i
structure[i]=not_defined;
}
Sequence::Sequence( Sequence& s )
{
numElts=s.size();
structure=new Sequence::value_type[numElts];
for(int i=0;i
structure[i]=s.at(i);
}
Sequence::~Sequence()
{
delete(structure);
}
Sequence& Sequence::operator=( Sequence& s )
{
numElts=s.size();
structure=new Sequence::value_type[numElts];
for(int i=0;i
structure[i]=s.at(i);
return *this;
}
Sequence::value_type& Sequence::operator[]( size_type position )
{
return structure[position];
}
Sequence::value_type& Sequence::at( size_type position )
{
return structure[position];
}
void Sequence::push_back( const value_type& value )
{
if(numElts==0)
throw exception();
size_type new_size=numElts+1;
value_type * old=structure;
structure=new value_type[new_size];
numElts=new_size;
for(int i=0;i
structure[i]=old[i];
structure[numElts-1]=value;
}
void Sequence::pop_back()
{
if(numElts==0)
throw exception();
size_type new_size=numElts-1;
value_type * old=structure;
structure=new value_type[new_size];
numElts=new_size;
for(int i=0;i
structure[i]=old[i];
}
void Sequence::insert( size_type position, value_type value )
{
if(position<0 || position>=numElts){
throw exception();
}
size_type new_size=numElts+1;
value_type * old=structure;
structure=new value_type[new_size];
numElts=new_size;
for(int i=0;i
structure[i]=old[i];
structure[position]=value;
for(int i=position+1;i
structure[i]=old[i-1];
}
const Sequence::value_type& Sequence::front() const
{
if(empty())
throw exception();
return structure[0];
}
const Sequence::value_type& Sequence::back() const
{
if(empty())
throw exception();
return structure[numElts-1];
}
bool Sequence::empty() const
{
return numElts==0;
}
Sequence::size_type Sequence::size() const
{
return numElts;
}
void Sequence::clear()
{
delete [] structure;
numElts=0;
structure=new value_type[numElts];
}
void Sequence::erase( size_type position, size_type count )
{
if(position<0 || position+count>numElts){
throw exception();
}
size_type new_size=numElts-count;
value_type * old=structure;
structure=new value_type[new_size];
numElts=new_size;
for(int i=0;i
structure[i]=old[i];
for(int i=position;i
structure[i]=old[i+count];
}
}
ostream& Sequence::print( ostream& os )
{
int i=0;
os << “<“;
for(i=0;i
if(structure[i]!=not_defined)
os << structure[i]<<“, “;
else
os << “???, “;
os <”;
return os;
}
ostream& operator<<( ostream& os, Sequence& s )
{
int i=0;
os << “<“;
for(i=0;i
if(s[i]!=not_defined)
os << s[i]<<“, “;
else
os << “???, “;
os <”;
return os;
}