如何解决循环单链接列表的复制构造器
所以我使用C ++编写代码已经有很长时间了,并且在复制构造函数方面苦苦挣扎,使用它时我的程序不断崩溃。
节点:
template <class T>
class Node
{
public:
Node(T data,Node<T> * n = 0)
{ element = data;
next = n;
prev = n;}
~Node()
{ next = 0;
prev = 0;}
T element;
Node<T>* next;
Node<T>* prev;
CircularList<T>::CircularList(const CircularList<T> & other)
{
if(other.tail == NULL)
{
this->tail = NULL;
}
else
{
this->tail = other.tail;
Node<T> * original = this->tail;
Node<T> * temp = other.tail->next;
while(temp != other.tail)
{
original->next = temp;
temp = temp->next;
original = original->next;
}
original->next = temp;
}
}
(编辑) 这是CircularList类的规范。我想我只是不知道要采取什么步骤来使它成为深拷贝而不是浅拷贝。
template<class T>
class CircularList;
template<class T>
ostream& operator<<(ostream&,CircularList<T>&);
template<class T>
class CircularList : public LinearStructure<T>
{
public:
friend ostream& operator<< <T>(ostream&,CircularList<T>&);
CircularList();
CircularList(const CircularList<T>& other);
CircularList<T>& operator=(const CircularList<T>& other);
virtual CircularList<T>* clone();
virtual ~CircularList();
virtual void insert(int index,T element);
virtual T remove(int index);
virtual T get(int index) const;
virtual bool isEmpty();
virtual void clear();
Node<T>* getleader();
protected:
ostream& print(ostream& os);
private:
int size() const;
Node<T>* tail;
};
是的,当我对此进行测试时(列表不为空):
CircularList<int> clist;
clist.insert(0,8);
CircularList<int> clist2(clist);
它崩溃了。任何帮助,将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。