#鏈表的實現
- 定義一個開始節點初始化爲空
- 鏈表的前驅和後繼,開始節點(頭節點沒有前驅只有唯一後繼),尾節點(沒有後繼只有唯一前驅,
- 鏈表存儲的節點有兩部分組成:數據段和指針域(後繼地址)
- 建立鏈表
- 清除鏈表
- 插入一個新元素
- 刪除一個元素
- 修改一個元素
- 檢索
帶頭結點的單鏈表
– 整個單鏈表: head
– 第一個結點: head->next,head ≠ NULL
– 空表判斷: head->next == NULL
- 當前結點a1:fence->next (curr 隱含)
#pragma once
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T>
class Link;
//類模板 除了構造和析構的名字可以省略參數列表,其他抵擋不能省略
template <typename T>
class Node
{
public:
Node(T data = T()):_data(data),_pnext(NULL){}
private:
T _data;
Node<T> *_pnext; //_pnext私有成員不能訪問
#if 0 //含有太多的多餘關係
template <typename T>
friend class Link;
#endif
friend class Link<T>;
};
template <typename T>
class Link {
public:
Link();
~Link();
bool isEmptyLink(); //檢查鏈表是否爲空
void clearLink(); //將鏈表存儲的內容清除,成爲空表
int lengthLink(); //鏈表的長度
bool appendLink(const T value); //尾部擴展節點
bool insertHeadLink(const T value); //頭部插入節點
bool insertLink(const int p, const T value); //某個節點插入
bool deleteLink(const int p); //刪除某個節點
bool getValueLink(const int p, T &value); //獲取節點的值
bool getPosLink(int &p, const T value); //檢索
void showLink(); //顯示鏈表
void reserveLink(); //翻轉鏈表
void deleteEndPosNode(int n); //刪除倒數N個節點
private:
Node<T> *_phead; //類的組合關係,利用友元類
};
template <typename T>
Link<T>::Link() {
cout << "Link()" << endl;
cout << "data type : " << typeid(_phead).name() << endl;
_phead = new Node<T>();
}
template <typename T>
Link<T>::~Link() {
Node<T> *ptmp = _phead;
while (ptmp != NULL) {
_phead = _phead->_pnext;
delete ptmp;
ptmp = _phead;
}
}
template <typename T>
bool Link<T>::isEmptyLink()
{
return _phead->_pnext == NULL;
}
template <typename T>
void Link<T>::clearLink()
{
Node<T> *ptmp = _phead;
Node<T> *pcur = ptmp->_pnext;
while (pcur != NULL) {
ptmp = ptmp->_pnext;
delete pcur;
pcur = ptmp;
}
}
template <typename T>
int Link<T>::lengthLink()
{
int i = 0;
Node<T> *pcur = _phead;
while(pcur->_pnext != NULL)
{
pcur = pcur->_pnext;
i++;
}
return i;
}
template <typename T>
bool Link<T>::appendLink(const T value)
{
cout << "appendLink(const T value)" << endl;
Node<T> *ptmp = new Node<T>(value);
if (ptmp == NULL) {
cout << "no space to new" << endl;
return false;
}
Node<T> *pcur = _phead;
while (pcur->_pnext != NULL) {
pcur = pcur->_pnext;
}
pcur->_pnext = ptmp;
return true;
}
template <typename T>
bool Link<T>::insertHeadLink(const T value)
{
cout << "insertHeadLink(const T value)" << endl;
Node<T> *ptmp = new Node<T>(value);
if (ptmp == NULL) {
cout << "no space to new" << endl;
return false;
}
ptmp->_pnext = _phead->_pnext;
_phead->_pnext = ptmp;
return true;
}
template <typename T>
bool Link<T>::insertLink(const int p, const T value)
{
cout << "insertLink(const int p, const T value)" << endl;
int i;
Node<T> *ptmp = new Node<T>(value);
Node<T> *pcur = _phead;
for ( i = 1; i < p; i++)
{
if (pcur->_pnext == NULL) {
if (i != p) {
cout << "no position of p" << endl;
return false;
}
else {
break;
}
}
pcur = pcur->_pnext;
}
ptmp->_pnext = pcur->_pnext;
pcur->_pnext = ptmp;
return true;
}
template <typename T>
bool Link<T>::deleteLink(const int p)
{
cout << "deleteLink(const int p)" << endl;
int i ;
Node<T> *pcur = _phead;
for ( i = 1; i < p; i++)
{
if (pcur->_pnext == NULL) {
cout << "no hava postion of p" << endl;
return false;
}
pcur = pcur->_pnext;
}
Node<T> *ptmp = pcur->_pnext;
pcur->_pnext = pcur->_pnext->_pnext;
delete ptmp;
ptmp = NULL;
return true;
}
template <typename T>
bool Link<T>::getValueLink(const int p, T &value)
{
cout << "getValueLink(const int p, T &value)" << endl;
Node<T> *pcur = _phead;
int i;
for ( i = 1; i <= p; i++)
{
if (pcur->_pnext == NULL) {
cout << "no hava postion of p" << endl;
return false;
}
pcur = pcur->_pnext;
}
value = pcur->_data;
return false;
}
template <typename T>
bool Link<T>::getPosLink(int &p, const T value)
{
cout << "getPosLink(int &p, const T value) " << endl;
int i = 1;
Node<T> *pcur = _phead->_pnext;
while (pcur->_data != value && pcur->_pnext != NULL) {
pcur = pcur->_pnext;
i++;
}
if (pcur->_data == value) {
p = i;
return true;
}
return false;
cout << endl;
}
template <typename T>
void Link<T>::showLink()
{
cout << "showLink()" << endl;
Node<T> *pcur = _phead->_pnext;
while (pcur != NULL) {
cout <<" data : "<< pcur->_data;
pcur = pcur->_pnext;
}
cout << endl;
}
template <typename T>
void Link<T>::reserveLink() {
Node<T> *pNode = _phead;
Node<T> *pPrev = NULL;
Node<T> *pReverseList = NULL:
while (pNode != NULL) {
Node<T> *next = pNode->_pnext;
if (next == NULL) {
pReverseList = pNode;
}
pNode->_pnext = pPrev;
pPrev = pNode;
pNode = next;
}
_phead->_pnext = pReverseList;
}
template <typename T>
void Link<T>::deleteEndPosNode(int n)
{
Node<T> *tmp = new Node<T>();
tmp->_pnext = _phead;
Node<T> *firsthead = tmp;
Node<T> *secondhead = tmp;
for (int i = 0; i <= n; i++)
{
firsthead = firsthead->_pnext;
}
while (firsthead != NULL)
{
firsthead = firsthead->_pnext;
secondhead = secondhead->_pnext;
}
Node<T> *del = secondhead->_pnext;
secondhead->_pnext = secondhead->_pnext->_pnext;
delete del;
}
#include "Link.h"
//模板不能在.cpp文件中實現
int main()
{
//類模板的選擇實例化。成員方法只有在使用的時候實例化
Link<int> link1;
link1.showLink();
cout << "==============================" << endl;
link1.appendLink(11);
link1.showLink();
cout << "==============================" << endl;
link1.insertHeadLink(10);
link1.insertHeadLink(12);
link1.insertHeadLink(15);
link1.insertHeadLink(13);
link1.insertHeadLink(14);
link1.showLink();
cout << "==============================" << endl;
link1.reserveLink();
link1.showLink();
cout << "==============================" << endl;
//int p = 0;
//link1.getPosLink(p, 10);
//cout << endl << p << endl;
cout << "==============================reserveLink" << endl;
int value = 0;
link1.getValueLink(1, value);
cout << endl << value << endl;
cout << "==============================" << endl;
int len = link1.lengthLink();
cout << "len: " << len << endl;
link1.deleteLink(5);
link1.showLink();
cout << "==============================" << endl;
int len2 = link1.lengthLink();
cout << "len: " << len2 << endl;
cout << "==============================" << endl;
link1.clearLink();
link1.showLink();
cout << "==============================" << endl;
link1.insertLink(1, 12);
link1.showLink();
cout << "==============================" << endl;
link1.deleteEndPosNode(1);
link1.showLink();
cout << "==============================" << endl;
char* str = new char[sizeof("hello")];
memcpy(str, "hello", sizeof("hello"));
Link<const char*> link2;
link2.appendLink(str);
link2.showLink();
link2.insertHeadLink("hi");
link2.showLink();
int len3 = link2.lengthLink();
cout << "len: " << len3 << endl;
}