リンク リストのコピー コンストラクターを 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;