国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一般線性鏈表類的C++實現

2019-11-17 05:46:18
字體:
來源:轉載
供稿:網友

以下的C++類LinkList實現了線性鏈表的一般操作。可以直接在其他的程序中直接建立它的對象,其中線性表中的數據在此為整型,具體應用的時候可以適當的修改,并可以在此基礎上繼續封裝特定的功能。

頭文件: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();
}
}

//初始化,分配一個頭節點。
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;
}

//返回鏈表的中當前節點數。
int LinkList::GetLength() {
return m_listLength;
}

//將鏈表清空,釋放當前所有節點。
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指定的節點內的數據設置為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 > (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;
}

//刪除一個指定的節點。
//節點位置由position指定。
//positon的值從1到listLength。
//若鏈表為空或指定的節點不存在則返回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;
}

//得到指定位置節點的指針。
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;
}

//定位與指定數據相等的數據節點。
//假如在當前鏈表中已經存在該數據則返回該數據節點的索引號。
//若不存在這樣的節點則返回0。
//節點索引從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;
}
*/




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 满城县| 墨竹工卡县| 涿州市| 什邡市| 永修县| 武汉市| 龙州县| 淮南市| 登封市| 固阳县| 赫章县| 德清县| 井陉县| 上思县| 兰州市| 敦化市| 阿坝| 韶山市| 永宁县| 晋中市| 得荣县| 怀安县| 平安县| 高碑店市| 万全县| 孟津县| 繁峙县| 武冈市| 太康县| 伊宁县| 金湖县| 从江县| 池州市| 铜鼓县| 买车| 勐海县| 密山市| 三门峡市| 长阳| 雷州市| 磐安县|