Help LinkedList!!

This is a discussion on Help LinkedList!! within the C++ Programming forums, part of the General Programming Boards category; Hey, I need help!! I am working on this program and it has 4 classes:Member, GeneralizedList, sublist and an Integer ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    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.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    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;
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Is this what you were talking about?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something about a linkedlist program..
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 10-17-2007, 01:14 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. linkedlist concept please!
    By SAMSAM in forum C Programming
    Replies: 3
    Last Post: 03-15-2003, 12:50 PM
  4. linkedlist guidance
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 06:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21