以下为LinkList.h文件代码
- #ifndef LINKLIST_H_INCLUDED
- #define LINKLIST_H_INCLUDED
- typedef struct LNode
- {
- int data;
- struct LNode *next;
- } LNode, *pLinkList;
- class LinkList
- {
- private:
- pLinkList pList;
- int listLength;
- public:
- LinkList();
- ~LinkList();
- bool InitList();
- bool DestroyList();
- bool ClearList();
- bool IsEmpty();
- int GetLength();
- bool GetNode(int position, LNode** node);
- int LocateElem(int elem);
- bool SetNodeData(int position, int newData);
- bool GetNodeData(int position, int &data);
- bool InsertNode(int beforeWhich, int data);
- /*bool DeleteNode(int position);*/
- bool DeleteNode(int data);
- };
- #endif // LINKLIST_H_INCLUDED
- 以下为LinkList.cpp文件代码
- #include <iostream>
- #include "LinkList.h"
- LinkList::LinkList()
- {
- pList = NULL;
- listLength = 0;
- InitList();
- }
- LinkList::~LinkList()
- {
- if (!DestroyList())
- {
- DestroyList();
- }
- }
- //初始化,分配一个头节点。
- bool LinkList::InitList()
- {
- if (!(pList = new LNode))
- {
- return false;
- }
- pList->next = NULL;
- return true;
- }
- //销毁链表。
- bool LinkList::DestroyList()
- {
- if (!ClearList()) {
- return false;
- }
- delete pList;
- return true;
- }
- //判断链表是否为空。若为空,返回true,否则返回false。
- bool LinkList::IsEmpty() {
- if (pList->next == NULL)
- {
- return true;
- }
- return false;
- }
- //返回链表的中当前节点数。
- int LinkList::GetLength()
- {
- return listLength;
- }
- //将链表清空,释放当前所有节点。
- bool LinkList::ClearList()
- {
- if (pList == NULL)
- {
- return false;
- }
- LNode *pTemp = NULL;
- while (pList->next != NULL)
- {
- pTemp = pList->next;
- pList->next = pTemp->next;
- delete pTemp;
- }
- listLength = 0;
- return true;
- }
- //将position指定的节点内的数据设置为newData。
- //第一个有效节点的position为1。
- bool LinkList::SetNodeData(int position, int newData)
- {
- LNode *pTemp = NULL;
- if (!(GetNode(position, &pTemp)))
- {
- return false;
- }
- pTemp->data = newData;
- return true;
- }
- //得到指定位置节点的数据。
- //节点索引从1到listLength。
- bool LinkList::GetNodeData(int position, int &data)
- {
- LNode *pTemp = NULL;
- if (!(GetNode(position, &pTemp)))
- {
- return false;
- }
- data = pTemp->data;
- return true;
- }
- //在链表中插入一个节点。
- //插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。
- //beforeWhich的取值在1到ListLength+1之间。
- bool LinkList::InsertNode(int beforeWhich, int data)
- {
- LNode *pTemp = NULL;
- if (beforeWhich < 1 || beforeWhich > (listLength + 1))
- {
- return false;
- }
- if (!(GetNode(beforeWhich - 1, &pTemp)))
- {
- return false;
- }
- LNode *newNode = new LNode;
- newNode->data = data;
- newNode->next = pTemp->next;
- pTemp->next = newNode;
- listLength++;
- return true;
- }
- //删除一个指定的节点。
- //节点位置由position指定。
- //positon的值从1到listLength。
- //若链表为空或指定的节点不存在则返回false。
- /*
- bool LinkList::DeleteNode(int position)
- {
- if (position < 1 || position > listLength)
- {
- return false;
- }
- LNode *pTemp = NULL;
- if (!(GetNode(position - 1, &pTemp)))
- {
- return false;
- }
- LNode *pDel = NULL;
- pDel = pTemp->next;
- pTemp->next = pDel->next;
- delete pDel;
- listLength--;
- return true;
- }*/
- //通过指定data删除节点
- bool LinkList::DeleteNode(int data)
- {
- LNode *pTemp = NULL;
- pTemp = pList;
- while (1)
- {
- if (pTemp->next->data == data)
- {
- break;
- }
- pTemp = pTemp->next;
- }
- LNode *pDel = NULL;
- pDel = pTemp->next;
- pTemp->next = pDel->next;
- delete pDel;
- listLength--;
- return true;
- }
- //得到指定位置节点的指针。
- bool LinkList::GetNode(int position, LNode **node)
- {
- LNode *pTemp = NULL;
- int curPos = -1;
- pTemp = pList;
- while (pTemp != NULL)
- {
- curPos++;
- if (curPos == position)
- break;
- pTemp = pTemp->next;
- }
- if (curPos != position)
- {
- return false;
- }
- *node = pTemp;
- return true;
- }
- //定位与指定数据相等的数据节点。
- //如果在当前链表中已经存在该数据则返回该数据节点的索引号。
- //若不存在这样的节点则返回0。
- //节点索引从0开始到listLength。
- int LinkList::LocateElem(int elem)
- {
- LNode *pTemp = NULL;
- int curIndex = 1;
- pTemp = pList->next;
- while ((pTemp != NULL) && (pTemp->data != elem))
- {
- pTemp = pTemp->next;
- curIndex++;
- }
- if (pTemp == NULL)
- {
- return 0;
- }
- return curIndex;
- }