Implement the sequence class from Section 3.2. You may wish to provide some addi
ID: 3820984 • Letter: I
Question
Implement the sequence class from Section 3.2. You may wish to provide some addi- tional useful member functions, such as: a function to add a new item at the front of the sequence; (2) a function to remove the item from the front of the sequence; (3) a function to add a new item at the end of the sequence; (4) a function that makes the last item of the sequence become the cur- rent item; (5) operators for and For the oper- ator, x y contains all the items of x, followed by all the items of y. The statement x all of the items of y to the end of what's already in x
Explanation / Answer
PROGRAM CODE:
sequence1.h
/*
* sequence1.h
*
* Created on: 24-Feb-2017
* Author: kasturi
*/
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib>
namespace main_savitch_3
{
class sequence
{
public:
typedef double value_type;
typedef std::size_t size_type;
size_type CAPACITY;
sequence( );
void insert(const value_type& figure);
void attach(const value_type& figure);
void start( );
void advance( );
void remove_current( );
value_type current( ) const;
size_type size( ) const;
bool is_item( ) const;
sequence operator+(sequence &s1, sequence &s2);
sequence operator+=(sequence &rhs);
void insert_front(const value_type& figure);
void remove_front();
void insert_last(const value_type& figure);
void advance_last();
private:
value_type *data;
size_type used;
size_type current_index;
};
}
#endif
sequence.1cpp
/*
* sequence1.cpp
*
* Created on: 24-Feb-2017
* Author: kasturi
*/
#include "sequence1.h"
#include <cassert>
#include <iostream>
namespace main_savitch_3
{
sequence::sequence()
{
used = 0;
current_index = 0;
CAPACITY = 30;
data = new value_type[CAPACITY];
}
void sequence::start()
{
if (used > 0)
{
current_index = 0;
}
else
{
current_index = used;
}
}
void sequence::advance()
{
if (is_item())
{
current_index++;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
void sequence::insert(const value_type& figure)
{
if (size() < CAPACITY)
{
if (!is_item())
{
start();
}
for (size_t i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = figure;
used++;
}
else
{
std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::attach(const value_type& figure)
{
if (used < CAPACITY)
{
if (is_item())
{
for (size_t i = used; i > current_index + 1; i--)
{
data[i] = data[i-1];
}
data[current_index + 1] = figure;
current_index++;
used++;
}
else
{
data[used] = figure;
current_index = used;
used++;
}
}
else
{
std::cout << "Did not attach, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::remove_current()
{
if (is_item())
{
for (size_t i = current_index; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
sequence::size_type sequence::size() const
{
return used;
}
bool sequence::is_item() const
{
return ((current_index >= 0) && (current_index < used) && (used != 0));
}
sequence::value_type sequence::current() const
{
if (is_item())
{
return data[current_index];
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
void sequence::insert_front(const value_type& figure)
{
if (used < CAPACITY)
{
if (is_item())
{
for (size_t i = used; i > 0; i--)
{
data[i] = data[i-1];
}
data[0] = figure;
current_index = 0;
used++;
}
else
{
data[0] = figure;
current_index = used;
used++;
}
}
else
{
std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::remove_front()
{
if (is_item())
{
for (size_t i = 0; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
void sequence::insert_last(const value_type& figure)
{
if (used < CAPACITY)
{
data[used] = figure;
used++;
}
else
{
std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::advance_last()
{
if (is_item())
{
current_index = used-1;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
sequence sequence::operator +(sequence &s1, sequence &s2)
{
sequence s3;
s1.start();
for(int i=0; i<s1.size(); i++)
{
s3.insert_last(s1.current());
s1.advance();
}
s2.start();
for(int i=0; i<s2.size(); i++)
{
s3.insert_last(s2.current());
s2.advance();
}
}
sequence sequence::operator +=(sequence &rhs)
{
rhs.start();
for(int i=0; i<rhs.size(); i++)
{
insert_last(rhs.current());
rhs.advance();
}
}
}