I am supposed to be creating a doubly circular linked list, but I am now have problems creating the actual linked list. Is there a problem with the classes? Also, for the assignment, the list is suppose to be a template class, but now I just want to get the code to work.
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;
//template <class Object>
//template <class Object>
class Node
{
//friend ostream& operator<<(ostream& os, const Node&c);
public:
Node( int d=0);
void print(){
cout<<this->data<<endl;
}
//private:
Node* next;
Node* prev;
int data;
friend class LinkList;
};
Node::Node(int d):data(d)
{
}
//template <class Object>
class LinkList
{
void create();
public:
//LinkList();
LinkList():head(NULL),tail(NULL),current(NULL)
{create();}
int base;
//LinkList(const LinkList & rhs, const LinkList & lhs);
~LinkList(){delete current;}
const Node& front() const;//element at current
const Node& back() const;//element following current
void move();
void insert (const Node & a);//add after current
void remove (const Node &a);
void print();
private:
Node* current;//current
Node* head;
Node* tail;
};
void LinkList::print()
{
Node *nodePt =head->next;
while(nodePt != head)
{
cout<<"print function"<<endl;
cout<<nodePt->data<<endl;
nodePt=nodePt->next;
}
}
//element at current
void LinkList::create()
{
current = head = tail = new Node(0);
current->next = current->prev = current;
}
const Node& LinkList::back()const
{
return *current;
}
//element after current
const Node& LinkList::front() const
{
return *current ->next;
}
void LinkList::move()
{
current = current ->next;
}
//insert after current
void LinkList :: insert(const Node& a)
{
Node* newNode= new Node();
newNode->prev=current;
newNode->next=current->next;
newNode->prev->next=newNode;
newNode->next->prev=newNode;
current=newNode;
}
void LinkList::remove(const Node& a)
{
Node* oldNode;
oldNode=current;
oldNode->prev->next=oldNode->next;
oldNode->next->prev=oldNode->prev;
delete oldNode;
}
//main file
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include "LinkedList.h"
using namespace std;
int main()
{
int n;
cout<<"How many Nodes would you like to create"<<endl;
cin>>n;
LinkList list1 ;
list1.print();
for(int loop = 0;loop < n;++loop)
{
list1.insert(loop);
}
list1.print();
}