Thread: LinkedList

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    18

    LinkedList

    For practising , I wrote a linked list inspired from a C++ book. But when I use the function Iterate there is an error message because of 2 undefined variables : itsNext and itsRect from the function getRect in class Rect_Node . If I remove Iterate , the rectangles that I created are correctly displayed .
    Perhaps someone knows why this is happening , here is the code .
    Code:
    #include <iostream>
    using namespace std;
    
    class Rectangles
    {
    public:
    	Rectangles(){};
    	Rectangles(int L): Length(L) {Height = Length / 2;}
    	virtual~Rectangles(){};
    	virtual void Display()const = 0;
    	int getLength()const{return Length;}
    private:
    	int Length;
    	int Height;
    };
    
    void Rectangles::Display()const
    {
    	for(int i = 0; i < Height; i++)
    	{
    		for(int j = 0; j < Length; j++)
    			cout << " * ";
    		
    	cout << "\n";
    	}
    }
    
    class Number_Rect : public Rectangles
    {
    public:
    	Number_Rect(): itsNB(01), Rectangles(12){};
    	Number_Rect(int NB, int L): itsNB(NB), Rectangles(L) {}
        void Display()const
    	{ Rectangles::Display(); cout << " Number = "<< itsNB << endl << endl;}	
    private:
    	int itsNB;
    };
    
    class Char_Rect : public Rectangles
    {
    public:
    	Char_Rect(): itsChar('a'), Rectangles(12){};
    	Char_Rect(char C, int L): itsChar(C), Rectangles(L) {}
        void Display()const
    	{ Rectangles::Display(); cout << " Character = "<< itsChar << endl << endl;}	
    private:
    	char itsChar;
    };
    
    class Rect_Node
    {
    public:
    	Rect_Node(Rectangles* pRectangle): itsRect(pRectangle), itsNext(0) {};
    	~Rect_Node(){};
    	Rectangles* getRect()const{if(itsRect)return itsRect; else return NULL;}
    	void setNext(Rect_Node* Next){itsNext = Next;}
    	Rect_Node* getNext()const{return itsNext;}
    private:
    	Rect_Node* itsNext;
    	Rectangles* itsRect;
    };
    
    class Rect_List
    {
    public:
    	Rect_List(): pHead(0), itsCount(0){};
    	~Rect_List(){delete pHead;}
    	Rectangles* Find(int Position, int Rectangle_Number)const;
    	int getCount()const{return itsCount;}
    	Rectangles* GetFirst()const;
    	void Insert(Rectangles*);
    	void Iterate()const;
    	Rectangles* operator[](int)const;
    private:
    	Rect_Node* pHead;
    	int itsCount;
    };
    
    Rectangles* Rect_List :: GetFirst()const
    {
    	    if(pHead)
    			return pHead->getRect();
    		else return NULL;
    }
    Rectangles* Rect_List :: operator[](int Offset)const
    {
    	    Rect_Node* Temp = pHead;
    	    if (!pHead)
    			return NULL;
    		if (Offset > itsCount)
    			return NULL;
    		for(int i = 0; i < Offset; i++)
    			Temp = Temp->getNext();
    		return Temp->getRect();
    }
    
    
    Rectangles* Rect_List :: Find(int Position, int Rectangle_Number)const
    {
    		Rect_Node* Temp = 0;
    		for(Temp = pHead, Position = 0; Temp != NULL; Temp->getNext(), Position++)
    		{ 
    			if (Temp->getRect()->getLength() == Rectangle_Number)
    				break;
    		}
    		if(Temp == NULL)
    			return NULL;
    		else
    			return Temp->getRect();
    	
    }
    
    void Rect_List :: Iterate()const
    {
           Rect_Node* Temp = pHead;
    		
    		 /*do
    		{
    		    Temp->getRect()->Display();
    		}while (Temp = Temp->getNext());*/
            
    		for( int i = 0; i < itsCount; i++ )
    		{
    		Temp->getRect()->Display();
    		Temp = Temp->getNext();
    		cout << " debug " << i << endl;
    		}
    
    		cout << endl << itsCount << endl;
    }
    void Rect_List :: Insert(Rectangles* pRectangle)
    {
    	    int NBL = 10;
    		NBL = pRectangle->getLength();
    		int testNB = 10;
    		Rect_Node* newRect = new Rect_Node(pRectangle); 
    		Rect_Node* Temp = 0;
    		Rect_Node* Temp1 = 0;
    		itsCount++;
    		Temp = pHead;
    		if(!pHead)
    		{
    			pHead = newRect;
    			newRect->setNext(0);
    			pHead->getRect()->Display();
    			return;
    		}
    		if(pHead->getRect()->getLength() > NBL)
    		{
    			newRect->setNext(pHead);
    			pHead = Temp;
    			pHead->setNext(0);
                newRect->getRect()->Display();
    			return;
    		}
    		for(;;)
    		{
    			if(!Temp->getNext())
    			{
    				pHead->setNext(newRect);
                    newRect->setNext(0);
    				newRect->getRect()->Display();
    			    return;
    		    }
    		    Temp1 = Temp->getNext();
    			testNB = Temp1->getRect()->getLength();
    			if( testNB > NBL )
    			{
    				Temp->setNext(newRect);
    				newRect->setNext(Temp1);
    				Temp1->setNext(0);
    				newRect->getRect()->Display();
    				return;
    			}
    			Temp = Temp1;
    		}		
    }
    
    
    int main()
    {
    	Rect_List r1;
    	
    	Rectangles* pRectangle = 0;
    	pRectangle = new Number_Rect(1, 2);
    	r1.Insert(pRectangle);Rectangles* pRectangle2 = 0;
    	pRectangle2 = new Number_Rect(2, 8);
    	r1.Insert(pRectangle2);Rectangles* pRectangle3 = 0;
    	pRectangle3 = new Number_Rect(3, 6);
    	r1.Insert(pRectangle3);Rectangles* pRectangle4 = 0;
    	pRectangle4 = new Number_Rect(4, 10);
    	r1.Insert(pRectangle4);
    	cout << endl;
    	r1.Iterate();
    
    	/*int RectLength;
    	int value1;
    	char value2;
    	int choice = 99;
    	while(choice != 0)
    	{
    		cout << " (0) Quit, (1) Number_Rect, (2) Character_Rect :";
    		cin >> choice;
    		if (choice != 0)
    		{
    			cout << " \nLength? : ";
    		    cin >> RectLength;
    			if (choice == 1)
    			{
    				cout << " \nRect Number?";
    			    cin >> value1;
    				pRectangle = new Number_Rect(value1, RectLength);
    			}
    			else
    			{
    				cout << " \nRect Character?";
    			    cin >> value2;
    				pRectangle = new Char_Rect(value2, RectLength);
    			}
    		r1.Insert(pRectangle);
    		(r1.GetFirst())->Display();
    		cout << " itsCount = " << r1.getCount()<<endl;
    		}// end if
    	}// end while
    
    	r1.Iterate();
    	(r1.GetFirst())->Display();*/
    	
    
    	return 0;
    }
    Thanks .

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Iīll love to help you but you post waaaaayy to much code. Isīs all confusing. Post only relevant code such as.

    • fuction iterate

    • Declaration/defination class Rect_Node

    • maybe what code line you think your program crash



    When you post your entire code no-one is gonna help you with a +200 lines of code.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

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, 01:50 PM
  4. linkedlist guidance
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 06:32 AM