This is a C++ problem that I have mostly completed with a couple of
areas I'm unsure on. I will be as detailed as possible.
The goal is to implement a class called List that uses an array to
store the items (some value type) of the List. I will now provide the
complete code for the header file, list1.h:
FILE: list1.h
CLASS PROVIDED: List (a container class for a List of value_types,
where each List may have a designated value_type called the current
value_type) const size_t CAPACITY = is the maximum number of
value_types that a List can hold.
TYPEDEF for the List class:
Typedef_______ value_type
List::value_type is the data type of the value_types in the List. It
may be any of the C++ built-in types , or a class with a default
constructor, an assignment operator, and a copy constructor.
CONSTRUCTOR for the List class:
List()
Postcondition: The List has been initialized as an empty
list.
MODIFICATION MEMBER FUNCTIONS for the List class:
Void start()
Postcondition: The first item on the List becomes the
current item (but if the List is empty, then there
is no current item).
Void advance()
Precondition: is_item returns true.
Postcondition: If the currentitem was already the last item
on the List, then there is no longer any
current item. Otherwise, the new current item is the is the
item immediately after the original current
item.
Void insert (const value_type& entry)
Precondition: size() < CAPACITY.
Postcondition: A new copy of entry has been inserted in the
List before the current item. If there was
no current item, then the new entry has been inserted at
the front of the List. In either case, the newly
inserted item is now the current item of the list.
Void attach(const value_type & entry)
Precondition: size() < CAPACITY
Postcondtion: A new copy of entry has been inserted in the
List after the current item. If there was no
current item, then the new entry has been attached to the
end of the list. In either case, the newly
inserted item is now the current item of the list.
Void remove_current()
Precondition: is_item returns true.
Postcondition: The current item has been removed from the
List, and the item after this (if there is
one) is now the current item.
CONSTANT MEMBER FUNCTIONS for the List class:
Size_t size() const
Postcondition: The return value is the number of items in
the list
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 on the List.
VALUE SEMANTICS for the List class:
Assignments and the copy constructor may be used with List
objects.
PRIVATE DATA MEMBERS:
value_type data[capacity];
size_t used;
size_t current_index;
I wont bore you with the entire implementation file. There are just a
few functions that Im struggling with:
ATTACH Im not sure how to do. I have INSERT done correctly, it goes
like this:
void List::insert(const value_type& entry)
{
assert(size()<CAPACITY);
size_t i;
if(!is_item())
current_index=0;
for(i=used; i>current_index; --i)
data[i]=data[i-1];
data[current_index]=entry;
++used;
}
My best guess for attach is as follows:
void List::attach(const value_type& entry)
{
assert(size()<CAPACITY);
size_t i;
if(!is_item)
data[current_index]=entry;
else
{
for(i=used; i>current_index+1;i++)
data[i]=data[i-1];
data[current_index+1]=entry;
current_index++;
}
used++
}
remove_current is the only other one that I have been having a problem
with. Here is what I have so far:
{
assert(is_item());
size_t i;
for(i=current_index+1;i<used;i++)
{
data[i]=data[i-1];
}
--used;
}
Any info is appreciated. If you need more info please let me know. |