GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Oct-2004, 07:55
wc3promet wc3promet is offline
New Member
 
Join Date: Oct 2004
Posts: 7
wc3promet is on a distinguished road
Question

Circular Linked Queue Copy Constructor and Assignment Operator Not Working?


Circular Linked Queue Copy Constructor and Assignment Operator Not Working?

Testing out Deep Copying... It ends up that no copying was done.

1st Scenario
CPP / C++ / C Code:
queue q1,q2
.
.
.
q1=q2;

2nd Scenario
CPP / C++ / C Code:
queue *q1=NULL;
queue *q2=NULL;
q1 = new queue;
q2 = new queue;
.
.
.
*q2= *q1;

Copy Constructor #1
CPP / C++ / C Code:
queue::queue(const queue &rhs)
{ 
 
	if(queueIsEmpty()) 
   	{ 
		   	tail=NULL; 
   	}
   
	else
   	{
    		// MAKE A NEW TAIL PTR;
    		tail = new node; 
     	tail->element = rhs.tail->element;
     	
    		// MAKE A NEW HEAD PTR;
    		node *head = new node; 
     	head->element = rhs.tail->next->element;
     	
     	// CURRENT POSITION IN RHS LIST
     	node *ptr=rhs.tail->next;
     
     do
     {
      enqueue(ptr->element);
      ptr=ptr->next;
      
     } 
     while(ptr !=rhs.tail);

    } 
}

Overloaded Copy Constructor #2
CPP / C++ / C Code:
queue::queue(const queue &*rhs)
{ 
 
	if(queueIsEmpty()) 
   	{ 
		   	tail=NULL; 
   	}
   
	else
   	{
    		// MAKE A NEW TAIL PTR;
    		tail = new node; 
     	tail->element = rhs.tail->element;
     	
    		// MAKE A NEW HEAD PTR;
    		node *head = new node; 
     	head->element = rhs.tail->next->element;
     	
     	// CURRENT POSITION IN RHS LIST
     	node *ptr=rhs.tail->next;
     
     do
     {
      enqueue(ptr->element);
      ptr=ptr->next;
      
     } 
     while(ptr !=rhs.tail);

    } 
}

Assignment Operator #1, for q1=q2;
CPP / C++ / C Code:
const queue& queue::operator=(const queue &rhs)
{
if(queueIsEmpty()) 
   	{ 
		   	tail=NULL; 
   	}
   
	else
   	{
    		// MAKE A NEW TAIL PTR;
    		tail = new node; 
     	tail->element = rhs.tail->element;
     	
    		// MAKE A NEW HEAD PTR;
    		node *head = new node; 
     	head->element = rhs.tail->next->element;
     	
     	// CURRENT POSITION IN RHS LIST
     	node *ptr=rhs.tail->next;
     
     do
     {
      enqueue(ptr->element);
      ptr=ptr->next;
      
     } 
     while(ptr !=rhs.tail);

    } 
    return *this;
} 

Overloaded Assignment Operator #2, for *q1=*q2;
CPP / C++ / C Code:
const queue& queue::operator=(const queue &*rhs)
{
if(queueIsEmpty()) 
   	{ 
		   	tail=NULL; 
   	}
   
	else
   	{
    		// MAKE A NEW TAIL PTR;
    		tail = new node; 
     	tail->element = rhs.tail->element;
     	
    		// MAKE A NEW HEAD PTR;
    		node *head = new node; 
     	head->element = rhs.tail->next->element;
     	
     	// CURRENT POSITION IN RHS LIST
     	node *ptr=rhs.tail->next;
     
     do
     {
      enqueue(ptr->element);
      ptr=ptr->next;
      
     } 
     while(ptr !=rhs.tail);

    } 
    return *this;
} 
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about linked list and queue Kay Chan C++ Forum 7 02-Sep-2004 09:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.