标签:include oid cpp windows 编程 链表 delete queue 耦合
利用C++实现链表结构
1.定义链表的数据结构
CList作为一个链表类,它的成员是由CNode组成
CNode有两个属性,tElement用于指向当前的节点,next用于指向下一个节点
/******************************************************** ** CList链表结构实现 ** ** ** ***********************************************************/ #ifndef _LIST_ #define _LIST_ #include <Windows.h> template <class T> //利用泛型编程笔迷避免链表节点与链表耦合 //链表节点 class CNode { public: CNode(T* tElement) : tElement(tElement), next(0) { } T* Element() const { return tElement; } CNode*& Next(){ return next; } private: T* tElement;//用于指示当前节点 CNode* next;//用于指示下一个节点 }; template <class T> class CList { public: CList() : dwCount(0), head(0){ } CList(T* tElement) : dwCount(1), head(new CNode<T>(tElement)){ } virtual ~CList(){ } void Append(CNode<T>*& node, T* tElement);//添加 void Insert(T* tElement);//插入 bool Remove(T* tElement);//溢出 DWORD Count() const { return dwCount; }//节点数量 CNode<T>*& Head() { return head; }//节点头 T* GetFirst(){ return head != NULL ? head->Element() : NULL; } T* GetLast(); T* GetNext(T* tElement); T* Find(DWORD(*Function)(T* tParameter), DWORD dwValue); protected: CList(const CList& list); CList& operator = (const CList& list); private: CNode<T>* head; DWORD dwCount; }; template <class T> void CList<T>::Append(CNode<T>*& node, T* tElement) { if (node == NULL) { dwCount++; node = new CNode<T>(tElement); return; } Append(node->Next(), tElement); } template <class T> void CList<T>::Insert(T* tElement) { dwCount++; if (head == NULL) { head = new CNode<T>(tElement); return; } CNode<T>* tmp = head; head = new CNode<T>(tElement); head->Next() = tmp; } template <class T> bool CList<T>::Remove(T* tElement) { if (head == NULL) { return NULL; } if (head->Element() == tElement) { CNode<T>* tmp = head; head = head->Next(); delete tmp; dwCount--; return true; } CNode<T>* tmp = head; CNode<T>* lst = head->Next(); while (lst != NULL) { if (lst->Element() == tElement) { tmp->Next() = lst->Next(); delete lst; dwCount--; return true; } lst = lst->Next(); tmp = tmp->Next(); } return false; } template <class T> T* CList<T>::GetLast() { if (head) { CNode<T>* tmp = head; while (tmp->Next()) { tmp = tmp->Next(); } return tmp->Element(); } return NULL; } template <class T> T* CList<T>::GetNext(T* tElement) { if (head == NULL) { return NULL; } if (tElement == NULL) { return GetFirst(); } if (head->Element() == tElement) { return head->Next() != NULL ? head->Next()->Element() : NULL; } CNode<T>* lst = head->Next(); while (lst != NULL) { if (lst->Element() == tElement) { return lst->Next() != NULL ? lst->Next()->Element() : NULL; } lst = lst->Next(); } return NULL; } template <class T> T* CList<T>::Find(DWORD(*Function)(T* tParameter), DWORD dwValue) { try { T* tElement = NULL; while (tElement = GetNext(tElement)) { if (Function(tElement) == dwValue) { return tElement; } } } catch (...) {} return NULL; } #endif
CQueue定义代码如下
#ifndef __QUEUE__ #define __QUEUE__ #include "CList.h" template<class T> class CQueue : CList<T> { public: CQueue() : CList<T>(){ } CQueue(T* tElement) : CList<T>(tElement){ } virtual ~CQueue(){ } virtual void Enqueue(T* tElement) { Append(Head(), tElement); } virtual T* Dequeue() { T* tElement = GetFirst(); Remove(tElement); return tElement; } virtual T* Peek() { return GetFirst(); } CList<T>::Count; protected: CQueue(const CQueue<T>& cQueue); CQueue<T>& operator = (const CQueue<T>& cQueue); }; #endif
#include <iostream> using namespace std; #include "CQueue.h" #include "CStack.h" int _main() { CQueue<int>* cQueue = new CQueue<int>(); CStack<double>* cStack = new CStack<double>(); for (int i = 0; i < 10; i++) { cQueue->Enqueue(new int(i)); cStack->Push(new double(i / 10.0)); } cout << "Queue - integer collection:" << endl; for (; cQueue->Count();) { cout << *cQueue->Dequeue() << " "; } cout << endl << endl << "Stack - double collection:" << endl; for (; cStack->Count();) { cout << *cStack->Pop() << " "; } delete cQueue; delete cStack; cout << endl << endl; return system("pause"); }
标签:include oid cpp windows 编程 链表 delete queue 耦合
原文地址:https://www.cnblogs.com/yuanshijie/p/12297995.html