Can't get copy constructor to work in Linked List ADT...

Here is my members file:
///////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include "linkedlist.h"
using namespace std;


linkedList::linkedList()
{
start_ptr=NULL;
}
linkedList::linkedList(linkedList const &otherlist)
{
int i=0;
node *temp=start_ptr;
while (i<length())
{

AddNode(valueAtIndex(i));
i++;
temp=temp->next;

}

}

int linkedList::valueAtIndex(int num1)const
{
node* temp1;
temp1=start_ptr;
for(int i=0;i<=num1;i++)
{
temp1=temp1->next;
}
return temp1->data;
}


void linkedList::AddNode(int num)
{
node *temp;

if( NULL == start_ptr)
{
start_ptr = new node;
start_ptr->data = num;
start_ptr->next = NULL;
tail = start_ptr;
}


while(tail->next!=NULL)
{
tail = tail->next;
}

temp = new node;
temp->data = num;
tail->next = temp;
temp->next = NULL;
}


void linkedList::divideAt(linkedList &secondlist, const int &item)
{
linkedList thirdList;

node *temp=start_ptr;

while(temp->data!=item && temp->next!=NULL)
{
temp=temp->next;
}
while(temp->next!=NULL)
{
secondlist.AddNode(temp->data);
temp=temp->next;

}
}


int linkedList::length()const
{
node *temp;
temp=start_ptr;
if(NULL == temp)
return 0;
else
{
int node_count=0;
while(temp->next!=NULL)
{
temp=temp->next;
node_count++;
}
return node_count;
}

}


void linkedList::PrintNodes()
{
ofstream myofstream("ListOut.txt",fstream::in | fstream::out | fstream::app);
node *temp2;
temp2 = start_ptr;
while(temp2->next!=NULL)
{

temp2=temp2->next;
myofstream<<temp2->data<<" ";
}
myofstream<<endl;
myofstream.close();
}



void linkedList::DeleteNode(int item)
{
node *temp,*temp2;
temp = start_ptr;
int count=0;

if(temp->data==item)
{
start_ptr = temp->next;
delete temp;
return;
}

temp2=temp;
while(temp!=NULL)
{

if (temp->data==item)
{
temp2->next=temp->next;
delete temp;
return;

}
else
{ temp2=temp;

temp=temp->next;
}

}
}
void linkedList::addTo( const linkedList &secondList, linkedList &thirdList)
{

/*for(int z=0;z <4;z++)
{
thirdList.DeleteNode(thirdList.valueAtIndex(z));
cout<<thirdList.valueAtIndex(z)<<endl;
}
*/


for(int i=0;i <length();i++)
{
thirdList.AddNode(valueAtIndex(i)+secondList.valueAtIndex(i));
}
system("PAUSE");
}
///////////////////////////////////////////////////////////////////////////////
Here is my class file:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

class linkedList{

struct node
{
int data;
node* next;

}*tail,*start_ptr;

public:

linkedList(linkedList const &otherlist);
linkedList();
void AddNode(int num);
void DeleteNode(int item1);
int length()const;
void PrintNodes();
void divideAt(linkedList &secondlist,const int &item);
void addTo( const linkedList &secondList, linkedList &thirdList);
int valueAtIndex(int indx)const;

};

#endif

Here is the file where I try to invoke the copy constructor for a test
////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include "linkedlist.h"
using namespace std;
ofstream myofstream("ListOut.txt");
void main()
{

ifstream myifstream("List1.txt");
ifstream myifstream2("List2.txt");
ifstream myifstream3("List3.txt");

linkedList list1;
linkedList list2;
linkedList list3;

int num=0;
myifstream>> num;
while(myifstream)
{

list1.AddNode(num);
myifstream>> num;


}
int num2=0;
myifstream2>> num2;
while(myifstream2)
{
list2.AddNode(num2);
myifstream2>> num2;
}
int num3=0;
myifstream3>> num3;
while(myifstream3)
{

list3.AddNode(num3);
myifstream3>> num3;


}
linkedList list4(list1);
}

I am trying to test the copy constructor by this call:
linkedList list4(list1);

I'm not even sure if this is right....but that's why I'm asking here.
When it breaks during debugging, execution is stopped a loop in my length function. I'm confused....Any help would be much appreciated

~frustrated




Can you please edit your post and use the code format tag to format your code. There's lots of it and it's unreadable as presented.

I don't see a copy constuctor.
Last edited on
In the copy constructor, you never use the otherlist argument.
I guess it should look like that (not tested though):

1
2
3
4
5
6
7
8
9
linkedList::linkedList(linkedList const &otherlist)
{
    const node* temp=otherlist.start_ptr;
    while (temp != NULL)
    {
        AddNode(temp->value);
        temp=temp->next;
    }
}

Last edited on
Topic archived. No new replies allowed.