Oct 23, 2019 at 5:18pm UTC
#include "linked_list.h"
#include <string>
using namespace std;
template <class object>
LinkedList<object>::LinkedList(const LinkedList<object> & rhs)
{
header = new Node();
for(Node * ptr=rhs.header->next;ptr!=NULL;ptr=ptr->next)
add(ptr->datum);
size=rhs.size;
}
template <class object>
LinkedList<object>::LinkedList(const object & d)
{
size=1;
header=new Node();
this->add(d);
}
template <class object>
LinkedList<object>::LinkedList()
{
size=0;
header=new Node();
}
template <class object>
void LinkedList<object>::add(const object & d)
{
size++;
header->datum=d;
Node * ptr = new Node();
ptr->next=header;
header=ptr;
ptr=NULL;
}
template <class object>
bool LinkedList<object>::remove(const object & d)
{
Node *ptr;
for(ptr=header; ptr->next != NULL && ptr->next->datum != d; ptr=ptr->next);
if(ptr->next==NULL) return 0;
size--;
Node * deleted=ptr->next;
ptr->next=deleted->next;
delete deleted;
deleted = NULL;
}
template<class object>
object LinkedList<object>::find(const object & d)
{
for(Node * ptr=header ; ptr != NULL ; ptr=ptr->next)
if((ptr->datum) == d) return ptr->datum;
return Not_Found;
}
template<class object>
unsigned LinkedList<object>::getSize()
{
return size;
}
template<class object>
object LinkedList<object>::removeFirst()
{
if(size==0) return Not_Found;
Node * deleted;
deleted=header->next;
header->next=header->next->next;
object returnV=deleted->datum;
delete deleted;
size--;
return returnV;
}
template <class object>
void LinkedList<object>::add(const LinkedList<object> & rhs)
{
LinkedList<object> added(rhs);
while(added.getSize() != 0)
this->add(added.removeFirst());
}
template <class object>
void LinkedList <object>:: print(ostream & out)
{
for(Node * ptr =header->next;ptr!=NULL;ptr=ptr->next)
out << ptr->datum << endl;
}
template <class object>
void LinkedList <object>:: printReverse(ostream & out)
{
printReversePrivate(out, header->next);
}
template <class object>
void LinkedList <object>:: printReversePrivate(ostream & out, Node * ptr)
{
if(ptr!=NULL){
printReversePrivate(out, ptr->next);
out << ptr->datum;
}
}
Oct 23, 2019 at 5:19pm UTC
this is the header
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#include <iostream>
#include <ostream>
using namespace std;
template<class object>
class LinkedList
{
private:
struct Node
{
Node * next;
object datum;
Node(){next=NULL;}
Node(object d, Node * n): datum(d), next(n){}
};
Node * header;
object Not_Found;
unsigned size;
void printReversePrivate(ostream & out, Node * ptr);
public:
LinkedList();
LinkedList(const object & d);
LinkedList(const LinkedList & rhs);
void printReverse(ostream & out);
void add(const object & d);
void add(const LinkedList & d);
bool remove(const object & d);
object find(const object & d);
object removeFirst();
unsigned getSize();
void print(ostream & out);
};
#include "linked_list.cpp"
#endif
Oct 23, 2019 at 5:21pm UTC
when i remove (#include "linked_list.cpp") from the header file i get some weird errors, but when i keep it i get 12 errors of the type "function template has already been defined"
Oct 23, 2019 at 5:40pm UTC
Please use code formatting and edit the main post instead of appending it.
Oct 23, 2019 at 9:51pm UTC
i get some weird errors
That's pretty vague.
Change the extension of linked_list.cpp to something else like .tcpp to indicate that it is a template file, not a source file. You can't compile a template file by itself, so giving it a .cpp extension might confuse the compiler.