您的位置:首页 > 理论基础 > 数据结构算法

C++数据结构之双链表实现

2018-09-18 17:41 393 查看

双链表模版实现

功能:

1 创建

2 遍历

3 反转

4 插入

5 取值

6 修改某位置值

7 删除

。。。。

xxx.h

节点定义:

#include <iostream>

 

template <typename T>

struct duLink {

T data;

struct duLink *next;

struct duLink *prev;

duLink(){this->prev = nullptr; this->next = nullptr;}

duLink(const T data){this->data = data; this->prev = nullptr; this->next = nullptr;}

duLink(const T &data, struct duLink *prev, struct duLink *next){this->data = data; this->prev = prev, this->next = next;}

duLink(const struct duLink &node){this->data = node.data; this->prev = node.prev; this->next = node.next;}

};

别名定义:

template <typename T>

using duLinkNode = struct duLink<T>;

链表定义:

template <typename T>

class duLinkList {

private:

duLinkNode <T> *head;

public:

duLinkList() {this->head = nullptr;}

duLinkList(const T &data){this->head = new duLinkNode<T>(data);}

duLinkList(const duLinkNode<T> &node){this->head = new duLinkNode<T>(node);}

duLinkList(const duLinkList<T> &list);

~duLinkList();

void clear();

bool isEmpty();

int length();

void show();

void reverse_show();

void reverse();

bool GetHeadNode(T &data);

bool GetTailNode(T &data);

bool GetPosNode(T &data, int pos);

bool SetHeadNode(const T &data);

bool SetTailNode(const T &data);

bool SetPosNode(const T &data, int pos);

bool InsertHeadNode(const T &data);

bool AppendTailNode(const T &data);

bool InsertPosNode(const T &data, int pos);

bool PopHeadNode(T &data);

bool PopTailNode(T &data);

bool PopNodePos(T &data, int pos);

bool DelHeadNode();

bool DelTailNode();

bool DelPosNode(int pos);

/*

function:find the position of node which equal to data in the list

data:the data which compared

return value:the position which equal to data;

this function need edit accoding the T

*/

int GetFirstNodePos(T &data);

/*

function:find the position of node which equal to data in the list

data:the data which compared

pos:the array which storage the position

return value:the num which equal to data;

this function need edit accoding the T

*/

int GetNodePos(T &data,int *pos);

};

成员函数实现:

#include "dulist.h"

#include "print.h"

#define LOG_TAG "DULIST"

复制构造函数:

template <typename T>

duLinkList<T>::duLinkList(const duLinkList<T> &list)

{

duLinkNode<T> *p1 = list.head, *p2, *p3;

 

if(p1 == nullptr)

{

ULOGW("empty list");

this->head = nullptr;

return;

}

this->head = new duLinkNode<T>(*p1);

p2 = this->head;

p1 = p1->next;

while(p1 != nullptr){

p3 = new duLinkNode<T>(*p1);

p2->next = p3;

p3->prev = p2;

p2 = p3;

p1 = p1->next;

}

p2->next = nullptr;

}

析构函数:删除链表中所有元素

template <typename T>

duLinkList<T>::~duLinkList()

{

duLinkNode<T> *p = this->head;

while(p != nullptr){

p = p->next;

delete this->head;

this->head = p;

}

}

清除函数:删除链表中所有函数,与析构函数操作几乎一致

template <typename T>

void duLinkList<T>::clear()

{

if(this->head == nullptr)

return ;

duLinkNode<T> *p = this->head;

while(p != nullptr){

p = p->next;

delete this->head;

this->head = p;

}

}

function:判断是否为空

template <typename T>

bool duLinkList<T>::isEmpty()

{

if(this->head != nullptr)

return true;

return false;

}

function:取链表长度

template <typename T>

int duLinkList<T>::length()

{

int len = 0;

duLinkNode<T> *p = this->head;

while(p != nullptr){

len++;

p = p->next;

}

return len;

}

function:遍历链表

template <typename T>

void duLinkList<T>::show()

{

std::cout<<"The sum of data in the list is: "<<length()<<std::endl;

std::cout<<"detail:";

duLinkNode<T> *p = this->head;

while(p != nullptr){

std::cout<<p->data<<" ";

p= p->next;

}

std::cout<<std::endl;

}

 

template <typename T>

void duLinkList<T>::reverse_show()

{

std::cout<<"The sum of data in the list is: "<<length()<<std::endl;

std::cout<<"reverse detail:";

duLinkNode<T> *p = this->head;

while(p->next != nullptr){

p= p->next;

}

while(p != nullptr){

std::cout<<p->data<<" ";

p= p->prev;

}

std::cout<<std::endl;

}

function:反转链表(只需要修改各节点指向即可

template <typename T>

void duLinkList<T>::reverse()

{

if(this->head == nullptr){

ULOGI("empty list");

return ;

}

duLinkNode<T> *p1 = this->head, *p2 = this->head->next, *p3;

p1->next = nullptr;

while(p2 != nullptr){

p1->prev = p2;

p3 = p2->next;

p2->next = p1;

p1 = p2;

p2 = p3;

}

this->head = p1;

}

function:获取表头、表尾即pos处节点

template <typename T>

bool duLinkList<T>::GetHeadNode(T &data)

{

return GetPosNode(data, 0);

}

 

template <typename T>

bool duLinkList<T>::GetTailNode(T &data)

{

int len = length();

return GetPosNode(data, len-1);

}

 

template <typename T>

bool duLinkList<T>::GetPosNode(T &data, int pos)

{

if(this->head == nullptr){

ULOGW("empty list");

return false;

}

if(pos < 0){

ULOGW("invalue pos");

return false;

}

int pos1 = pos;

duLinkNode <T> *p = this->head;

while((p->next!=nullptr) && pos){

--pos;

p = p->next;

}

if(pos != 0){

ULOGW("no ", pos1, "th node");

return false;

}

data = p->data;

return true;

}

 

template <typename T>

bool duLinkList<T>::SetHeadNode(const T &data)

{

return SetPosNode(data, 0);

}

 

template <typename T>

bool duLinkList<T>::SetTailNode(const T &data)

{

int len = length();

return SetPosNode(data, len-1);

}

 

template <typename T>

bool duLinkList<T>::SetPosNode(const T &data, int pos)

{

if(this->head == nullptr){

ULOGW("empty list");

return false;

}

if(pos < 0){

ULOGW("invalue pos");

return false;

}

int pos1 = pos;

duLinkNode <T> *p = this->head;

while((p->next!=nullptr) && pos){

--pos;

p = p->next;

}

if(pos != 0){

ULOGW("no ", pos1, "th node");

return false;

}

p->data = data;

 

return true;

}

function:在表头、表尾或pos处插入新节点

template <typename T>

bool duLinkList<T>::InsertHeadNode(const T &data)

{

return InsertPosNode(data, 0);

}

 

template <typename T>

bool duLinkList<T>::AppendTailNode(const T &data)

{

return InsertPosNode(data, length());

}

 

template <typename T>

bool duLinkList<T>::InsertPosNode(const T &data, int pos)

{

 

if(pos < 0){

ULOGW("invalue pos");

return false;

}

duLinkNode <T> *p1 = new duLinkNode<T>(data);

if(pos == 0){

p1->next = this->head;

p1->prev = nullptr;

this->head->prev = p1;

this->head = p1;

return true;

}

duLinkNode <T> *p2 = this->head;

while((p2->next!=nullptr) && (--pos)){

p2 = p2->next;

}

//if pos != 0, then append the node after the tail

p1->next = p2->next;

p1->prev = p2;

p2->next = p1;

if(pos != 0)

return true;

p2 = p1->next;

p2->prev = p1;

 

return true;

}

function:取出表头、表尾或pos处的节点,并删除表中的节点

template <typename T>

bool duLinkList<T>::PopHeadNode(T &data)

{

return PopNodePos(data, 0);

}

template <typename T>

bool duLinkList<T>::PopTailNode(T &data)

{

return PopNodePos(data, length()-1);

}

 

template <typename T>

bool duLinkList<T>::PopNodePos(T &data, int pos)

{

if(this->head == nullptr){

ULOGW("empty list");

return false;

}

if(pos < 0){

ULOGW("invalue pos");

return false;

}

int pos1 = pos;

duLinkNode <T> *p1 = this->head;

if(pos == 0){

data = this->head->data;

this->head = this->head->next;

this->head->prev = nullptr;

delete p1;

return true;

}

duLinkNode <T> *p2;

while((p1->next!=nullptr) && pos){

--pos;

p1 = p1->next;

}

if(pos != 0){ //if pos != 0, then cannot find the pos value

ULOGW("no ", pos1, "th node");

return false;

}

data = p1->data;

p2 = p1->prev;

p2->next = p1->next;

delete p1;

if(p2->next != nullptr){

p1 = p2->next;

p1->prev = p2;

}

 

return true;

}

function:删除表头、表尾或pos处的值

template <typename T>

bool duLinkList<T>::DelHeadNode()

{

return DelPosNode(0);

}

 

template <typename T>

bool duLinkList<T>::DelTailNode()

{

return DelPosNode(length()-1);

}

 

template <typename T>

bool duLinkList<T>::DelPosNode(int pos)

{

if(this->head == nullptr){

ULOGW("empty list");

return false;

}

if(pos < 0){

ULOGW("invalue pos");

return false;

}

int pos1 = pos;

duLinkNode <T> *p1 = this->head;

if(pos == 0){

this->head = this->head->next;

this->head->prev = nullptr;

delete p1;

return true;

}

duLinkNode <T> *p2 = this->head;

while((p1->next!=nullptr) && pos){

--pos;

p1 = p1->next;

}

if(pos != 0){ //if pos != 0, then cannot find the pos value

ULOGW("no ", pos1, "th node");

return false;

}

p2 = p1->prev;

p2->next = p1->next;

delete p1;

p1 = p2->next;

if(p2->next != nullptr){

p1 = p2->next;

p1->prev = p2;

}

return true;

}

源码下载地址(包含测试代码):https://download.csdn.net/download/zhouchao_0321/10674661

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: