Linked Classes??

This is a discussion on Linked Classes?? within the C++ Programming forums, part of the General Programming Boards category; Alright...this may sound like a dumb question but here it goes. I know how to create linked lists and stuff ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    47

    Unhappy Linked Classes??

    Alright...this may sound like a dumb question but here it goes.
    I know how to create linked lists and stuff with the *NextElement pointer inside. I was wondering if it was possible to do this same thing with classes so that you could have an array of classes that could expand in size??

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yes.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    You mean a linked list of objects?

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    A quick, limited example (just a file I have on hand for answering linked list questions and such)

    Code:
    #define EXIT_SUCCESS 0
    #define EXIT_FAILURE 1
    
    #include <stdio.h>
    using namespace::std;
    
    class llist		
    {
      public:
        int adddata(unsigned char add);
        llist(void);	
        ~llist(void);
        void printlist();
      private:
        unsigned char data;
        llist *next;
    };
    
    int llist::adddata(unsigned char add)
    {
    	llist *current = this;			//this is a pointer to this instance of the class
    	
    	while (current->next)			//move current to the last llist in the list
    		current = current->next;
    	current->next = new llist;		//create a new llist at the end of the list
    	
    	if (current->next)				//memory allocation succeeded
    	{
    		current->next->data=add;	//store add in the new llist
    		return EXIT_SUCCESS;
    	}
    	else							//memory allocation failed
    		return EXIT_FAILURE;	
    }
    
    llist::llist()
    {
    	next=NULL;
    	data = 0;
    }
    
    llist::~llist()
    {
    	if (next)			//use recursion to delete all the llists after this one
    		delete next;
    }
    
    void llist::printlist()
    {
    	llist *current = this;
    
    	while (current)
    	{
    		cout<<current->data<<endl;	//display current node's data
    		current=current->next;		//move to next node
    	}				
    }
    Away.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    47

    Thanks

    Thanks That Helped A Lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists and Classes
    By reversaflex in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2008, 12:25 AM
  2. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  3. Replies: 2
    Last Post: 01-18-2003, 12:32 AM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM

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