这是我的节点和链表类
#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
{
public:
//LinkList();
LinkList():head(NULL),tail(NULL),current(NULL){}
int base;
//LinkList(const LinkList & rhs, const LinkList & lhs);
~LinkList(){delete head,tail,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 create();
void print();
private:
Node* current;//current
Node* head;
Node* tail;
};
void LinkList::print()
{
Node *nodePt =head;
while(nodePt)
{
cout<<"print function"<<endl;
cout<<nodePt->data<<endl;
nodePt=nodePt->next;
}
}
//element at current
void LinkList::create()
{
Node start(0);
}
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;
}
#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;
Node a(n);
Node b(n+1);
a.print();
a.next=&b;
LinkList list1 ;
list1.create();
list1.print();
list1.insert(0);
list1.print();
//for(int i=0 ;i<n;i++)
//{
//list1.insert(i);
//}
我想创建一个双循环链表,但我现在在创建实际链表时遇到了问题。是班级的问题。同样对于分配,列表应该是一个模板类,但现在我只想让代码工作。我不确定如何正确创建链接列表。