Skip to end of metadata
Go to start of metadata

In this project, I made a singly linked list in C++. That means each node has data and a next pointer, and the list has a head, tail, and size. To allow generic types to be stored in nodes, I used a template with typename T. There is a class for Node and a class for LinkedList. They both use static polymorphism in that they implement a template, allowing for any type to be used. I test it with ints and chars. Below is a picture of both class declarations; the definitions of most LinkedList methods are implemented later.

Drawbacks of using templates as opposed to, say, overloading, are that they make the program take longer to compile and are harder to debug than if I had, say, made derived classes and/or overloaded functions based on types. But the advantages include minimizing code repetition (and arguably, time to code) and increased type safety.

For my char type linked list, I made a new method that changes the character stored in the node pointer to a random char in order to test the ll_map function. The biggest challenge was figuring out how to solve the errors, which is, as I mentioned, harder with templates; in trying to keep everything in line with the generic type, I had a hard time remembering which pointer pointed where, especially in the functions independent of any class.

The main kinds of polymorphism in C++ are compile time polymorphism (function and operator overloading) and run time polymorphism (function overriding, e.g. redefining parent class member functions).

Extension: memory management

Special thank you to: Ying

Labels
  • No labels