Google Answers Logo
View Question
 
Q: Intermediate C++ Troubleshooting ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Intermediate C++ Troubleshooting
Category: Miscellaneous
Asked by: martim07-ga
List Price: $20.00
Posted: 10 Oct 2002 12:57 PDT
Expires: 09 Nov 2002 11:57 PST
Question ID: 74959
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 won’t bore you with the entire implementation file. There are just a
few functions that I’m struggling with:

ATTACH – I’m 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.
Answer  
Subject: Re: Intermediate C++ Troubleshooting
Answered By: davidmaymudes-ga on 10 Oct 2002 17:00 PDT
Rated:5 out of 5 stars
 
Here's how I would do remove_current:

void List::remove_current()
{
    assert(is_item());

    for (size_t i = current_index; i < used - 1; i++) {  // note the
-1 here
         data[i] = data[i+1];  // you had data[i] = data[i-1], which
                               // would move stuff forward, not
backward
    }
    --used;  // you had this part right!
}

whether this is correct depends on how is_item is implemented; if you
use the convention that current_index == used if there is no current
item, then the code above is correct, but if you've done it some other
way, like defining a special constant for "no current item", you'd
need to set current_index to that constant.

attach could be written as:
void List::attach(const value_type& entry) 
{ 
     assert(size()<CAPACITY); 

     if (!is_item()) {
          current_index = used - 1;  // case with no current item is
the same
                                     // as if the last item is the
current one
     }

     for (i = used; i > current_index + 1; i++) {
         data[i] = data[i-1];
     } 
     data[current_index+1] = entry; 
     ++current_index; 
     ++used; 
}

so you had that one basically right, and in fact the way you did it
would happen to work if you're using the convention I mentioned above,
where !is_item is the same as current_index == used.  you could make
the argument that it's a little better to do it the way I did, since
it will work even if is_item is implemented in a different way, but it
isn't a big difference.

Good luck with your continued learning, let me know if you have
further questions!

--David
martim07-ga rated this answer:5 out of 5 stars
Very detailed and helpful. It would take more than $20 for me to wade through this.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy