Thread: Simple list class

  1. #1
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166

    Simple list class

    Okay after finishing the chapter about pointers and memory management, my book asks me to create a simple list class that holds strings, and supports bidirectional iterators. So far, this is what I came up with for the push_back function. I dynamically allocate a string array that is one larger than the the previous, and adds the next string to it.

    I'm not sure if this is how I should be going about the problem however. Can I get the opinions of others to see if I'm on the right track before I dig a hole for myself?

    Also, how do I deallocate str (so I just don't keep creating new arrays), and still be able to keep the values for the list?



    Code:
    class string_list{
    private:
    	string* list;	//points to the array of strings in the list
    public:
    	string_list();
    	void push_back(string&);
    };
    
    
    void string_list::push_back(string s&){
    
    	element_count = element_count + 1;
    
    	string* str = new string[element_count];
    
    	if(element_count == 1){
    
    		str[0] = s;
    		list = str;
    	}
    
    	else{
    
    		::copy(list, list + (element_count - 1), str);
    
    		str[elemen_count - 1] = s;
    
    		list = str;
    
    	}
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, all function prototypes and class definitions should have descriptive names for the arguments, not just types. While allowed, some things like IntelliSense picks names for your arguments from them and just having a function list like "int, int, int" is just confusing. It also helps when reading your class definitions or function prototypes.

    string s&
    Beware that this is not right. It should be
    string& s
    And it should probably be const too
    const string& s

    And as for that
    element_count = element_count + 1;
    You can just do
    element_count++;
    Easy!

    The general idea here is that you allocate new memory to hold all the data if the buffer is not big enough. Then you copy the old data to the new buffer and delete the old. Since list holds the old memory, delete it before assigning the new memory to it.
    And you can practice allocating more memory, not just increase one for every string, because that's slow. You just need to keep track of how much memory you have.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    ack sorry for those really bad mistakes, it's late =(. In general though, I'm on the right track?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, you seem to be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How is this a string list? It looks more like a string vector.

  6. #6
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by dra
    Okay after finishing the chapter about pointers and memory management, my book asks me to create a simple list class that holds strings, and supports bidirectional iterators.
    This seems more like your book is talking about linked lists. A double linked list class, in this case.

    In linked lists you create new elements as needed and link them together using pointers, you don't create a new array and then delete the old one, like you are actually doing.
    Your way may work, but it will be very slow if you want to handle a large amount of data.
    By the way, learning how to make a linked list class is very funny.

    If you are interested, here is some good tutorial:
    http://aaroncox.net/tutorials/miscel...nkedLists.html
    http://richardbowles.tripod.com/cpp/...t/doublist.htm

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. Well, both have their own use, so I suppose the question is: what is the intended use? If it's just an array to hold strings, then perhaps a linked list might be a better alternative.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by carlorfeo View Post
    This seems more like your book is talking about linked lists. A double linked list class, in this case.

    In linked lists you create new elements as needed and link them together using pointers, you don't create a new array and then delete the old one, like you are actually doing.
    Your way may work, but it will be very slow if you want to handle a large amount of data.
    By the way, learning how to make a linked list class is very funny.

    If you are interested, here is some good tutorial:
    http://aaroncox.net/tutorials/miscel...nkedLists.html
    http://richardbowles.tripod.com/cpp/...t/doublist.htm
    Okay, I'll definitely look into it. Just for clarification though, I'm working out of Accelerated C++ (chapter 11). It introduced the concept of pointers and arrays, as well as dynamically creating arrays and copying from one array to another, so the solution I presented seemed to make sense based on the material that has been taught....

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, there's nothing wrong with it. But a linked list might be better for this kind of thing. The other, vector way, is useful for other things, but not a lot of insert/delete since it will have to allocate, delete and move data, while a linked list doesn't have to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Simple list class question
    By fidodidohk in forum C++ Programming
    Replies: 6
    Last Post: 05-01-2007, 10:31 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. link list
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2001, 05:41 AM