-
Help LinkedList!!
Hey,
I need help!!
I am working on this program and it has 4 classes:Member, GeneralizedList, sublist and an Integer class.
The Member class is an Abstract class with all abstract functions. The sublist and Integer class are derived classes of the Member class. the Sublist is also a derived class of the GeneralizedList.
I want to print what is in the linked list but instead I print the memory address, could someone tell me what I am doing wrong. here are the codes
Code:
class Member{
public:
virtual void print(ostream & out)const = 0;
virtual bool search(int a) = 0;
virtual Member * copy() = 0;
virtual ~Member(){}
};
typedef struct Node{
Member * member;
Node * next;
}Node;
#include "GeneralizedList.h"
GeneralizedList::GeneralizedList()
{
size = 0;
first = NULL;
last = NULL;
current = NULL;
}
GeneralizedList::GeneralizedList(const GeneralizedList &g)
{
Node *temp;
size = g.size;
first = NULL;
temp = g.first;
while(temp != NULL)
{
if(first == NULL)
{
first = last = new Node;
first = last = temp;
}
else
{
Node *aNode = new Node;
aNode = temp;
last->next = aNode;
last = last->next;
}
temp = temp->next;
}
}
const GeneralizedList & GeneralizedList:: operator =(const GeneralizedList& g)
{
if(this != &g)
{
delete first;
delete last;
size = g.size;
Node* temp;
temp = g.first;
while(temp!=NULL)
{
if(first == NULL)
{
first = last = new Node;
first = last = temp;
}
else
{
Node *aNode = new Node;
aNode = temp;
last->next = aNode;
last = last->next;
}
temp = temp->next;
}
}
return *this;
}
void GeneralizedList::print(ostream &out)const
{
current = first;
if(current !=NULL){
out<<"(";
while(current!=NULL)
{
if(current->next!= NULL)
out << current->member<<", ";
else
out<<current->member;
current = current->next;
}
out<<")\n";
}
}
GeneralizedList::~GeneralizedList()
{
Node *temp;
while(first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
}
bool GeneralizedList::search(int a)
{
//Integer *p = new Integer(a);
Node *temp = first;
while (temp!=NULL)
{
if(temp->member->search(a))
return true;
temp = temp->next;
}
return false;
}
bool GeneralizedList::isEmpty()
{
return (size == 0);
}
int GeneralizedList::get_size()
{
return size;
}
void GeneralizedList::add(Member *p)
{
if(isEmpty())
{
first = last = new Node;
first->member = last->member = p;
first->next = last->next = NULL;
}
else
{
Node *aNode = new Node;
aNode->member = p;
aNode->next = NULL;
last->next = aNode;
last = last->next;
}
size++;
}
const Member& GeneralizedList::operator [](int index)const
{
int count = 0;
while(index < size)
{
Node *temp = first;
if(count == index)
{
Member *p = temp->member;
return *p;
}
temp = temp->next;
count++;
}
// the index is more than the size
cout<<"index is more than size\n";
//Member garbage = NULL;
}
Member & GeneralizedList::operator [](int index)
{
int count = 0;
while(index < size)
{
Node *temp = first;
if(count == index)
{
Member *p = temp->member;
return *p;
}
temp = temp->next;
count++;
}
// the index is more than the size
cout<<"index is more than size\n";
}
ostream& operator<<(ostream& out, const GeneralizedList& g)
{
g.print(out);
return out;
}
If I can fix the generalizedlist problem I can do the same for the sublist.
-
i do not see that you call your virtual function of the member.
Shouldn't you do it if you want to print the contents of the derived from member clsses?
-
Yeah I did that in the sublist class
Code:
#include "Sublist.h"
Sublist::Sublist(): GeneralizedList(){}
void Sublist::print(ostream& out)const
{
current = first;
if(current !=NULL)
{
out<<"(";
while(current!=NULL)
{
if(current->next!= NULL)
out<<current->member<<", ";
else
out<<current->member;
current = current->next;
}
out<<")\n";
}
}
Sublist * Sublist::copy()
{
//return this;
Sublist *temp = new Sublist(*this);
return temp;
}
bool Sublist::search(int a)
{
Node *temp = first;
while (temp!=NULL)
{
if(temp->member->search(a))
return true;
temp = temp->next;
}
return false;
}
Sublist::~Sublist()
{
Node *temp;
while(first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
}
ostream& operator<<(ostream& out, const Sublist& s)
{
s.print(out);
return out;
}
and this is the integer class
Code:
#include "Integer.h"
Integer::Integer(const int i):number(i){}
Integer::~Integer()
{
cout<<"Destructor called for Integer class"<<endl;
}
bool Integer::search(int a)
{
return (number == a);
}
void Integer::print(ostream& out)const
{
out<<number;
}
ostream& operator<<(ostream& out, const Integer& i)
{
i.print(out);
return out;
}
Integer * Integer::copy()
{
Integer *temp = new Integer(number);
return temp;
}
int Integer::get_number() const
{
return number;
}
-
Is this what you were talking about?
-
I have not look at your code but, if your are printing out different memory addresses instead of there actual value have you probably miss a * somewhere:
Code:
int* number = new int;
*number = 5;
cout << number << endl; //will print out the memory address of number not 5
cout << *number << endl; //This another way will print out 5