【发布时间】:2013-07-12 01:15:59
【问题描述】:
这是我的节点和链表类
#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);
//}
我想创建一个双循环链表,但我现在在创建实际链表时遇到了问题。是班级的问题。同样对于分配,列表应该是一个模板类,但现在我只想让代码工作。我不确定如何正确创建链接列表。
【问题讨论】:
-
你有什么问题?
-
除了需要有人完成家庭作业外,没有对问题的描述。你的一些方法甚至没有意义,你是从其他地方复制过来的吗?除了说“打开关于数据结构和/或 C++ 的教科书”之外,我认为我们真的没有任何帮助。
标签: c++ linked-list nodes