C++語言實(shí)現(xiàn)線性表之鏈表實(shí)例
更新時(shí)間:2015年04月20日 11:21:48 作者:司青
這篇文章主要介紹了C++語言實(shí)現(xiàn)線性表之鏈表,實(shí)例分析了C++實(shí)現(xiàn)線性表中鏈表的原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C++語言實(shí)現(xiàn)線性表之鏈表實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
插入、刪除結(jié)點(diǎn)的代碼有點(diǎn)多,但這樣提高了代碼的可讀性,且不增加時(shí)間復(fù)雜度,不會(huì)影響程序性能
#include <iostream>
using namespace std;
template<typename T>
class CList;
template<class T>
class Node
{
friend CList<T>;
private:
T m_data;
Node *m_pNext;
};
template<class T>
class CList
{
public:
CList();
~CList();
bool IsEmpty();
void Append(const T &data);
void Delete(const int &pos);
void Print();
int GetLength();
T Find(const int &pos);
void Insert(const int &pos,const T &data);
private:
Node<T> *m_pHead;
Node<T> *m_pEnd;
int m_len;
void Create();
void Destroy();
};
//為頭結(jié)點(diǎn)分配空間
template<class T>
void CList<T>::Create()
{
m_pHead = new Node<T>;
m_pEnd = new Node<T>;
m_pHead->m_pNext = NULL;
m_pEnd->m_pNext = m_pHead->m_pNext;
m_len = 0;
}
template<class T>
CList<T>::CList()
{
Create();
}
//刪除所有結(jié)點(diǎn)
template<class T>
void CList<T>::Destroy()
{
Node<T> *pF = m_pHead->m_pNext;
Node<T> *pT;
while(pF)
{
pT = pF;
pF = pF->m_pNext;
delete pT;
}
}
template<class T>
CList<T>::~CList()
{
Destroy();
}
//判斷是否為空
template<class T>
bool CList<T>::IsEmpty()
{
if(!m_pHead->m_pNext)
{
return true;
}
else
{
return false;
}
}
//從表的最后加入一個(gè)元素
template<class T>
void CList<T>::Append(const T &data)
{
Node<T> *pT = new Node<T>;
pT->m_data = data;
pT->m_pNext = NULL;
if(!m_pHead->m_pNext)
{
m_pHead->m_pNext = pT;
}
else
{
(m_pEnd->m_pNext)->m_pNext = pT;
}
m_pEnd->m_pNext = pT;
++m_len;
}
//刪除一個(gè)元素
template<class T>
void CList<T>::Delete(const int &pos)
{
if(pos < 0 || pos < m_len)
{
cout<<"位置不合法"<<endl;
return;
}
Node<T> *pPre = NULL;//存放前一個(gè)結(jié)點(diǎn)
Node<T> *pBehind = NULL;//存放后一個(gè)結(jié)點(diǎn)
Node<T> *pT = m_pHead->m_pNext;//目標(biāo)結(jié)點(diǎn)
int ix = -1;
while(pT)
{
++ix;
if(ix == pos - 1 - 1)
{
pPre = pT;
}
else if(ix == pos - 1)
{
pBehind = pT->m_pNext;
break;
}
pT = pT->m_pNext;
}
if(!pPre)//如果指針為空則說明pos是指第一個(gè)元素
{
delete pT;
m_pHead->m_pNext = pBehind;
--m_len;
return;
}
if(!pBehind)//如果指針為空則說明pos是指最后一個(gè)元素
{
m_pEnd = pPre;
delete pT;
}
pPre->m_pNext = pBehind;
--m_len;
}
//輸出所有數(shù)據(jù)
template<class T>
void CList<T>::Print()
{
Node<T> *pT = m_pHead->m_pNext;
while(pT)
{
cout<<pT->m_data<<",";
pT = pT->m_pNext;
}
cout<<endl;
}
template<class T>
int CList<T>::GetLength()
{
return m_len;
}
//查找數(shù)據(jù)
template<class T>
T CList<T>::Find(const int &pos)
{
if(pos <= 0)
{
cout<<"輸入不合法"<<endl;
return NULL;
}
if(pos > m_len)
{
cout<<"超出表長"<<endl;
return NULL;
}
int i = 0;
Node<T> *pT = m_pHead->m_pNext;
while(pT)
{
++i;
if(i == pos)
{
return pT->m_data;
}
pT = pT->m_pNext;
}
return NULL;
}
template<class T>
void CList<T>::Insert(const int &pos,const T &data)
{
if(pos <= 0 || pos >m_len)
{
cout<<"輸入不合法"<<endl;
return;
}
int i = 0;
Node<T> *pT = m_pHead->m_pNext;
Node<T> *pPre = NULL;
Node<T> *pBehind = NULL;
while(pT)
{
++i;
if(i == pos - 1)
{
pPre = pT;
}
if(i == pos)
{
pBehind = pT->m_pNext;
break;
}
pT = pT->m_pNext;
}
Node<T> *pNew = new Node<T>;
pNew->m_data = data;
if(!pPre)//如果指針為空則說明pos是指第一個(gè)元素
{
pNew->m_pNext = m_pHead->m_pNext;
m_pHead->m_pNext = pNew;
++m_len;
return;
}
if(!pBehind)//如果指針為空則說明pos是指最后一個(gè)元素
{
m_pEnd->m_pNext = pNew;
}
pPre->m_pNext = pNew;
pNew->m_pNext = pT;
++m_len;
}
希望本文所述對大家的C++程序設(shè)計(jì)有所幫助。
相關(guān)文章
C指針原理教程之編譯原理-小型計(jì)算器實(shí)現(xiàn)
本文給大家分享的是如何使用C語言編寫一個(gè)小型計(jì)算器的實(shí)例代碼,有需要的小伙伴可以參考下2019-02-02
C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮
這篇文章主要為大家詳細(xì)介紹了C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
基于C++的攝像頭圖像采集及拼接程序的簡單實(shí)現(xiàn)
本程序是在?ubuntu14.04?平臺(tái)下實(shí)現(xiàn)的,在本項(xiàng)目目錄下,已經(jīng)有編譯生成的可執(zhí)行程序,其中Camera_to_Frmae.cpp是我們從雙攝像頭實(shí)時(shí)抓取單幀圖像的源碼,對基于C++的攝像頭圖像采集及拼接程序的實(shí)現(xiàn)感興趣的朋友一起看看吧2022-01-01
OpenCV圖像旋轉(zhuǎn)Rotate的詳細(xì)介紹
這篇文章主要介紹了OpenCV圖像旋轉(zhuǎn)Rotate,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

