以下的C++類LinkList實現(xiàn)了線性鏈表的一般操作。可以直接在其他的程序中直接建立它的對象,其中線性表中的數(shù)據(jù)在此為整型,具體應(yīng)用的時候可以適當(dāng)?shù)男薷模⒖梢栽诖嘶A(chǔ)上繼續(xù)封裝特定的功能。
頭文件:LinkList.h
typedef struct LNode { int data; struct LNode *next; }LNode, *pLinkList;
class LinkList { private: pLinkList m_pList; int m_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); };
Cpp文件:LinkList.cpp
#include <iostream.h> #include "LinkList.h"
LinkList::LinkList() { m_pList = NULL; m_listLength = 0;
InitList(); }
LinkList::~LinkList() { if (!DestroyList()) { DestroyList(); } }
//初始化,分配一個頭節(jié)點。 bool LinkList::InitList() { if (!(m_pList = new LNode)) { return false; } m_pList->next = NULL;
return true; }
//銷毀鏈表。 bool LinkList::DestroyList() { if (!ClearList()) { return false; }
delete m_pList;
return true; }
//判斷鏈表是否為空。若為空,返回true,否則返回false。 bool LinkList::IsEmpty() { if (m_pList->next == NULL) { return true; } return false; }
//返回鏈表的中當(dāng)前節(jié)點數(shù)。 int LinkList::GetLength() { return m_listLength; }
//將鏈表清空,釋放當(dāng)前所有節(jié)點。 bool LinkList::ClearList() { if (m_pList == NULL) { return false; }
LNode *pTemp = NULL; while (m_pList->next != NULL) { pTemp = m_pList->next; m_pList->next = pTemp->next; delete pTemp; } m_listLength = 0;
return true; }
//將position指定的節(jié)點內(nèi)的數(shù)據(jù)設(shè)置為newData。 //第一個有效節(jié)點的position為1。 bool LinkList::SetNodeData(int position, int newData) { LNode *pTemp = NULL; if (!(GetNode(position, &pTemp))) { return false; }
pTemp->data = newData;
return true; }
//得到指定位置節(jié)點的數(shù)據(jù)。 //節(jié)點索引從1到listLength。 bool LinkList::GetNodeData(int position, int &data) { LNode *pTemp = NULL;
if (!(GetNode(position, &pTemp))) { return false; }
data = pTemp->data;
return true; }
//在鏈表中插入一個節(jié)點。 //插入的位置由beforeWhich指定,新節(jié)點插入在beforeWhich之前。 //beforeWhich的取值在1到ListLength+1之間。 bool LinkList::InsertNode(int beforeWhich, int data) { LNode *pTemp = NULL;
if (beforeWhich < 1 || beforeWhich > (m_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;
m_listLength++;
return true; }
//刪除一個指定的節(jié)點。 //節(jié)點位置由position指定。 //positon的值從1到listLength。 //若鏈表為空或指定的節(jié)點不存在則返回false。 bool LinkList::DeleteNode(int position) { if (position < 1 || position > m_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;
m_listLength--;
return true; }
//得到指定位置節(jié)點的指針。 bool LinkList::GetNode(int position, LNode **node) { LNode *pTemp = NULL; int curPos = -1;
pTemp = m_pList; while (pTemp != NULL) { curPos++; if (curPos == position) break; pTemp = pTemp->next; }
if (curPos != position) { return false; }
*node = pTemp;
return true; }
//定位與指定數(shù)據(jù)相等的數(shù)據(jù)節(jié)點。 //如果在當(dāng)前鏈表中已經(jīng)存在該數(shù)據(jù)則返回該數(shù)據(jù)節(jié)點的索引號。 //若不存在這樣的節(jié)點則返回0。 //節(jié)點索引從0開始到listLength。 int LinkList::LocateElem(int elem) { LNode *pTemp = NULL; int curIndex = 1;
pTemp = m_pList->next; while ((pTemp != NULL) && (pTemp->data != elem)) { pTemp = pTemp->next; curIndex++; }
if (pTemp == NULL) { return 0; }
return curIndex; }
/* int main(){ LinkList l;
l.InsertNode(1, 10); l.InsertNode(2, 20); l.InsertNode(3, 30); l.InsertNode(4, 40); cout << l.GetLength() << endl;
int dataTemp = 0; for (int i = 1; i <= l.GetLength(); i++) { l.GetNodeData(i, dataTemp); cout << dataTemp << endl; }
if (l.SetNodeData(3, 50)) { cout <<"DONE\n"; } else { cout << "Failed\n"; }
for (i = 1; i <= l.GetLength(); i++) { l.GetNodeData(i, dataTemp); cout << dataTemp << endl; }
if (l.DeleteNode(4)) { cout <<"DONE\n"; } else { cout << "Failed\n"; }
for (i = 1; i <= l.GetLength(); i++) { l.GetNodeData(i, dataTemp); cout << dataTemp << endl; }
cout << l.LocateElem(50) << endl; return 0; } */
|