雙向鏈表(雙鏈表)是鏈表的一種。和單鏈表一樣,雙鏈表也是由節點組成,它的每個數據結點中都有兩個指針,分別指向直接后繼和直接前驅。所以,從雙向鏈表中的任意一個結點開始,都可以很方便地訪問它的前驅結點和后繼結點。一般我們都構造雙向循環鏈表。
C++實現雙向鏈表
雙向鏈表頭文件(LinkList.h)
#pragma once
#ifndef _LINK_LIST_H
#define _LINK_LIST_H
#include <iostream>
using namespace std;
template<class T>
struct DNode
{
public:
T value;
DNode *prev;
DNode *next;
public:
DNode() {}
DNode(T t, DNode *prev, DNode *next) {
this->value = t;
this->prev = prev;
this->next = next;
}
};
template<class T>
class DoubleLink {
public:
DoubleLink();
~DoubleLink();
int size();
int isEmpty();
T get(int index);
T getFirst();
T getLast();
int insert(int index, T t);
int insertFirst(T t);
int appendLast(T t);
int del(int index);
int delFirst();
int delLast();
private:
int count;
DNode<T> *pHead;
private:
DNode<T> *getNode(int index);
};
template<class T>
DoubleLink<T>::DoubleLink() : count(0)
{
// 創建表頭。注意:表頭沒有存儲數據
pHead = new DNode<T>();
pHead->next = NULL;
}
// 析構函數
template<class T>
DoubleLink<T>::~DoubleLink()
{
// 刪除所有結點
DNode<T>* pTemp;
DNode<T>* pNode = pHead->next;
while (pNode != pHead)
{
pTemp = pNode;
pNode = pNode->next;
delete pTemp;
}
// 刪除表頭
delete pHead;
pHead = NULL;
}
// 返回結點數目
template<class T>
int DoubleLink<T>::isEmpty()
{
return count == 0;
}
// 返回節點數目
template < class T>
int DoubleLink<T>::size()
{
return count;
}
// 獲取index位置的結點
template<class T>
DNode<T>* DoubleLink<T>::getNode(int index)
{
// 判斷參數有效性
if (index < 0 || index >= count)
{
cout << " getNode failed! the index is out of round" << endl;
return NULL;
}
// 正向查找, 查找優化,減少查找次數
if (index <= count / 2)
{
int i = 0;
DNode<T>* pIndex = pHead->next;
while (i++ < index)
{
pIndex = pIndex->next;
}
return pIndex;
}
// 反向查找
int j = 0;
int rIndex = count - index - 1;
DNode<T>* pRindex = pHead->prev;
while (j++ < rIndex)
{
pRindex = pRindex->prev;
}
return pRindex;
}
// 獲取第index位置的結點的值
template<class T>
T DoubleLink<T>::get(int index)
{
return getNode(index)->value;
}
// 獲取第一個結點的值
template<class T>
T DoubleLink<T>::getLast()
{
return getNode(count-1)->value;
}
// 獲取最后一個結點的值
template<class T>
T DoubleLink<T>::getFirst()
{
return getNode(0)->value;
}
// 將結點插入到第index位置之前
template<class T>
int DoubleLink<T>::insert(int index, T t)
{
if (index == 0)
return insertFirst(t);
DNode<T>* pIndex = getNode(index);
DNode<T>* pNode = new DNode<T>(t, pIndex->prev, pIndex);
pIndex->prev->next = pNode;
pIndex->prev = pNode;
count++;
return 0;
}
// 將結點插入到第一個結點處
template<class T>
int DoubleLink<T>::insertFirst(T t)
{
DNode<T>* pNode = new DNode<T>(t, pHead, pHead->next); // 構造函數時就已經指定結點的前驅和后繼結點了
// 這里第一個結點的時候需要判斷空指針
if (pHead->next == NULL)
pHead->prev = pNode;
else
pHead->next->prev = pNode;
pHead->next = pNode;
count++;
return 0;
}
// 將結點追加到鏈表的末尾
template<class T>
int DoubleLink<T>::appendLast(T t)
{
DNode<T>* pNode = new DNode<T>(t, pHead->prev, pHead);
pHead->prev->next = pNode;
pHead->prev = pNode;
count++;
return 0;
}
// 刪除index位置的結點
template<class T>
int DoubleLink<T>::del(int index)
{
DNode<T>* pIndex = getNode(index);
pIndex->next->prev = pIndex->prev;
pIndex->prev->next = pIndex->next;
delete pIndex;
count--;
return 0;
}
// 刪除第一個結點
template<class T>
int DoubleLink<T>::delFirst()
{
return del(0);
}
// 刪除最后一個結點
template<class T>
int DoubleLink<T>::delLast()
{
return del(count - 1);
}
#endif // !_LINK_LIST_H
雙向鏈表測試文件(LinkList.cpp)
#include "pch.h"
#include <iostream>
#include <string>
#include "LinkList.h"
using namespace std;
// 雙向鏈表操作int數據
void intTest(){
int intArr[4] = { 10,20,30,40 };
cout << "\n -------------intTest------------" << endl;
// 創建雙向鏈表
DoubleLink<int>* pDlinkList = new DoubleLink<int>();
pDlinkList->insert(0, 20);
pDlinkList->appendLast(10);
pDlinkList->insertFirst(30);
pDlinkList->insert(1, 40);
pDlinkList->delFirst();
// 雙向鏈表是否為空
cout << "isEmpty= " << pDlinkList->isEmpty() << endl;
// 雙向鏈表的長度
cout << "size=" << pDlinkList->size() << endl;
// 打印雙向鏈表的全部數據
int length = pDlinkList->size();
for (int i = 0; i < length; i++)
{
cout << "pDlinkList(" << i << ")=" << pDlinkList->get(i) << endl;
}
}
void stringTest()
{
string sArr[4] = { "ten","tewnty","thirty","forty" };
cout << "\n------------stringTest-----------" << endl;
DoubleLink<string>* pDlinkList = new DoubleLink<string>();
pDlinkList->insert(0, sArr[1]);
pDlinkList->appendLast(sArr[0]);
pDlinkList->insertFirst(sArr[2]);
// 雙向鏈表是否為空
cout << "isEmpty()=" << pDlinkList->isEmpty() << endl;
// 雙向鏈表的長度
cout << "size=" << pDlinkList->size() << endl;
// 打印雙向鏈表的全部數據
int length = pDlinkList->size();
for (int i = 0; i < length; i++)
{
cout << "pDlinkList(" << i << ")=" << pDlinkList->get(i) << endl;
}
}
struct stu
{
int id;
char name[20];
};
static stu stuArr[] =
{
{10,"one"},
{20,"two"},
{30,"three"},
{40,"four"}
};
#define ARR_STU_SIZE ((sizeof(stuArr)) / (sizeof(stuArr[0])))
void objectTest()
{
cout << "\n------------stringTest-----------" << endl;
DoubleLink<stu>* pDlinkList = new DoubleLink<stu>();
pDlinkList->insert(0, stuArr[1]);
pDlinkList->appendLast(stuArr[0]);
pDlinkList->insertFirst(stuArr[2]);
// 雙向鏈表是否為空
cout << "isEmpty()=" << pDlinkList->isEmpty() << endl;
// 雙向鏈表的長度
cout << "size=" << pDlinkList->size() << endl;
// 打印雙向鏈表的全部數據
struct stu p;
int length = pDlinkList->size();
for (int i = 0; i < length; i++)
{
p = pDlinkList->get(i);
cout << "pDlinkList(" << i << ")=[" << p.id <<" ," << p.name<<"]" << endl;
}
}
int main()
{
intTest();
stringTest();
objectTest();
return 0;
}
此文實現參考,侵權必刪,大佬的有一些錯誤,經過測試已經修改。