Here is the LinkedList.h file:

// LinkedList.h -- class for a linked list as a data structure

template <class DataType>
struct Node {
	DataType info;
	Node<DataType> *next;
};

// LinkedList maintains a current position in list after each function call
// If an object of a struct is used for DataType, the == operator must be 
// overloaded for it; the left and right operands are both DataType objects
// the == comparison is used for finding elements, usually by key value
// For find, retrieve and remove functions, if you are using an object as an element, a 
// typical use would be to set the key of the desired object to find or remove, then pass 
// the object into the function.
// The constructor, copy constructor, overloaded assignment operator, and insert function
// can throw an exception if out of heap memory.

template <class DataType>
class LinkedList
{
public:
	LinkedList( );
	LinkedList( const LinkedList<DataType> & aplist );
	~LinkedList( );
	LinkedList<DataType> & operator =( const LinkedList<DataType> & rlist );
	void insert( const DataType & element ); // no current position after use
	bool first( DataType & listEl );	  	
         inline bool getNext( DataType & listEl );	  
         bool find ( const DataType & element );  	
         bool retrieve( DataType & element ); 
         bool replace( const DataType & newElement ); // replaces element at current position 
         bool isEmpty( ) const; // returns true if linked list is empty
         void makeEmpty( );		// no current position
private:
	Node<DataType> *start;
	Node<DataType> *current; // points to node at current position	
	inline void deepCopy( const LinkedList<DataType> & original );
};

#include "LinkedList.cpp"

and here is my LinkedList.cpp file:

// LinkedList.cpp -- function definitions for the linked list data structure

#include <iostream>

using namespace std;

template <class DataType>
LinkedList<DataType>::LinkedList( )
{
	start = current = NULL;
}

template <class DataType>
LinkedList<DataType>::LinkedList( const LinkedList<DataType> & aplist )
{
	deepCopy( aplist );
}

template <class DataType>
LinkedList<DataType>::~LinkedList( )
{
	makeEmpty( );
}

template <class DataType>
LinkedList<DataType> & LinkedList<DataType>::operator =( const LinkedList<DataType> & rlist )
{
	if ( this == &rlist )
		return *this;
	makeEmpty( );
	deepCopy( rlist );
	return *this;
}

// inserts at the beginning of the linked list
// no current position after use		
template <class DataType>
void LinkedList<DataType>::insert( const DataType & element )
{
	current = NULL;
	Node<DataType> *newNode = new Node<DataType>;
	newNode->info = element;
	newNode->next = start;
	start = newNode;
}	

// returns first element of list in listEl and current position is set to this element; 
// if list is empty, returns false and there is no current position; 
// otherwise, returns true
template <class DataType>
bool LinkedList<DataType>::first( DataType & listEl )
{
	if ( start == NULL ) 
		return false;

	current = start;
	listEl = start->info;
	return true;
}

// retrieves the next element of a linked list beyond the last element that was retrieved
// by first or getNext functions and returns it in listEl;
// current position is set to this element.
// if no element exists at this position, getNext returns false and there is no 
// current position; returns true otherwise	
template <class DataType>
inline bool LinkedList<DataType>::getNext( DataType & listEl )				  
{
	if ( current == NULL ) 
		return false;
	if ( current->next == NULL ) {
		current = NULL;
		return false;
		}

	current = current->next;
	listEl = current->info;
	return true;
}


// returns true if element is found; returns false if element is not found
// if found, found element becomes current position in list; 
// if not found, there is no current position
template <class DataType>
bool LinkedList<DataType>::find( const DataType & element )
{
	DataType item;
	if ( !first( item ) )
		return false;
	do if ( item == element ) 
			return true;
	while ( getNext( item ) );

	return false;
}

// returns true if element is found; returns false if element is not found
// if found, found element becomes current position in list; 
// if not found, there is no current position
template <class DataType>
bool LinkedList<DataType>::retrieve( DataType & element )
{
	if ( !find( element ) )
		return false;
	element = current->info;
	return true;
}

// replaces element at current position in list with newElement; 
// returns false if there is no current position (no list modification occurs); 
// returns true otherwise 
template <class DataType>
bool LinkedList<DataType>::replace( const DataType & newElement ) 
{
	if ( current == NULL )
		return false;
	current->info = newElement;
	return true;
}

// returns true if element is found; returns false if element is not found
// if found, element is set to found element;
// no current position after use
template <class DataType>
bool LinkedList<DataType>::remove( DataType & element )
{
	current = NULL;
	if ( start == NULL )
		return false;
	Node<DataType> *ptr = start;
	if ( ptr->info == element ) {
		element = ptr->info;
		start = start->next;
		delete ptr;
		return true;
		}
	while ( ptr->next != NULL ) {
		if ( ptr->next->info == element ) {
			Node<DataType> *tempPtr = ptr->next;
			element = tempPtr->info;
			ptr->next = tempPtr->next;
			delete tempPtr;
			return true;
			}
		ptr = ptr->next;
		}

	return false;
}

template <class DataType>
bool LinkedList<DataType>::isEmpty( ) const				  
{
	return start == NULL;
}

template <class DataType>
void LinkedList<DataType>::makeEmpty( )					  
{
	while ( start != NULL ) {
		current = start;
		start = start->next;
		delete current;
	}

	current = NULL;
}

template <class DataType>
inline void LinkedList<DataType>::deepCopy( const LinkedList<DataType> & original )
{
	start = current = NULL;
	if ( original.start == NULL )
		return;
	Node<DataType> *copyptr = start = new Node<DataType>;
	Node<DataType> *originalptr = original.start;
	copyptr->info = originalptr->info;
	if ( originalptr == original.current )
		current = copyptr;
	while ( originalptr->next != NULL ) {
		originalptr = originalptr->next;
		copyptr->next = new Node<DataType>;
		copyptr = copyptr->next;
		copyptr->info = originalptr->info;
		if ( originalptr == original.current )
			current = copyptr;
	}
	copyptr->next = NULL;
}

Everytime I try to compile the LinkedList.cpp file, I keep getting these errors:

LinkedList.cpp:8: error: expected constructor, destructor, or type conversion before '<' token
LinkedList.cpp:14: error: expected constructor, destructor, or type conversion before '<' token
LinkedList.cpp:20: error: expected constructor, destructor, or type conversion before '<' token
LinkedList.cpp:26: error: expected constructor, destructor, or type conversion before '<' token
LinkedList.cpp:38: error: expected initializer before '<' token
LinkedList.cpp:51: error: expected initializer before '<' token
LinkedList.cpp:67: error: expected initializer before '<' token
LinkedList.cpp:86: error: expected initializer before '<' token
LinkedList.cpp:102: error: expected initializer before '<' token
LinkedList.cpp:114: error: expected initializer before '<' token
LinkedList.cpp:126: error: expected initializer before '<' token
LinkedList.cpp:153: error: expected initializer before '<' token
LinkedList.cpp:159: error: expected initializer before '<' token
LinkedList.cpp:171: error: expected initializer before '<' token

Could someone please help me out and tell me what's wrong? Any form of help would be greatly appreciated...

Your cpp worked with my small driver that I made

#include <string>
#include "LinkedList.h"

using namespace std;

int main(){
    LinkedList<int> intlist;
    intlist.insert(0);
    intlist.insert(1);
    intlist.insert(2);
    intlist.insert(3);
    int dummy;
    if(intlist.first(dummy)){
        cout<<dummy;
    }
    if(intlist.getNext(dummy)){
        cout<<dummy;
    }
    if(intlist.getNext(dummy)){
        cout<<dummy;
    }
    if(intlist.getNext(dummy)){
        cout<<dummy;
    }
    cin>>dummy;
    return 0;
}

However, I modified your .h to be this:

// LinkedList.h -- class for a linked list as a data structure
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H


template <class DataType>
struct Node {
	DataType info;
	Node<DataType> *next;
};

// LinkedList maintains a current position in list after each function call
// If an object of a struct is used for DataType, the == operator must be 
// overloaded for it; the left and right operands are both DataType objects
// the == comparison is used for finding elements, usually by key value
// For find, retrieve and remove functions, if you are using an object as an element, a 
// typical use would be to set the key of the desired object to find or remove, then pass 
// the object into the function.
// The constructor, copy constructor, overloaded assignment operator, and insert function
// can throw an exception if out of heap memory.

template <class DataType>
class LinkedList
{
public:
	LinkedList( );
	LinkedList( const LinkedList<DataType> & aplist );
	~LinkedList( );
	LinkedList<DataType> & operator =( const LinkedList<DataType> & rlist );
	void insert( const DataType & element ); // no current position after use
	bool first( DataType & listEl );	  	
         inline bool getNext( DataType & listEl );	  
         bool find ( const DataType & element );  	
         bool retrieve( DataType & element ); 
         bool replace( const DataType & newElement ); // replaces element at current position 
         bool isEmpty( ) const; // returns true if linked list is empty
         void makeEmpty( );		// no current position
         bool remove( DataType & element );
private:
	Node<DataType> *start;
	Node<DataType> *current; // points to node at current position	
	inline void deepCopy( const LinkedList<DataType> & original );
};

#include "LinkedList.cpp"
#endif

All I added to the .h was:

#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
bool remove( DataType & element );
#endif

Every time I compile your LinkedList.cpp, I get those weird errors, but your data structure seems to work for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.