Have you programmed in other languages before? If this is your first non-trivial program ever than I want to say that you have a distinguished career in programming ahead of you. It takes most people a long time to grasp the concept of pointers.
Still, there are some problems, but they are pretty common.
First, it's usually easier to distinguish between the list and a an individual item within the list. These are usually represented as different classes. If you think about it, the person using the list class shouldn't have to care about previous and next pointers. They just want to store their ints. In other words, the user wants the main program to look sort of like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int main() {
// make a new intrusive list
intrusive_list mylist;
mylist.insert(5);
std::cout << "The root element has value " << mylist.get_root_value() << std::endl;
// append an element
mylist.append(3);
std::cout << "The root element has value " << myList.get_root_value() << std::endl;
// insert an element
mylist.insert(7);
std::cout << "The root element has value " << myList.get_root_value() << std::endl;
// delete an element
mylist.delete_item();
std::cout << "The root element has value " << myList.get_root_value() << std::endl;
// check size
std::cout << "The list has " << myList.size() << " elements." << std::endl;
myList.print();
}
|
See how much easier this is to use? You can tell someone "it's a list of integers" and they don't have to know much more about it.
The trick to coding it this way is that the things like the insert() and append() methods have to create a list item using new. And that means that they also have to delete it in the list destructor. Have you dealt with this sort of memory management before? It's another tough topic for beginners.
Your insert() and append() methods won't work if you're inserting/appending to the middle of the list. Append() is dropping a new item between "this" and the item after "this." So you need to adjust some more pointers. Insert() does the same. Look at you delete_item method. It has the logic for dealing with all the cases.
Comments on methods should explain what the method does, what the parameters are and what the return value is. You've done a good job here. In a professional setting you wouldn't need to indicate the default constructor, copy constructor and assignment operator - the reader can tell that's what they are from the declaration.
I'd declare the methods in the class and put the definitions below. This way a reader can see what the class does and what methods are available without having to scroll through hundreds of lines of implementation details.
get_root() should return a pointer to the root node, not a copy of it.
Nice job.