链表实现
#include <stdio.h> #include <stdlib.h> #include "string.h" #include <windows.h> #define SUCCESS 1 // 执行成功 #define ERROC -1 // 执行失败 #define INDEX_IS_ERROR -2 // 错误的索引号 #define BUFFER_IS_EMPTY -3 // 缓冲区已空 template <class T_ELE> class LinkedList { public: LinkedList(); ~LinkedList(); public: BOOL IsEmpty(); //判断链表是否为空 空返回1 非空返回0 void Clear(); //清空链表 DWORD GetElement(IN DWORD dwIndex, OUT T_ELE& Element); //根据索引获取元素 DWORD GetElementIndex(IN T_ELE& Element); //根据元素获取链表中的索引 DWORD Insert(IN T_ELE Element); //新增元素 DWORD Insert(IN DWORD dwIndex, IN T_ELE Element); //根据索引新增元素 DWORD Delete(IN DWORD dwIndex); //根据索引删除元素 DWORD GetSize(); //获取链表中元素的数量 private: typedef struct _NODE { T_ELE Data; _NODE* pNext; }NODE, * PNODE; PNODE GetIndexCurrentNode(DWORD dwIndex); //获取索引为dwIndex的指针 PNODE GetIndexPreviousNode(DWORD dwIndex); //获取索引为dwIndex的前一个节点指针 PNODE GetIndexNextNode(DWORD dwIndex); //获取索引为dwIndex的后一个节点指针 private: PNODE m_pList; //链表头指针,指向第一个节点 DWORD m_dwLength; //元素的数量 }; //无参构造函数 初始化成员 template<class T_ELE> LinkedList<T_ELE>::LinkedList() :m_pList(NULL), m_dwLength(0) { } //析构函数 清空元素 template<class T_ELE> LinkedList<T_ELE>::~LinkedList() { Clear(); printf("析构函数被执行了"); } //判断链表是否为空 template<class T_ELE> BOOL LinkedList<T_ELE>::IsEmpty() { if (m_pList == NULL || m_dwLength == 0) { return 1; } else { return 0; } } //清空链表 template<class T_ELE> void LinkedList<T_ELE>::Clear() { // 1. 判断链表是否为空 if (IsEmpty()) { return; } PNODE ptemp = m_pList; // 2. 循环删除链表中的节点 for (int f = 0; f < m_dwLength; f++) { PNODE ptemp1 = ptemp->pNext; delete ptemp; ptemp = ptemp1; } // 3. 删除最后一个节点并将链表长度置为0 m_dwLength = 0; } //根据索引获取元素 template<class T_ELE> DWORD LinkedList<T_ELE>::GetElement(IN DWORD dwIndex, OUT T_ELE& Element) { // 1. 判断索引是否有效 if (dwIndex < 0 || dwIndex >= m_dwLength) { return INDEX_IS_ERROR; } // 2. 取得索引指向的节点 PNODE ptempn = m_pList; for (int h = 0; h < dwIndex; h++) { ptempn = ptempn->pNext; } // 3. 将索引指向节点的值复制到OUT参数 memcpy(&Element, ptempn, sizeof(T_ELE)); return SUCCESS; } //根据元素内容获取索引 template<class T_ELE> DWORD LinkedList<T_ELE>::GetElementIndex(IN T_ELE& Element) { // 1. 判断链表是否为空 if (IsEmpty()) { return BUFFER_IS_EMPTY; } PNODE ptempno = m_pList; // 2. 循环遍历链表,找到与Element相同的元素 for (int l = 0; l < m_dwLength; l++) { while (!memcmp(ptempno, &Element, sizeof(T_ELE))) { printf("相同的元素在索引%d", l); return l; } ptempno = ptempno->pNext; } } //在链表尾部新增节点 template<class T_ELE> DWORD LinkedList<T_ELE>::Insert(IN T_ELE Element) { // 1. 判断链表是否为空 PNODE pNewNode = new NODE; memset(pNewNode, 0, sizeof(NODE)); memcpy(pNewNode, &Element, sizeof(T_ELE)); if (IsEmpty()) { m_pList = pNewNode; m_dwLength++; return SUCCESS; } else { PNODE ptempnode = m_pList; for (int i = 1; i < m_dwLength; i++) { ptempnode = ptempnode->pNext; } ptempnode->pNext = pNewNode; m_dwLength++; return SUCCESS; } // 2. 如果链表中已经有元素 } //将节点新增到指定索引的位置 template<class T_ELE> DWORD LinkedList<T_ELE>::Insert(IN DWORD dwIndex, IN T_ELE Element) { if (IsEmpty()) { if (dwIndex != 0) { return ERROC; } Insert(Element); } if (dwIndex == m_dwLength) { Insert(Element); } PNODE pNewNODE = new NODE; PNODE ptempnode = m_pList; memset(pNewNODE, 0, sizeof(NODE)); memcpy(pNewNODE, &Element, sizeof(T_ELE)); if (dwIndex<0 || dwIndex> m_dwLength) { printf("不是有效的索引值"); return INDEX_IS_ERROR; } if (dwIndex == 0) { pNewNODE->pNext = ptempnode->pNext; ptempnode->pNext = pNewNODE; m_dwLength++; return SUCCESS; } if (dwIndex >= 0 && dwIndex <= m_dwLength) { for (int k = 0; k < dwIndex; k++) { ptempnode = ptempnode->pNext; } pNewNODE->pNext = ptempnode->pNext; ptempnode->pNext = pNewNODE; m_dwLength++; return SUCCESS; } // 1. 判断链表是否为空 // 2. 判断索引值是否有效 // 3. 如果索引为0 // 4. 如果索引为链表尾 // 5. 如果索引为链表中 } //根据索引删除节点 template<class T_ELE> DWORD LinkedList<T_ELE>::Delete(IN DWORD dwIndex) { if (IsEmpty()) { return BUFFER_IS_EMPTY; } // 1. 判断链表是否为空 if (dwIndex<0 || dwIndex> m_dwLength) { printf("不是有效的索引值"); return INDEX_IS_ERROR; } // 2. 判断索引值是否有效 if (dwIndex == 0 && m_dwLength == 1) { delete m_pList; } PNODE ptemp = m_pList; PNODE ptemp1 = ptemp; // 3. 如果链表中只有头节点,且要删除头节点 if (dwIndex == 0) { m_pList = ptemp1->pNext; delete ptemp1; } // 4. 如果要删除头节点 if (dwIndex >= 0 && dwIndex <= m_dwLength) { for (int m = 1; m < dwIndex; m++) { ptemp1 = ptemp1->pNext; } ptemp = ptemp1->pNext; ptemp1->pNext = ptemp->pNext; delete ptemp; } // 5. 如果是其他情况 } //获取链表中节点的数量 template<class T_ELE> DWORD LinkedList<T_ELE>::GetSize() { return m_dwLength; } void TestLink() { LinkedList<int>* p = new LinkedList<int>; p->Insert(0x1); p->Insert(0x2); p->Insert(0x3); p->Insert(1,0x4); int x = 0; //m_pList->Insert p->GetElement(2, x); printf("%x", x); p->Delete(2); p->GetElement(2,x); printf("%x", x); } int main() { TestLink(); return 0; }