根据之前的调试,应该是拷贝构造函数有问题。可是我真的看不出来它有什么错误
#include<iostream>
using namespace std;
template<class T>
class Node {
public:
T date;
Node<T>* next;
Node() {
this->date = 0;
this->next = NULL;
}
Node(T date, Node<T>* next = NULL) {
this->date = date;
this->next = next;
}
};
#include<iostream>
using namespace std;
#include"Node.h"
template<class T>
class CirSinglyList {
private:
Node<T>* head;
Node<T>* rear;
public:
CirSinglyList();
CirSinglyList(T values[], int n);
~CirSinglyList();
friend ostream& operator<<<>(ostream&, CirSinglyList<T>&);
void insert(T x);
CirSinglyList<T> operator*(CirSinglyList<T>& list);
CirSinglyList(const CirSinglyList<T>& list);
};
template<class T>
CirSinglyList<T>::CirSinglyList() {
this->head = new Node<T>();
this->head->next = this->head;
this->rear = this->head;
}
template<class T>
CirSinglyList<T>::CirSinglyList(T values[], int n) {
this->head = new Node<T>();
this->head->next = this->head;
this->rear = this->head;
for (int i = 0; i < n; i++) {
this->rear->next = new Node<T>(values[i]);
this->rear = this->rear->next;
}
this->rear->next = this->head;
}
template<class T>
CirSinglyList<T>::~CirSinglyList() {
this->rear = NULL;
Node<T>* front;
while (this->head) {
front = this->head->next;
delete head;
this->head = front;
}
}
template<class T>
ostream& operator<<<>(ostream& out, CirSinglyList<T>& list) {
out << "(";
for (Node<T>* p = list.head->next;p!= list.head;p = p->next) {
out << p->date;
if (p->next != list.head) {
out << ",";
}
}
out << ")\n";
return out;
}
template<class T>
void CirSinglyList<T>::insert(T x) {
Node<T>* front = new Node<T>(x,this->head);
this->rear->next = front;
this->rear = this->rear->next;
}
template<class T>
CirSinglyList<T>::CirSinglyList(const CirSinglyList<T>& list) {
this->head=new Node<T>();
this->rear=this->head;
for(Node<T>* p=list.head->next;p!=list.head;p=p->next)
{
this->rear->next=new Node<T>(p->date);
this->rear=this->rear->next;
}
this->rear->next=this->head;
}
template<class T>
CirSinglyList<T> CirSinglyList<T>::operator*(CirSinglyList<T>& list) {
CirSinglyList<T> cir;
Node<T>* p1 = this->head->next;
Node<T>* p2 = list.head->next;
while (p1 != this->head) {
while (p2 != list.head) {
if (p1->date == p2->date) {
cir.insert(p2->date);
break;
}
p2 = p2->next;
}
p2 = list.head->next;
p1 = p1->next;
}
return cir;
}
#include<iostream>
using namespace std;
#include"CirSinglyList.h"
int main() {
int arr1[12] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int arr2[7] = { 3,16,9,8,5,19,33 };
CirSinglyList<int> cir1(arr1, 12);
CirSinglyList<int> cir2(arr2, 7);
cout << cir1 << endl;
cout << cir2 << endl;
CirSinglyList<int> cir3= cir1 * cir2;
cout << cir3 << endl;
}
是析构函数出了问题。因为你这个是循环链表,你这样处理会导致一个指针重复释放
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632