Skip to end of metadata
Go to start of metadata

C++ is my extension language, so these are all extensions:

  1.  First, I wrote a LinkedList implementation that is very similar to the C implementation, except that it uses templates instead of void* pointers to make it generic.  For example, 

         The T in the template can be used similar to a generic type in Java.  When the class or function is called, the type is put in brackets:


        This makes this implementation superior to the C implementation because the item in the node is a T type instead of a pointer.  Therefore, there are fewer pointers to keep track of and the functions that are passed into the LinkedList functions do not need to use pointers but can also take in T objects.  It also means that there is less pointers that need to be freed, so there is a smaller chance of memory leaks.  This does result in the problem that for the ll_map function, since the pointer is not changed the actual value needs to, so the mapping function needs to return a T object the over writes the old one. Also, you cannot pass a template variable into a function that is a parameter, so mapfunc, the function used in ll_map must still take in a void* and return a void* that is then converted to a T object in the ll_map function.  This is a complication that is made up for in the fact that the ll_clear function no longer needs a freefunc to free the item in the node. 

     Thus, in this case the functions are polymorphic because the T can become anything.  It is important to note that a downside to this is that the LinkedList needs to be all of one type because it is set at the beginning.  It cannot have chars and ints, it can only have one or the other.  Therefore, there are pros and cons to using templates, but overall, I think it is superior.

Here is the output of this implementation:

  1. The second extension that I completed was to use the C++ template to create another generic implementation of a LinkedList in C++.  In all of the implementations so far, I have a Node and LinkedList data structure of some kind and then functions that modify them.  In this implementation, I made a LinkedList class that uses a template to be generic, and all of the functions are member functions of the class.  The benefit of this approach is that because the class uses the template, none of the member functions need to.  This makes the coding easier.  Also, since they can directly modify they fields of the class, there is no need to pass the linked list into all of the functions and then access the fields through the linked list.  This eliminates a step.  It makes it very similar to a generic Java class.  

A downside of this implementation is that yet again, the entire Linked List needs to be the same type.  When the Linked List is declared, its type is declared with it, and it cannot be modified after.  

Here is the output of running this extension:



  • No labels