#include<iostream>
#ifdef _DEBUG #ifndef DEBUG_NEW #define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) #define new DEBUG_NEW #endif #endif
using namespace std;
namespace _nmsp1 { template<typename T> struct Double_Node { T data; Double_Node<T>* prior; Double_Node <T>* next;
};
template<typename T> class Db_Cir_Link_List { public: Db_Cir_Link_List(); ~Db_Cir_Link_List();
public: bool ListInsert(int i, const T& e); bool ListDelete(int i);
bool GetElem(int i, T& e); int LocateElem(const T& e);
void DispList(); int ListLength(); bool Empty();
private: Double_Node<T>* m_head; int m_length; };
template<typename T> Db_Cir_Link_List<T>::Db_Cir_Link_List() { m_head = new Double_Node<T>; m_head->next = m_head; m_head->prior = m_head; m_length = 0; }
template <typename T> bool Db_Cir_Link_List<T>::ListInsert(int i, const T& e) { if (i < 1 || i >(m_length + 1)) { cout << "元素" << e << "插入的位置" << i << "不合法,合法的位置是1到" << m_length + 1 << "之间!" << endl; return false; }
Double_Node<T>* p_curr = m_head; for (int j = 0; j < (i - 1); ++j) { p_curr = p_curr->next; }
Double_Node<T>*node = new Double_Node<T>; node->data = e; node->next = p_curr->next; node->prior = p_curr;
p_curr->next->prior = node; p_curr->next = node; cout << "成功在位置为" << i << "处插入元素" << e << "!" << endl; m_length++; return true; }
template < typename T> bool Db_Cir_Link_List<T>::ListDelete(int i) { if (m_length < 1) { cout << "当前双循环链表为空,不能删除任何数据!" << endl; return false; } if (i < 1 || i > m_length) { cout << "删除的位置" << i << "不合法,合法的位置是1到" << m_length << "之间!" << endl; return false; }
Double_Node<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); ++j) { p_curr = p_curr->next; }
Double_Node<T>* p_willdel = p_curr->next; Double_Node<T>* p_willdelNext = p_willdel->next; p_curr->next = p_willdel->next; p_willdelNext->prior = p_curr;
cout << "成功删除位置为" << i << "的元素,该元素的值为" << p_willdel->data << "!" << endl; m_length--; delete p_willdel; return true; }
template<class T> bool Db_Cir_Link_List<T>::GetElem(int i, T& e) { if (m_length < 1) { cout << "当前双循环链表为空,不能获取任何数据!" << endl; return false; }
if (i < 1 || i > m_length) { cout << "获取元素的位置" << i << "不合法,合法的位置是1到" << m_length << "之间!" << endl; return false; }
Double_Node<T>* p_curr = m_head; for (int j = 0; j < i; ++j) { p_curr = p_curr->next; } e = p_curr->data; cout << "成功获取位置为" << i << "的元素,该元素的值为" << e << "!" << endl; return true; }
template<class T> int Db_Cir_Link_List<T>::LocateElem(const T& e) { Double_Node<T>* p_curr = m_head; for (int i = 1; i <= m_length; ++i) { if (p_curr->next->data == e) { cout << "值为" << e << "的元素在双循环链表中第一次出现的位置为" << i << "!" << endl; return i; } p_curr = p_curr->next; } cout << "值为" << e << "的元素在双循环链表中没有找到!" << endl; return -1; }
template<class T> void Db_Cir_Link_List<T>::DispList() { Double_Node<T>* p = m_head->next; while (p != m_head) { cout << p->data << " "; p = p->next; } cout << endl; }
template<class T> int Db_Cir_Link_List<T>::ListLength() { return m_length; }
template<class T> bool Db_Cir_Link_List<T>::Empty() { if (m_head->next == m_head) { return true; } return false; }
template <typename T> Db_Cir_Link_List<T>::~Db_Cir_Link_List() { Double_Node<T>* pnode = m_head->next; Double_Node<T>* ptmp; while (pnode != m_head) { ptmp = pnode; pnode = pnode->next;
delete ptmp; } delete m_head; m_head = nullptr; m_length = 0; } }
int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_nmsp1::Db_Cir_Link_List<int>slinkobj; slinkobj.ListInsert(1, 1); slinkobj.ListInsert(2, 2); slinkobj.ListInsert(3, 3); slinkobj.DispList(); slinkobj.ListDelete(3); slinkobj.DispList();
slinkobj.ListInsert(3, 3); slinkobj.ListInsert(4, 4); int a=0; slinkobj.GetElem(3,a); cout << slinkobj.ListLength() << endl; slinkobj.Empty(); slinkobj.LocateElem(3); }
|