#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    11

    Linked list fun!


    I am wondering what type of constructor to use for this problem. I have to use two classes. Linklist and linknode will be the class names. the linklist class will contain linknode objects which have a value and two pointers(for doubly linked list). the problem comes in when i have to declare a linklist. What type of paramaters should i use to pass to the constructor if there will be who knows how many linknodes? Shoudl it be an array of linknodes of size x and have the ability to resize? the problem is that i have no idea how many linknodes there will be. Any suggestions?

    Thanks,

    Jonnyfive
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    the list class could take no args, and just set its head/tail poitners to NULL. then each node gets allocated dynamically by a get_node function in the list class. the node class contsructor could perhaps take values for its data and pointers; usually the data members for a node class are public or it should be friends with the list class. hth
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    11
    The problem i'm having is in that i have to declare a linknode in the linklist private variable. A question i have is if i use a constructor in the linknode that takes 0 parameters , and i use as a private member var in linklist,
    Code:
    linknode node;
    will i be able to use the "node" that was instantiated with zero parameters like i would one that was instantiated with the value and next and prev pointers. Is there any way to declare the private member variable in linklist to come with the parameters that i would be able to reset and therefore use?
    Thanks,

    Jonathan Cobb
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    are you saying the members of the node class are private? if so then you need to make the list class a FRIEND class.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    11
    the code goes as such:
    Code:
    class linknode{
    public:
    	int number;
    	linknode * prev;
    	linknode * next;
    	linknode(){};
    	linknode(int, linknode*, linknode*){};
    	~linknode(){};
    
    	linknode operator= (const linknode& right){
    		this->prev = right.prev;
    		this->next = right.next;
    		this->number = right.number;
    	};
    };
    and the linklist class declaration as such
    Code:
    class linklist{
    private:
    	linknode node;   // ***************** <-here
    
    
    public:
    	linklist(){};
    	~linklist(){};
    	linknode* head;
    	linknode* tail;
    	linknode* temp;
    	
    	
    
    	void insert_front(int number){
    		temp= new linknode(number, 0,0);  //instantiates a new linknode with 
    		temp->next = head;
    		cout<< number;			//outputs the right number
    		cout << temp->number;   //outputs the wrong number(junk)
    		
    		temp->prev = 0;
    		head = temp;
    	};

    the linknode node variable in the private member of linklist class
    Thanks,

    Jonathan Cobb
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    what is the purpose of that node? why not make the head/tail pointers the only private members?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    More to the point, why are you going to all this problem instead of using the STL? It will be a lot easier to pull this off as a list or vector.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo