View the step-by-step solution to:

Question

Hello,i have troubles with my cmpt assignmentli hardly understand what is being asked and how to implement it,so

ill start from the beginning,the part where i have to implement the program that simply visits VisitALL() ,what does it do?i dont get the way it visits,could u please explain?And in the Vector class i dont understand the concept of capacity and size,isnt it the same thing?Please help!

Thank you


Here is the supplementary code for the assignment1

FILE1

#ifndef VECTOR_H

#define VECTOR_H


#include <algorithm>

#include <iostream>

#include <stdexcept>

#include "dsexceptions.h"


template <typename Object>

class Vector

{

  public:

    explicit Vector( int initSize = 0 )

      : theSize{ initSize }, theCapacity{ initSize + SPARE_CAPACITY }

      { objects = new Object[ theCapacity ]; }

     

    Vector( const Vector & rhs )

      : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr }

    {

        objects = new Object[ theCapacity ];

        for( int k = 0; k < theSize; ++k )

            objects[ k ] = rhs.objects[ k ];

    }

   

    Vector & operator= ( const Vector & rhs )

    {

        Vector copy = rhs;

        std::swap( *this, copy );

        return *this;

    }

   

    ~Vector( )

      { delete [ ] objects; }


    Vector( Vector && rhs )

      : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ rhs.objects }

    {

        rhs.objects = nullptr;

        rhs.theSize = 0;

        rhs.theCapacity = 0;

    }

  

    Vector & operator= ( Vector && rhs )

    {   

        std::swap( theSize, rhs.theSize );

        std::swap( theCapacity, rhs.theCapacity );

        std::swap( objects, rhs.objects );

       

        return *this;

    }

   

    bool empty( ) const

      { return size( ) == 0; }

    int size( ) const

      { return theSize; }

    int capacity( ) const

      { return theCapacity; }


    Object & operator[]( int index )

    {

                                                     #ifndef NO_CHECK

        if( index < 0 || index >= size( ) )

            throw ArrayIndexOutOfBoundsException{ };

                                                     #endif

        return objects[ index ];

    }


    const Object & operator[]( int index ) const

    {

                                                     #ifndef NO_CHECK

        if( index < 0 || index >= size( ) )

            throw ArrayIndexOutOfBoundsException{ };

                                                     #endif

        return objects[ index ];

    }


    void resize( int newSize )

    {

        if( newSize > theCapacity )

            reserve( newSize * 2 );

        theSize = newSize;

    }


    void reserve( int newCapacity )

    {

        if( newCapacity < theSize )

            return;


        Object *newArray = new Object[ newCapacity ];

        for( int k = 0; k < theSize; ++k )

            newArray[ k ] = std::move( objects[ k ] );


        theCapacity = newCapacity;

        std::swap( objects, newArray );

        delete [ ] newArray;

    }


      // Stacky stuff

    void push_back( const Object & x )

    {

        if( theSize == theCapacity )

            reserve( 2 * theCapacity + 1 );

        objects[ theSize++ ] = x;

    }

      // Stacky stuff

    void push_back( Object && x )

    {

        if( theSize == theCapacity )

            reserve( 2 * theCapacity + 1 );

        objects[ theSize++ ] = std::move( x );

    }


    void pop_back( )

    {

        if( empty( ) )

            throw UnderflowException{ };

        --theSize;

    }


    const Object & back ( ) const

    {

        if( empty( ) )

            throw UnderflowException{ };

        return objects[ theSize - 1 ];

    }


      // Iterator stuff: not bounds checked

    typedef Object * iterator;

    typedef const Object * const_iterator;


    iterator begin( )

      { return &objects[ 0 ]; }

    const_iterator begin( ) const

      { return &objects[ 0 ]; }

    iterator end( )

      { return &objects[ size( ) ]; }

    const_iterator end( ) const

      { return &objects[ size( ) ]; }


    static const int SPARE_CAPACITY = 2;


  private:

    int theSize;

    int theCapacity;

    Object * objects;

};


#endif

File 2


#ifndef LIST_H

#define LIST_H


#include <algorithm>

using namespace std;


template <typename Object>

class List

{

  private:   

    // The basic doubly linked list node.

    // Nested inside of List, can be public

    // because the Node is itself private

    struct Node

    {

        Object  data;

        Node   *prev;

        Node   *next;


        Node( const Object & d = Object{ }, Node * p = nullptr, Node * n = nullptr )

          : data{ d }, prev{ p }, next{ n } { }

       

        Node( Object && d, Node * p = nullptr, Node * n = nullptr )

          : data{ std::move( d ) }, prev{ p }, next{ n } { }

    };


  public:

    class const_iterator

    {

      public:

 

        // Public constructor for const_iterator.

        const_iterator( ) : current{ nullptr }

          { }


        // Return the object stored at the current position.

        // For const_iterator, this is an accessor with a

        // const reference return type.

        const Object & operator* ( ) const

          { return retrieve( ); }

       

        const_iterator & operator++ ( )

        {

            current = current->next;

            return *this;

        }


        const_iterator operator++ ( int )

        {

            const_iterator old = *this;

            ++( *this );

            return old;

        }


        const_iterator & operator-- ( )

        {

            current = current->prev;

            return *this;

        }


        const_iterator operator-- ( int )

        {

            const_iterator old = *this;

            --( *this );

            return old;

        }

           

        bool operator== ( const const_iterator & rhs ) const

          { return current == rhs.current; }


        bool operator!= ( const const_iterator & rhs ) const

          { return !( *this == rhs ); }


      protected:

        Node *current;


        // Protected helper in const_iterator that returns the object

        // stored at the current position. Can be called by all

        // three versions of operator* without any type conversions.

        Object & retrieve( ) const

          { return current->data; }


        // Protected constructor for const_iterator.

        // Expects a pointer that represents the current position.

        const_iterator( Node *p ) :  current{ p }

          { }

       

        friend class List<Object>;

    };


    class iterator : public const_iterator

    {

      public:


        // Public constructor for iterator.

        // Calls the base-class constructor.

        // Must be provided because the private constructor

        // is written; otherwise zero-parameter constructor

        // would be disabled.

        iterator( )

          { }


        Object & operator* ( )

          { return const_iterator::retrieve( ); }


        // Return the object stored at the current position.

        // For iterator, there is an accessor with a

        // const reference return type and a mutator with

        // a reference return type. The accessor is shown first.

        const Object & operator* ( ) const

          { return const_iterator::operator*( ); }

       

        iterator & operator++ ( )

        {

            this->current = this->current->next;

            return *this;

        }


        iterator operator++ ( int )

        {

            iterator old = *this;

            ++( *this );

            return old;

        }


        iterator & operator-- ( )

        {

            this->current = this->current->prev;

            return *this;

        }


        iterator operator-- ( int )

        {

            iterator old = *this;

            --( *this );

            return old;

        }


      protected:

        // Protected constructor for iterator.

        // Expects the current position.

        iterator( Node *p ) : const_iterator{ p }

          { }


        friend class List<Object>;

    };


  public:

    List( )

      { init( ); }


    ~List( )

    {

        clear( );

        delete head;

        delete tail;

    }


    List( const List & rhs )

    {

        init( );

        for( auto & x : rhs )

            push_back( x );

    }


    List & operator= ( const List & rhs )

    {

        List copy = rhs;

        std::swap( *this, copy );

        return *this;

    }


   

    List( List && rhs )

      : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail }

    {

        rhs.theSize = 0;

        rhs.head = nullptr;

        rhs.tail = nullptr;

    }

  

    List & operator= ( List && rhs )

    {   

        std::swap( theSize, rhs.theSize );

        std::swap( head, rhs.head );

        std::swap( tail, rhs.tail );

       

        return *this;

    }

   

    // Return iterator representing beginning of list.

    // Mutator version is first, then accessor version.

    iterator begin( )

      { return iterator( head->next ); }


    const_iterator begin( ) const

      { return const_iterator( head->next ); }


    // Return iterator representing endmarker of list.

    // Mutator version is first, then accessor version.

    iterator end( )

      { return iterator( tail ); }


    const_iterator end( ) const

      { return const_iterator( tail ); }


    // Return number of elements currently in the list.

    int size( ) const

      { return theSize; }


    // Return true if the list is empty, false otherwise.

    bool empty( ) const

      { return size( ) == 0; }


    void clear( )

    {

        while( !empty( ) )

            pop_front( );

    }

    // front, back, push_front, push_back, pop_front, and pop_back

    // are the basic double-ended queue operations.

    Object & front( )

      { return *begin( ); }


    const Object & front( ) const

      { return *begin( ); }


    Object & back( )

      { return *--end( ); }


    const Object & back( ) const

      { return *--end( ); }


    void push_front( const Object & x )

      { insert( begin( ), x ); }


    void push_back( const Object & x )

      { insert( end( ), x ); }


    void push_front( Object && x )

      { insert( begin( ), std::move( x ) ); }


    void push_back( Object && x )

      { insert( end( ), std::move( x ) ); }


    void pop_front( )

      { erase( begin( ) ); }


    void pop_back( )

      { erase( --end( ) ); }


    // Insert x before itr.

    iterator insert( iterator itr, const Object & x )

    {

        Node *p = itr.current;

        ++theSize;

        return iterator( p->prev = p->prev->next = new Node{ x, p->prev, p } );

    }


    // Insert x before itr.

    iterator insert( iterator itr, Object && x )

    {

        Node *p = itr.current;

        ++theSize;

        return iterator( p->prev = p->prev->next = new Node{ std::move( x ), p->prev, p } );

    }

   

    // Erase item at itr.

    iterator erase( iterator itr )

    {

        Node *p = itr.current;

        iterator retVal( p->next );

        p->prev->next = p->next;

        p->next->prev = p->prev;

        delete p;

        --theSize;


        return retVal;

    }


    iterator erase( iterator from, iterator to )

    {

        for( iterator itr = from; itr != to; )

            itr = erase( itr );


        return to;

    }


  private:

    int   theSize;

    Node *head;

    Node *tail;


    void init( )

    {

        theSize = 0;

        head = new Node;

        tail = new Node;

        head->next = tail;

        tail->prev = head;

    }

};


#endif


Here is the Question,i wasn't able to attach it

Overview

For this assignment, you are two write two small programs that compare the times for some basic operations on lists using the Vector and List classes provided by the textbook author, and use them to carry out some simple experiments. To carry out the timing, you will also need to make some small modifications to the Vector and List implementations.


Part 1

For the first timing program, you will need to add an implementation of a new operation visitAll() to the implementations in Vector.h and List.h. This operation simply "visits" each element in the list. Then,  program (call it's file part1.cpp) that constructs a list of values in two different containers: one in a Vector, and the other (with the same number of elements) in a List. Your program should construct each list by adding elements one-by-one using push_back. It should also record the time it takes to construct each list, and then the time it takes to execute visitAll() for each list. Finally, the program should report these times.

Here are some details (some suggestions, some requirements):

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 Computer Science tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors