0

わかりましたので、リンクされたリストのコピー コンストラクターを作成しようとしています。配列のコンストラクターをコピーする方法は知っていますが、リンクされたリストのコンストラクターは知りません。どうすればこれを行うことができるか、誰かが私にアイデアを教えてくれますか?ありがとう。

class node 
{


public :

    double data;  
    node *next;  /// pointer that points to next elemnt
    node () { next = NULL; data = 0; }
    node (double val ) { next = NULL; data = val; }

private:



};

キュー ヘッダー

class linked_queue
{


public :

    linked_queue() { front = NULL; back = NULL; ctr = 0;  }   /// default constructor
    bool _empty();  
    void _size();   
    void _front();   
    void _back();   
    void _push(double); 
    void pop();  
    void _display();   
    ~linked_queue(); /// destructor
    linked_queue& operator= ( const linked_queue& rhs );
    linked_queue( const linked_queue& other );

private :

    int  ctr; /// counter
    node  *front;  /// front pointer
    node *back;  ///back pointer

};

編集:これは私が思いついたものです

linked_queue::linked_queue( const linked_queue& other ) {

ctr = 0;
front = NULL;
back = NULL;

node *p = other.front;

while ( p != NULL )
{
    _push( p->data);
    p = p->next;
}

}

4

1 に答える 1

0

リストを調べて、同じ値を持つノードの束を割り当て、nextポインターを設定するだけです。最後にfrontbackポインターを とに設定するとctr、完了です。

于 2013-05-03T04:20:36.537 に答える