0

リンク リストのコピー コンストラクターを c++ で実装しようとしています。最初にリンクされたリストの要素を配列にコピーしてから、を作成することは可能array[i] = listでしょうか? (listがパラメータとして取られます。)

template <typename Type>
Single_list<Type>::Single_list( Single_list<Type> const &list ):
list_head( 0 ),
list_tail( 0 ),
node_count( 0 ) {

    // enter your implementation here
    for(int i = 0; i < node_count; i++){
    *tmp_array = new array[node_count];
    tmp_array[i] = list;


}

申し訳ありませんが、私はコーディングが初めてです..

ここにあります:

template <typename Type>
class Single_list {
    private:
        Single_node<Type> *list_head;
        Single_node<Type> *list_tail;
        int node_count;


    public:
        Single_list();
        Single_list( Single_list const & );
        ~Single_list();

        // Accessors

        int size() const;
        bool empty() const;

        Type front() const;
        Type back() const;

        Single_node<Type> *head() const;
        Single_node<Type> *tail() const;

        int count( Type const & ) const;

        // Mutators

        void swap( Single_list & );
        Single_list &operator = ( Single_list const & );

        void push_front( Type const & );
        void push_back( Type const & );

        Type pop_front();

        int erase( Type const & );

    // Friends

    template <typename T>
    friend std::ostream &operator << (std::ostream &, Single_list<T> const&);
};

template <typename Type>
Single_list<Type>::Single_list():
list_head( 0 ),
list_tail( 0 ),
node_count( 0 ) {
    // empty constructor
}

template <typename Type>
Single_list<Type>::Single_list( Single_list<Type> const &list ):
list_head( 0 ),
list_tail( 0 ),
node_count( 0 ) {

    Single_List<Type> *tmp_ptr = 0;

    for(tmp_ptr = head(); tmp_ptr !== 0; tmp_ptr->next()){
         //my copy constructor so far..
        tmp_ptr->retrieve() = list;
    }
}

template <typename Type>
Single_list<Type>::~Single_list() {

    Single_list<Type> *tmp_ptr = 0; //temp pointer initialized

    for(tmp_ptr = head(); tmp_ptr !==0; tmp_ptr-next()){
        //iterate through single_list, then delete
        delete tmp_ptr;
    }
}

ああ、オペレーター:

template <typename Type>
Single_list<Type> &Single_list<Type>::operator = (Single_list<Type> const &rhs) {
    Single_list<Type> copy( rhs );

    swap( copy );

    return *this;
4

1 に答える 1

0

コーディングスキルを開発するためにこれを行っている場合は、std::list<>. それ以外の場合は、そのまま使用できます。

移動コンストラクターと移動代入演算子=は、標準テンプレート ライブラリ (STL) の C++0x と C++11 で既に実装されています。適切なヘッダー ( ) をインクルードして使用するだけで、(互換性のあるコンパイラで) それらを利用できます#include <list>

以下に簡単な例を示します。

#include <list>

using namespace std;

int main()
{
    list<int> my_list;
    for (int i = 0; i < 1000000; ++i) // make a big list
        my_list.push_back(i);

    list<int> my_copied_list = my_list;       // copy the list using the conventional assignment operator (slow)
    list<int> my_moved_list  = move(my_list); // move the list using the move assignment operator (fast)
}
于 2013-10-18T03:42:37.370 に答える