// Description: sequence3.h// CLASS PROVIDED: sequence (part of the namespace main_savitch_5)// This is the header file for the project C++"// TYPEDEFS and MEMBER CONSTANTS for the sequence class:// typedef ____ value_type// sequence::value_type is the data type of the items in the sequence. It// may be any of the C++ built-in types (int, char, etc.), or a class with a// default constructor, an assignment operator, and a copy constructor.//// typedef ____ size_type// sequence::size_type is the data type of any variable that keeps track of// how many items are in a sequence.// CONSTRUCTOR for the sequence class:// sequence( )// Postcondition: The sequence has been initialized as an empty sequence.//copy constructor//copying source into our sequence//destructor// MODIFICATION MEMBER FUNCTIONS for the sequence class:// void start( )// Postcondition: The first item on the sequence becomes the current item// (but if the sequence is empty, then there is no current item).// void advance( )// Precondition: is_item returns true.// Postcondition: If the current item was already the last item in the// sequence, then there is no longer any current item. Otherwise, the new// current item is the item immediately after the original current item.// void insert(const value_type& entry)// Postcondition: A new copy of entry has been inserted in the sequence// before the current item. If there was no current item, then the new entry // has been inserted at the front of the sequence. In either case, the newly// inserted item is now the current item of the sequence.// void attach(const value_type& entry)// Postcondition: A new copy of entry has been inserted in the sequence after// the current item. If there was no current item, then the new entry has // been attached to the end of the sequence. In either case, the newly// inserted item is now the current item of the sequence.// void remove_current( )// Precondition: is_item returns true.// Postcondition: The current item has been removed from the sequence, and// the item after this (if there is one) is now the new current item.// CONSTANT MEMBER FUNCTIONS for the sequence class:// size_type size( ) const// Postcondition: The return value is the number of items in the sequence.//// bool is_item( ) const// Postcondition: A true return value indicates that there is a valid// "current" item that may be retrieved by activating the current// member function (listed below). A false return value indicates that// there is no valid current item.//// value_type current( ) const// Precondition: is_item( ) returns true.// Postcondition: The item returned is the current item in the sequence.//// VALUE SEMANTICS for the sequence class:// Assignments and the copy constructor may be used with sequence objects.#ifndef MAIN_SAVITCH_SEQUENCE_H#define MAIN_SAVITCH_SEQUENCE_H#include <cstdlib> // Provides size_tnamespace main_savitch_5
{class sequence
{public:// TYPEDEFS and MEMBER CONSTANTS:typedefdouble value_type;typedef std::size_t size_type;// CONSTRUCTORS:
sequence();
sequence(const sequence& source);
~sequence();// MODIFICATION MEMBER FUNCTIONS:void start();void advance();void insert(const value_type& entry);void attach(const value_type& entry);void remove_current();// CONSTANT MEMBER FUNCTIONS:
size_type size()const;bool is_item()const;
value_type current()const;private:
node *head_ptr;// List head pointer
node *tail_ptr;// List tail pointer
node *cursor;// Pointer to current
node *precursor;// Pointer to previors
size_type many_nodes;// Number of nodes on the list}#endif

#include <iostream>#include <cstdlib> // Provides size_t#include "node1.h" // Provides node class#include <cassert>#include "sequence3.h" usingnamespace std;namespace main_savitch_5
{// CONSTRUCTOR for the sequence class:
sequence::sequence(){
head_ptr=NULL;
tail_ptr=NULL;
cursor=NULL;
precursor=NULL;
many_nodes=0;}//copy constructor//copying source into our sequence
sequence::sequence(const sequence& source){
head_ptr=NULL;
tail_ptr=NULL;//copys source's head_ptr and tail_ptr into sequence
list_copy(source.head_ptr, head_ptr, tail_ptr);
start();//method to start at the beginning of source (head_ptr)for(node *ptr=source.head_ptr; ptr!=source.cursor; ptr=ptr->link()){
advance();}
many_nodes = source.many_nodes;}//destructor
sequence::~sequence(){
list_clear(head_ptr);
many_nodes=0;}// MODIFICATION MEMBER FUNCTIONS for the sequence class:// void start( )// Postcondition: The first item on the sequence becomes the current item// (but if the sequence is empty, then there is no current item).void sequence::start(){
precursor=NULL;
cursor=head_ptr;}// void advance( )// Precondition: is_item returns true.// Postcondition: If the current item was already the last item in the// sequence, then there is no longer any current item. Otherwise, the new// current item is the item immediately after the original current item.void sequence::advance(){assert(is_item());
precursor=cursor;
cursor= cursor->link();}// void insert(const value_type& entry)// Postcondition: A new copy of entry has been inserted in the sequence// before the current item. If there was no current item, then the new entry // has been inserted at the front of the sequence. In either case, the newly// inserted item is now the current item of the sequence.void sequence::insert(const value_type& entry){if(precursor==NULL||cursor==NULL){
list_head_insert(head_ptr,entry);
precursor=NULL;
cursor=head_ptr;if(tail_ptr==NULL){
tail_ptr=head_ptr;}}else{
list_insert(precursor,entry);
cursor=precursor->link();if(cursor->link()==NULL){
tail_ptr=cursor;}}
many_nodes++;}// void attach(const value_type& entry)// Postcondition: A new copy of entry has been inserted in the sequence after// the current item. If there was no current item, then the new entry has // been attached to the end of the sequence. In either case, the newly// inserted item is now the current item of the sequence.void sequence::attach(const value_type& entry){if(head_ptr==NULL){
list_head_insert(head_ptr,entry);
cursor=head_ptr;
tail_ptr=head_ptr;}elseif(cursor==NULL||cursor==tail_ptr){
list_insert(tail_ptr,entry);
precursor=tail_ptr;
tail_ptr=tail_ptr->link();
cursor=tail_ptr;}else{
list_insert(cursor, entry);
precursor=cursor;
cursor=cursor->link();}
many_nodes++;}void sequence::operator=(const sequence& source){if(this==&source){return;}
list_clear(head_ptr);
many_nodes=0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
start();for(node *ptr=source.head_ptr; ptr!=source.cursor; ptr=ptr->link()){
advance();}
many_nodes = source.many_nodes;}// void remove_current( )// Precondition: is_item returns true.// Postcondition: The current item has been removed from the sequence, and// the item after this (if there is one) is now the new current item.void sequence::remove_current(){assert(is_item());if(precursor==NULL){
cursor=cursor->link();
list_head_remove(head_ptr);}elseif(cursor==tail_ptr){
list_remove(precursor);
tail_ptr=precursor;
cursor=NULL;}else{
list_remove(precursor);
cursor=precursor->link();}
many_nodes--;}// value_type current( ) const// Precondition: is_item( ) returns true.// Postcondition: The item returned is the current item in the sequence.
sequence::value_type sequence::current()const{assert(is_item());return cursor->data();}}