/ Published in: C++

Not working.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include "LinkedList.h" #include "LinkedNode.h" template < class T > LinkedList< T >::LinkedList() { size = 0; } template < class T > void LinkedList< T >::add( T value ) { // first set up our node LinkedNode * node = new LinkedNode( this, firstNode, &value ); if( firstNode == NULL ) { // this node is the first one in our list firstNode = node; } //and our size got increased by one size++; } //unnecessary function? /* set the currentNode pointer to a new node void LinkedList::setCurrentNode( int nodeNumber ) { // start from the first node LinkedNode * node = this->firstNode; // then go through the links until we reach the node we want for( int i = 0; i < nodeNumber; i++ ) { node = node->next(); } // set the node as the current node this->currentNode = node; } */ template < class T > void LinkedList< T >::remove( T value ) { // start at the first node LinkedNode< T > * node = firstNode; // go through the links until we find the value that matches while( node->value() != value ) { node = node->next(); } //link over this node before we remove it node->previous()->setNextNode( node->next() ); //now we can remove this node delete node; node = NULL; //and our size just got decreased by one size--; } template < class T > void LinkedList< T >::removeByIndex( int index ) { //start with the first node LinkedNode< T > * node = firstNode; //go through the nodes until we get to the one we want for( int i = 0; i < index; i++ ) { node = node->next(); } //now we have to link over this node node->previous()->setNextNode( node->next() ); // remove the node delete node; node = NULL; // and our size got decreased by one size--; } template < class T > T LinkedList< T >::get( int index ) { // start with the first node LinkedNode< T > * node = firstNode; // go through the nodes until we get to the one we want for( int i = 0; i < index; i++ ) { node = node->next(); } //return the node's value return *( node->getValue() ); } template < class T > int LinkedList< T >::getSize() { return size; }
Comments
