Linked List Template and Binary Search Tree

I am posting this because after all my research I could not find a good example of both a templatized linked list and binary search tree. These are what I was able to come up with hope this helps anyone looking for something like this. The template linked list is very useful.

#ifndef LIST_H#define LIST_H#include <iostream>usingnamespace std;
#include "ListNode.h"template <typename T>
class List //linked list of ListNode objects
{
public:
List();
~List();
void insertNewNode(T); //fucntion used to insert new node in order in the listvoid print(); //prints the contents of the linked list
ListNode<T>* search(T); //searches for a value in the linked list and returns the point to object that contains that valueprivate:
ListNode<T> *startPtr; //stores the pointer of first object in the linked list
ListNode<T> *endPtr; //stored the pointer of the last object in the linked listbool isEmpty(); //utility functions used to see if the list contains no elementsvoid insertBegin(T); //inserts new node before the first node in the listvoid insertEnd(T); //inserts new node after the last node in the list
};
template <typename T>
List<T>::List() //creates list with start and end as NULL
{
startPtr = NULL;
endPtr = NULL;
}
template <typename T>
List<T>::~List()
{
if ( !isEmpty() ) // List is not empty
{
ListNode<T> *currentPtr = startPtr;
ListNode<T> *tempPtr;
while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
}
template <typename T>
bool List<T>::isEmpty()
{
if(startPtr == NULL && endPtr == NULL) //if the start pointer and end pointer are NULL then the list is emptyreturn 1;
elsereturn 0;
}
template <typename T>
void List<T>::insertBegin(T dataIn)
{
if(isEmpty()) //if the list is empty create first element of the list
{
ListNode<T> * newPtr = new ListNode<T>(dataIn); //creates new node
startPtr = newPtr; //start and end pointer are same becuase there is only one object in list
endPtr = newPtr;
}else//if node(s) exist in list insert additional object before the first
{
ListNode<T> * newPtr = new ListNode<T>(dataIn);
newPtr->nextPtr = startPtr; //the next pointer of the new node points to the node that was previously first
startPtr = newPtr; //the pointer for the new node is now the starting node
}
}
template <typename T>
void List<T>::insertEnd(T dataIn)
{
if(isEmpty()) //if the list is empty create first element of the list (same as first case in insert at begin)
{
ListNode<T> * newPtr = new ListNode<T>(dataIn);
startPtr = newPtr;
endPtr = newPtr;
}else//if node(s) exist in the list then insert new node at the end of the list
{
ListNode<T> * newPtr = new ListNode<T>(dataIn);
endPtr->nextPtr = newPtr; //the current last node's next pointer points to the new node
endPtr = newPtr; //the new node is now the last node in the list
}
}
template <typename T>
void List<T>::insertNewNode(T dataIn) //general funtionn to insert new node the proper order in the list
{
if(isEmpty()) //if there is no nodes in the list simply insert at beginning
{
insertBegin(dataIn);
}else//otherwise
{
if(dataIn < startPtr->data) //if the data of the new object is less than than the data of first node in list insert at beginning
{
insertBegin(dataIn);
}
elseif(dataIn >= endPtr->data) //if the data of the new object is greater than than the data of last node in list insert at end
{
insertEnd(dataIn);
}
else//the new node is being inserted in order but not at the beginning or end
{
ListNode<T> * currentPtr = startPtr;
ListNode<T> * newPtr = new ListNode<T>(dataIn); //creates new nodewhile(currentPtr != endPtr) //runs until the end of the list is reached
{
if((newPtr->data < currentPtr->nextPtr->data) && (newPtr->data >= currentPtr->data)) //if the data of the new node is less the data in the next node and greater than the data in the current node, insert after current node
{
ListNode<T> * next = currentPtr->nextPtr;
currentPtr->nextPtr = newPtr; //current nodes next pointer now points to the new node
newPtr->nextPtr = next; //the new nodes next pointer now points the node previously after the current nodebreak;
}
currentPtr = currentPtr->nextPtr; //moves to the next node in the list
}
}
}
}
template <typename T>
void List<T>::print()
{
if(isEmpty())
{
cout << "The list is empty" << endl;
}else
{
ListNode<T> * currentPtr = startPtr;
cout << "The contents of the list is: ";
while(currentPtr != NULL) //prints until the end of the list is reached
{
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr; //moves to next node in list
}
cout << endl;
}
}
template <typename T>
ListNode<T>* List<T>::search(T key) //search functions that searches for node that contains data equal to the key
{
ListNode<T>* nodePtr;
bool found = false;
nodePtr = startPtr;
while((!found) && (nodePtr != NULL)) //runs through list until data is found within a node or end of list is reached
{
if(nodePtr->data == key) //if the node's data equals the key then the node has been found
found = true;
else
nodePtr = nodePtr->nextPtr; //moves to next node in list
}
return nodePtr; //returns pointer to the node that contains data equal to key (NULL if not found)
}
#endif

I don’t want to criticize you, but there is very little reason not to use std::list/std::map/std::set instead of your own implementations of a list and binary search tree. Especially if these implementations are worse.

I would choose doubly linked list for a general list container, as it allows you to insert and remove arbitrary elements in a constant time. Cost of this feature is very low on modern computers, with gigabytes of RAM. Additionally, good list implementations perform insertion and removal of nodes without any conditional statements.

As for the tree, well, it is not a self-balancing tree, what makes it inappropriate for many situations.

As a side note, the only case when I have implemented a list myself in C++ was when I wanted a container that does not possess its elements (i.e. only existing elements can be inserted/removed, without any memory allocation/deallocation operations). This still can be accomplished using std::list, but I thought it is better to reimplement everything.

The reason I posted this is that I had an assignment where I had to design a binary search tree and a linked list and couldn't find anything online as to how to design it yourself. Everywhere I looked they used the STL linked list which is not the purpose of the assignment. I wanted to post this so anybody else in my situation has something to refer to and get the basic idea.

I believe working world and academic world has some mis-match alignment. Developing BST and such are common assignments in Uni computer science course (I did that in my Uni studies too!). But ask those in the working world developers, out of every 100, how many of them need to do that ? My estimate is 95 of them will be busy using available libraries e.g STL to build business specific apps. The remaining 5 will do the ACTUAL BST etc implementation and most likely in R&D sector :)

But ask those in the working world developers, out of every 100, how many of them need to do that ? My estimate is 95 of them will be busy using available libraries e.g STL to build business specific apps.

No kidding...I had that assignment, and basically it consisted of copying the code from the book. >_>

This is strange. C++ STL was created to alleviate the need for hand-written code for commonly needed algorithm and data structures. The fact you need to copy the code from the book indicates somehow your employer prefer to re-invent rather than re-use which goes against business nature as most business-es want solution want fast and quick to earn monies FIRST before their competitors.

This confirm my earlier posting. Working and academic worlds are different, at least in terms of software implementation. Academic world is to teach whereas working world is to deliver the fastest output using available preferably free tools and libraries for you to achieve your objectives.

In R&D, the objectives are slightly different. Some monies are meant to be "burnt" to search for the holy grail solution to some common workplace business problems. In this arena, you really get to implement your linked list, BST etc etc.

But if you take a look at numbers, how many ppl are employed in R&D sector and how many ppl are employed in business sector :P