how i implement it i am totly confused

The insert and attach member functions. There are two member functions to
add new items to a sequence. One of the functions, called insert, places a new
item before the current item. For example, suppose that we have created the
sequence shown to the right with three items, and that the current item is 8.8. In
this example, we want to add 10.0, immediately before the current item. When
10.0 is inserted before the current item, other items—such as 8.8 and 99.0—will
move down to make room for the new item. After the insertion, the sequence has
the four items shown in the lower box.
If there is no current item, then insert places the new item at the front of the
sequence. In any case, after the insert function returns, the newly inserted item
will be the current item, as specified in this precondition/postcondition contract:
methods are:
void insert(const value_type& entry);
void attach(const value_type& entry);
a) You didn't post what attach function should do.
b) You didn't post class where you want implement these function.
Because I would implement these like:
1
2
3
4
void insert(const value_type& entry) {
    C.push_back(entry);
    std::rotate(C.begin() + current, C.end() - 1, C.end())
}
Last edited on
sorry but i dont understand it ....
#include<iostream>
#include<cstdlib>
using namespace std;
class sequence
{

public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
size_t size_type;
static const size_type capacity = 30;
// CONSTRUCTOR
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:
value_type data[capacity];
size_type used;
size_type current_index;


};
this is class
a) attach function:
void attach(const value_type& entry);
// Precondition: size( ) < CAPACITY.
// 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. In either case, the new item is now the
// current item of the sequence.

1
2
3
4
5
6
7
8
9
void sequence::insert(const value_type& entry)
{
    if (!(size() < capacity))
        //Deal with error
        ;
    current_index = (current_index < size())? current_index : 0;
    data[used++] = entry;
    std::rotate(data + current_index , data + size() - 1, data + size());
}


1
2
3
4
5
6
7
8
9
10
void sequence::attach(const value_type& entry)
{
    if (!(size() < capacity))
        //Deal with error
        ;
    current_index  = (current_index < size())? current_index : size() - 1;
    data[used++] = entry;
    std::rotate(data + current_index + 1, data + size() - 1, data + size());
    ++current_index;
}
Topic archived. No new replies allowed.