Thread: Circular Double Linked List - Logic in Operator=

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Exclamation Circular Double Linked List - Logic in Operator=

    Hello folks, I am working ona a circular double linked list and trying to overload the operator= operand but I think my logic is false.

    It takes in a linked list and inserts the data into the current linked list.

    I believe this is not a deep copy

    Code:
    template <class T>
    CList<T>& CList<T>::operator=(const CList<T> &a){
    	if(this!=&a){
    		if(a.curr_){
    			Node<T> *nn_ = a.curr_;
    			do{		
    				nn_ = nn_->next_;
    				insert(nn_->data_);
    			}while(nn_!=a.curr_);
    		}
    	}
    	return *this;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Usually the result of using operator= is that both sides would have the same value. What you are trying to do is append, which is more along the lines of what operator+= does.

    If you want to make it a real operator=, then clear your current list before adding the other elements.

    The only thing I see wrong with that code if it was for operator+= is that I think it skips the insertion of a.curr_. If it was an operator+= I wouldn't check for self-assignment either.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Any tips on how to do this?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure what you mean. Those are the tips on how to do that. Is there something in particular that you'd like more information on?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    How to remove every node of the list.
    I came up with this code but I come up with an unhandled windows32 exception upon runtime.

    Code:
    template <class T>
    bool CList<T>::remove(){
    	
    	if(this->isempty()){
    		return false;
    	}
    	else if(!this->isempty()&&curr_){
    		Node<T> *prev_;
    		Node<T> *next_;
    		
    		if(curr_->next_==curr_&&curr_->prev_==curr_){
    			delete curr_;
    			curr_ = NULL;
    		}
    		else{
    		prev_ = curr_->prev_;
    		next_ = curr_->next_;
    		
    		delete curr_;
    		
    		prev_->next_ = next_;
    		next_->prev_ = prev_;
    
    		curr_ = next_;
    
    		return true;
    		}
    	}
    	else return false;
    }
    It deletes the node pointed at by curr_
    Last edited by INFERNO2K; 09-30-2006 at 10:03 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see anything particularly wrong with that remove function (except that if there is only one node in the list the function exits without returning a value). I assume you have a clear function that keeps calling it while it returns true, which also should be ok.

    You'll have to find out more information on when, where and why the exception occurs. A debugger would be most helpful for that. You can also make a small test program that writes to cerr after each step if you can't use the debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. how do i reverse a circular double linked list
    By kobra_swe in forum C Programming
    Replies: 13
    Last Post: 04-08-2008, 04:20 PM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  4. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM