Return to Snippet

Revision: 9027
at October 17, 2008 22:57 by itsok2kry


Updated Code
#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;
}

Revision: 9026
at October 17, 2008 19:21 by itsok2kry


Updated Code
class LinkedNode;

template < class T >
class LinkedList
{
	private:
		LinkedNode * firstNode; //the first node in our list
		//LinkedNode * currentNode; //the node we're currently operating on
		//void setCurrentNode( int nodeNumber );
		int size;
	public:
		LinkedList();
		void add( T value );
		void remove( T value );
		void remove( int index );
		T get( int index );

};

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 >::remove( 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() );
}

Revision: 9025
at October 17, 2008 17:29 by itsok2kry


Updated Code
class LinkedNode;

template < class T >
class LinkedList
{
	private:
		LinkedNode * firstNode; //the first node in our list
		LinkedNode * currentNode; //the node we're currently operating on
		//void setCurrentNode( int nodeNumber );
		int size = 0;
	public:
		//LinkedList( T * val );
		void add( &T value );
		void remove( &T value );
		void remove( int index );
		T * get( int index );

};

//template < class T >
//LinkedList< T >::LinkedList()
//{
	// todo:
	// set up the first node
	
//}

template < class T >
void LinkedList< T >::add( &T value )
{
	// first set up our node
	LinkedNode * node = new LinkedNode( this, this->firstNode, value );

	if( firstNode == NULL )
	{
		// this node is the first one in our list
		this->firstNode = node;
	}

	// this is the node we are currently working with
	this->currentNode = node;

	//and our size got increased by one
	this->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 = this->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
	this->size--;
}

template < class T >
void LinkedList< T >::remove( int index )
{
	//start with the first node
	LinkedNode< T > * node = this->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
	this->size--;
}

template < class T >
T * LinkedList< T >::get( int index )
{
	// start with the first node
	LinkedNode< T > * node = this->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->value();
}

Revision: 9024
at October 17, 2008 16:37 by itsok2kry


Initial Code
#include "LinkedNode.cpp"

class LinkedNode;

template < class T >
class LinkedList
{
	private:
		LinkedNode * firstNode; //the first node in our list
		LinkedNode * currentNode; //the node we're currently operating on
		//void setCurrentNode( int nodeNumber );
		int size = 0;
	public:
		//LinkedList( T * val );
		void add( &T value );
		void remove( &T value );
		void remove( int index );
		T * get( int index );

};

//template < class T >
//LinkedList< T >::LinkedList()
//{
	// todo:
	// set up the first node
	
//}

template < class T >
void LinkedList< T >::add( &T value )
{
	// first set up our node
	LinkedNode * node = new LinkedNode( this, this->firstNode, value );

	if( firstNode == NULL )
	{
		// this node is the first one in our list
		this->firstNode = node;
	}

	// this is the node we are currently working with
	this->currentNode = node;

	//and our size got increased by one
	this->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 = this->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
	this->size--;
}

template < class T >
void LinkedList< T >::remove( int index )
{
	//start with the first node
	LinkedNode< T > * node = this->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
	this->size--;
}

template < class T >
T * LinkedList< T >::get( int index )
{
	// start with the first node
	LinkedNode< T > * node = this->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->value();
}

Initial URL


Initial Description
Not working.

Initial Title
Linked List (LinkedList.cpp)

Initial Tags


Initial Language
C++