Given below is the code for the question. Since the test files (mentioned in question) are missing,
I wrote a small test for the implementation. Output is shown at the end. Please don't forget to
rate the answer if it helped. Thank you.
sequence2.cpp
#include "sequence2.h"
using namespace CISP430_A2;
sequence::sequence(size_type entry )
{
capacity = entry;
used = 0;
current_index = 0;
data = new value_type[capacity];
}
// COPY CONSTRUCTOR
sequence::sequence(const sequence& entry)
{
data = NULL;
*this = entry;
}
// Library facilities used: cstdlib
// MODIFICATION MEMBER FUNCTIONS
void sequence::start( )
{
current_index = 0;
}
void sequence::advance( )
{
if(is_item())
current_index++;
}
void sequence::insert(const value_type& entry)
{
if(size() == capacity) //check if resizing is needed
{
resize(capacity * 1.1); //increaase by 10%
}
if(is_item() && current_index > 0)
current_index--;
else
current_index = 0;
for(size_type i = size(); i > current_index; i--)
data[i] = data[i-1];
data[current_index] = entry;
used++;
}
void sequence::attach(const value_type& entry)
{
if(size() == capacity) //check if resizing is needed
{
resize(capacity * 1.1); //increaase by 10%
}
if(!is_item())
current_index = used;
else
current_index++;
//make room for new entry by pushing elements after current to right
for(size_type i = size(); i > current_index ; i--)
data[i] = data[i-1];
data[current_index] = entry;
used++;
}
void sequence::remove_current( )
{
if(is_item())
{
for(size_type i = current_index + 1; i < size(); i++)
data[i-1] = data[i];
used--;
}
}
void sequence::resize(size_type new_capacity )
{
if(new_capacity > capacity)
{
value_type *temp = new value_type[new_capacity];
for(int i = 0; i < used; i++)
temp[i] = data[i];
delete []data;
data = temp;
}
}
void sequence::operator =(const sequence &entry)
{
if(data != NULL) delete []data;
capacity = entry.capacity;
used = entry.used;
current_index = entry.current_index;
data = new value_type[capacity];
for(int i = 0; i < used; i++)
data[i] = entry.data[i];
}
// CONSTANT MEMBER FUNCTIONS
sequence::size_type sequence::size( ) const
{
return used ;
}
bool sequence::is_item( ) const
{
return size() != 0 && current_index < used;
}
sequence::value_type sequence::current( ) const
{
return data[current_index];
}
//Destructor
sequence::~sequence()
{
delete []data;
}
output
attach 1 2 3
[1 2 3 ]
advance after print... so should not have current item
is_item() = 0
insert 4 5 6, should appear in front of seq as 6 5 4
print from current
[6 5 4 1 2 3 ]
print from current
[7 5 4 1 2 3 ]
Solution
Given below is the code for the question. Since the test files (mentioned in question) are missing,
I wrote a small test for the implementation. Output is shown at the end. Please don't forget to
rate the answer if it helped. Thank you.
sequence2.cpp
#include "sequence2.h"
using namespace CISP430_A2;
sequence::sequence(size_type entry )
{
capacity = entry;
used = 0;
current_index = 0;
data = new value_type[capacity];
}
// COPY CONSTRUCTOR
sequence::sequence(const sequence& entry)
{
data = NULL;
*this = entry;
}
// Library facilities used: cstdlib
// MODIFICATION MEMBER FUNCTIONS
void sequence::start( )
{
current_index = 0;
}
void sequence::advance( )
{
if(is_item())
current_index++;
}
void sequence::insert(const value_type& entry)
{
if(size() == capacity) //check if resizing is needed
{
resize(capacity * 1.1); //increaase by 10%
}
if(is_item() && current_index > 0)
current_index--;
else
current_index = 0;
for(size_type i = size(); i > current_index; i--)
data[i] = data[i-1];
data[current_index] = entry;
used++;
}
void sequence::attach(const value_type& entry)
{
if(size() == capacity) //check if resizing is needed
{
resize(capacity * 1.1); //increaase by 10%
}
if(!is_item())
current_index = used;
else
current_index++;
//make room for new entry by pushing elements after current to right
for(size_type i = size(); i > current_index ; i--)
data[i] = data[i-1];
data[current_index] = entry;
used++;
}
void sequence::remove_current( )
{
if(is_item())
{
for(size_type i = current_index + 1; i < size(); i++)
data[i-1] = data[i];
used--;
}
}
void sequence::resize(size_type new_capacity )
{
if(new_capacity > capacity)
{
value_type *temp = new value_type[new_capacity];
for(int i = 0; i < used; i++)
temp[i] = data[i];
delete []data;
data = temp;
}
}
void sequence::operator =(const sequence &entry)
{
if(data != NULL) delete []data;
capacity = entry.capacity;
used = entry.used;
current_index = entry.current_index;
data = new value_type[capacity];
for(int i = 0; i < used; i++)
data[i] = entry.data[i];
}
// CONSTANT MEMBER FUNCTIONS
sequence::size_type sequence::size( ) const
{
return used ;
}
bool sequence::is_item( ) const
{
return size() != 0 && current_index < used;
}
sequence::value_type sequence::current( ) const
{
return data[current_index];
}
//Destructor
sequence::~sequence()
{
delete []data;
}
output
attach 1 2 3
[1 2 3 ]
advance after print... so should not have current item
is_item() = 0
insert 4 5 6, should appear in front of seq as 6 5 4
print from current
[6 5 4 1 2 3 ]
print from current
[7 5 4 1 2 3 ]

Given below is the code for the question. Since the test files (ment.pdf

  • 1.
    Given below isthe code for the question. Since the test files (mentioned in question) are missing, I wrote a small test for the implementation. Output is shown at the end. Please don't forget to rate the answer if it helped. Thank you. sequence2.cpp #include "sequence2.h" using namespace CISP430_A2; sequence::sequence(size_type entry ) { capacity = entry; used = 0; current_index = 0; data = new value_type[capacity]; } // COPY CONSTRUCTOR sequence::sequence(const sequence& entry) { data = NULL; *this = entry; } // Library facilities used: cstdlib // MODIFICATION MEMBER FUNCTIONS void sequence::start( ) { current_index = 0; } void sequence::advance( ) { if(is_item()) current_index++; } void sequence::insert(const value_type& entry) { if(size() == capacity) //check if resizing is needed {
  • 2.
    resize(capacity * 1.1);//increaase by 10% } if(is_item() && current_index > 0) current_index--; else current_index = 0; for(size_type i = size(); i > current_index; i--) data[i] = data[i-1]; data[current_index] = entry; used++; } void sequence::attach(const value_type& entry) { if(size() == capacity) //check if resizing is needed { resize(capacity * 1.1); //increaase by 10% } if(!is_item()) current_index = used; else current_index++; //make room for new entry by pushing elements after current to right for(size_type i = size(); i > current_index ; i--) data[i] = data[i-1]; data[current_index] = entry; used++; } void sequence::remove_current( ) {
  • 3.
    if(is_item()) { for(size_type i =current_index + 1; i < size(); i++) data[i-1] = data[i]; used--; } } void sequence::resize(size_type new_capacity ) { if(new_capacity > capacity) { value_type *temp = new value_type[new_capacity]; for(int i = 0; i < used; i++) temp[i] = data[i]; delete []data; data = temp; } } void sequence::operator =(const sequence &entry) { if(data != NULL) delete []data; capacity = entry.capacity; used = entry.used; current_index = entry.current_index; data = new value_type[capacity]; for(int i = 0; i < used; i++) data[i] = entry.data[i]; } // CONSTANT MEMBER FUNCTIONS sequence::size_type sequence::size( ) const { return used ; } bool sequence::is_item( ) const { return size() != 0 && current_index < used;
  • 4.
    } sequence::value_type sequence::current( )const { return data[current_index]; } //Destructor sequence::~sequence() { delete []data; } output attach 1 2 3 [1 2 3 ] advance after print... so should not have current item is_item() = 0 insert 4 5 6, should appear in front of seq as 6 5 4 print from current [6 5 4 1 2 3 ] print from current [7 5 4 1 2 3 ] Solution Given below is the code for the question. Since the test files (mentioned in question) are missing, I wrote a small test for the implementation. Output is shown at the end. Please don't forget to rate the answer if it helped. Thank you. sequence2.cpp #include "sequence2.h" using namespace CISP430_A2; sequence::sequence(size_type entry ) { capacity = entry; used = 0; current_index = 0;
  • 5.
    data = newvalue_type[capacity]; } // COPY CONSTRUCTOR sequence::sequence(const sequence& entry) { data = NULL; *this = entry; } // Library facilities used: cstdlib // MODIFICATION MEMBER FUNCTIONS void sequence::start( ) { current_index = 0; } void sequence::advance( ) { if(is_item()) current_index++; } void sequence::insert(const value_type& entry) { if(size() == capacity) //check if resizing is needed { resize(capacity * 1.1); //increaase by 10% } if(is_item() && current_index > 0) current_index--; else current_index = 0; for(size_type i = size(); i > current_index; i--) data[i] = data[i-1]; data[current_index] = entry; used++;
  • 6.
    } void sequence::attach(const value_type&entry) { if(size() == capacity) //check if resizing is needed { resize(capacity * 1.1); //increaase by 10% } if(!is_item()) current_index = used; else current_index++; //make room for new entry by pushing elements after current to right for(size_type i = size(); i > current_index ; i--) data[i] = data[i-1]; data[current_index] = entry; used++; } void sequence::remove_current( ) { if(is_item()) { for(size_type i = current_index + 1; i < size(); i++) data[i-1] = data[i]; used--; } } void sequence::resize(size_type new_capacity ) { if(new_capacity > capacity) { value_type *temp = new value_type[new_capacity]; for(int i = 0; i < used; i++)
  • 7.
    temp[i] = data[i]; delete[]data; data = temp; } } void sequence::operator =(const sequence &entry) { if(data != NULL) delete []data; capacity = entry.capacity; used = entry.used; current_index = entry.current_index; data = new value_type[capacity]; for(int i = 0; i < used; i++) data[i] = entry.data[i]; } // CONSTANT MEMBER FUNCTIONS sequence::size_type sequence::size( ) const { return used ; } bool sequence::is_item( ) const { return size() != 0 && current_index < used; } sequence::value_type sequence::current( ) const { return data[current_index]; } //Destructor sequence::~sequence() { delete []data; } output attach 1 2 3
  • 8.
    [1 2 3] advance after print... so should not have current item is_item() = 0 insert 4 5 6, should appear in front of seq as 6 5 4 print from current [6 5 4 1 2 3 ] print from current [7 5 4 1 2 3 ]