I couldn't figure out how to do the copy constructor, so I decided to work on the insert function so I can be able to insert a node into the list, and be able to output the list and go from there. Everything seems to compile now, however I can't figure out how to output the linked list. The header file was given to me by my professor and I just don't understand how to print out the list with the code she has given me, it's incredibly frustrating. Here's what I made so far:
For the header file:
Code:
#include <iostream>
using namespace std;
template <class T>
class List {
private:
class Node {
public:
T data;
Node *next;
};
public:
List();
List(const List &);
~List();
int getSize() const;
bool isEmpty() const;
bool insert(const T&, const int &);
bool remove(const int &);
T runJosephusAlgorithm();
const List& operator=(const List&);
void display(ostream&) const;
friend ostream& operator << <>(ostream&, const List<T>&);
private:
Node *first; // pointer to the first element of the linked list
};
template<class T>
List<T>::List()
{
first = NULL;
}
template <class T>
List<T>::List(const List & Original_list) // copy constructor creates copy of original list in body
{
}
template <class T>
List<T>::~List()
{
}
template<class T>
bool List<T>::insert(const T& data, const int & count)
{
int n = 1;
Node *lastPtr;
Node *currPtr;
Node *newNode = new Node;
if (count == 1)
{
newNode->next = first;
first = newNode;
return true;
}
if (count > 1 && count != 0)
{
currPtr = first;
lastPtr = first;
while ((currPtr != NULL) && (n < count))
{
n++;
lastPtr = currPtr;
currPtr = currPtr->next;
}
lastPtr->next = newNode;
newNode->next = currPtr;
return true;
}
else
return false;
}
template <class T>
void List<T>::display(ostream &out) const
{
Node *current = first;
while (current != NULL) {
out << current->data << endl;
current = current->next;
}
}
template <class T>
ostream & operator<<(ostream & o, const List<T> &output_elements)
{
o << output_elements.data;
return o;
}
and the main function:
Code:
#include<iostream>
#include"circularlist.h"
int main()
{
List<int> cl_1;
cl_1.insert(12, 1);
cl_1.display(); // No idea how to print this out????
}