題目是bool operator==(CirDoublyList<T> &list) //比較*this和list是否相等,不能比較長度
以下是DoubleNode.h頭檔案,課本照抄沒問題,
template <class T>
class DoubleNode
{
public:
T data;
DoubleNode<T> *prev, *next;
DoubleNode()
{
this->prev = this->next = NULL;
}
DoubleNode(T data, DoubleNode<T> *prev=NULL, DoubleNode<T> *next=NULL)
{
this->data = data;
this->prev = prev;
this->next = next;
}
};
以下是DoubleNode.h的頭檔案,請大神們幫看看哪里有錯
#include <iostream>
using namespace std;
#include "DoubleNode.h"
template <class T>
class CirDoublyList
{
public:
DoubleNode<T> *head;
CirDoublyList();
CirDoublyList(T values[], int n);
~CirDoublyList();
bool empty();
int count();
bool operator==(CirDoublyList<T> &list);
bool equals(DoubleNode<T>*p,DoubleNode<T>*q);
T& get(int i);
virtual void set(int i, T x);
friend ostream& operator<<<>(ostream& out, CirDoublyList<T>&);
DoubleNode<T>* insert(int i, T x);
virtual DoubleNode<T>* insert(T x);
T remove(int i);
void removeAll();
};
template <class T>
CirDoublyList<T>::CirDoublyList()
{
this->head = new DoubleNode<T>();
this->head->prev = this->head->next = this->head;
}
template <class T>
CirDoublyList<T>::CirDoublyList(T values[], int n)
{
this->head = new DoubleNode<T>();
this->head->prev = this->head->next = this->head;
for (int i=0; i<n; i++)
this->insert(values[i]);
}
template <class T>
CirDoublyList<T>::~CirDoublyList()
{
this->removeAll();
delete this->head;
}
template <class T>
bool CirDoublyList<T>::empty()
{
return this->head->next==this->head;
}
template <class T>
int CirDoublyList<T>::count()
{
int i=0;
for (DoubleNode<T> *p=this->head->next; p!=this->head; p=p->next)
i++;
return i;
}
template <class T>
T& CirDoublyList<T>::get(int i)
{
DoubleNode<T> *p=this->head->next;
for (int j=0; p!=this->head && j<i; j++)
p=p->next;
if (i>=0 && p!=NULL)
return p->data;
throw out_of_range("無法回傳i的值");
}
template <class T>
void CirDoublyList<T>::set(int i, T x)
{
DoubleNode<T> *p=this->head->next;
for (int j=0; p!=this->head && j<i; j++)
p=p->next;
if (i>=0 && p!=this->head)
p->data = x;
else throw out_of_range("無法設定i的值");
}
template <class T>
ostream& operator<<<>(ostream& out, CirDoublyList<T> &list)
{
out<<"(";
for (DoubleNode<T> *p=list.head->next; p!=list.head; p=p->next)
{
out<<p->data;
if (p->next!=list.head)
out<<", ";
}
out<<")\n";
return out;
}
template <class T>
DoubleNode<T>* CirDoublyList<T>::insert(int i, T x)
{
DoubleNode<T> *front=this->head;
for (int j=0; front->next!=head && j<i; j++)
front = front->next;
DoubleNode<T> *q = new DoubleNode<T>(x, front, front->next);
front->next->prev = q;
front->next = q;
return q;
}
template <class T>
DoubleNode<T>* CirDoublyList<T>::insert(T x)
{
DoubleNode<T> *q = new DoubleNode<T>(x, this->head->prev, this->head);
this->head->prev->next = q;
this->head->prev = q;
return q;
}
template <class T>
T CirDoublyList<T>::remove(int i)
{
DoubleNode<T> *p=this->head->next;
for (int j=0; p!=this->head && j<i; j++)
p=p->next;
if (i>=0 && p!=this->head)
{
T old = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
return old;
}
throw out_of_range("無法洗掉i的值");
}
template <class T>
void CirDoublyList<T>::removeAll()
{
DoubleNode<T> *p=this->head->next;
while (p!=this->head)
{
DoubleNode<T> *q = p;
p = p->next;
delete q;
}
this->head->next = this->head->prev = this->head;
}
template <class T>
bool CirDoublyList<T>::operator==(CirDoublyList<T> &list)
{
return equals(this->head->next,list.head->next);
}
template <class T>
bool CirDoublyList<T>::equals(DoubleNode<T> *p,DoubleNode<T> *q)
{
if(p==NULL&&q==NULL||p!=NULL&&q!=NULL&&p->data=https://bbs.csdn.net/topics/=q->data&&equals(p->next,q->next))
return true;
else return false;
}
以下是cpp
#include"CirDoublyList.h"
int main()
{
CirDoublyList<char>lista("aaa",3);
CirDoublyList<char>listb("aaa",3);
if(lista==listb)
cout<<"相等"<<endl;
else
cout<<"不相等"<<endl;
system("pause");
return 0;
}
如果兩條鏈里面字符數目不同,可以正常運行輸出不等,按照這么運行就會 0x00d92c09 處未處理的例外: 0xC00000FD: Stack overflow崩潰
還有如果一條鏈有字幕另一條鏈空,也會有錯誤,請教!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/62993.html
下一篇:請問這里面的ID_COMMCTRL是什么型別,代表什么? 還有編譯出錯 'ID_COMMCTRL' : undeclared identifier請問怎么解決
