#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 Node { T data; Node<T>* next;
};
template <typename T> class CirLinkList { public: CirLinkList(); ~CirLinkList();
public: bool ListInsert(int i, const T& e); void ListInsertBack(const T& e); bool ListDelete(int i);
bool GetElem(int i, T& e); int LocateElem(const T& e);
void DispList(); int ListLength(); bool Empty(); void rear(); void Merge_Link_list(CirLinkList<T> p1, CirLinkList<T> p2);
private: Node<T>* m_head; Node<T>* m_rear; int m_length; };
template <typename T> CirLinkList<T>::CirLinkList() { m_head = new Node<T>; m_head->next = m_head; m_rear = m_head; m_length = 0; }
template <typename T> bool CirLinkList<T>::ListInsert(int i, const T& e) { if (i < 1 || i >(m_length + 1)) { cout << "元素" << e << "插入的位置" << i << "不合法,合法的位置是1到" << m_length + 1 << "之间!" << endl; return false; }
if (i - 1 == m_length) { ListInsertBack(e); return true; }
Node<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); ++j) { p_curr = p_curr->next; }
Node<T>* node = new Node<T>; node->data = e; node->next = p_curr->next; p_curr->next = node;
cout << "成功在位置为" << i << "处插入元素" << e << "!" << endl; m_length++; return true;
}
template<typename T> void CirLinkList<T>::ListInsertBack(const T& e) { Node<T>* node = new Node<T>; node->data = e; node->next = m_rear->next; m_rear->next = node; m_rear = node; if (m_rear->next == m_head) { cout << "尾部插入元素成功,更新尾指针成功" << endl; } m_length++;
}
template < typename T> bool CirLinkList<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; }
Node<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); ++j) { p_curr = p_curr->next; } Node<T>* p_willdel = p_curr->next; p_curr->next = p_willdel->next; cout << "成功删除位置为" << i << "的元素,该元素的值为" << p_willdel->data << "!" << endl; m_length--; delete p_willdel; return true; }
template<class T> bool CirLinkList<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; }
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 CirLinkList<T>::LocateElem(const T& e) { 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 CirLinkList<T>::DispList() { Node<T>* p = m_head->next; while (p != m_head) { cout << p->data << " "; p = p->next; } cout << endl; }
template<class T> int CirLinkList<T>::ListLength() { return m_length; }
template <typename T> CirLinkList<T>::~CirLinkList() { Node<T>* pnode = m_head->next; Node<T>* ptmp; while (pnode != m_head) { ptmp = pnode; pnode = pnode->next;
delete ptmp; } delete m_head; m_head = nullptr; m_length = 0; }
template <typename T> void CirLinkList<T>::rear() { cout << " 链表最后一个元素为" << m_rear->data << endl; }
template<typename T> void CirLinkList<T>::Merge_Link_list(CirLinkList<T>p1, CirLinkList<T>p2) { Node<T>* p1_rear = p1.m_rear; Node<T>* p1head = p1_rear->next; Node<T>* p2_rear = p2.m_rear; Node<T>* p2head = p2_rear->next;
p1_rear->next = p2_rear->next->next; p2_rear->next = p1head;
p1_rear = p2_rear; p1.m_length += p2.m_length; cout << "合并链表" << endl;
}
}
int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_nmsp1::CirLinkList<int> slinkobj; _nmsp1::CirLinkList<int> slinkobj1; slinkobj.ListInsert(1, 12); slinkobj.ListInsert(1, 24); slinkobj.ListInsert(3, 48); slinkobj.ListInsert(2, 100); slinkobj.DispList(); slinkobj.rear();
slinkobj1.ListInsert(1, 1000); slinkobj1.ListInsert(2, 2000); slinkobj1.ListInsert(3, 3000); slinkobj1.ListInsert(4, 4000); slinkobj1.DispList(); slinkobj.Merge_Link_list(slinkobj, slinkobj1); slinkobj1.DispList(); slinkobj.DispList();
return 0; }
|