双向链表原理
简单来说就是单链表加上一个指向前一个元素的指针,更方便的访问节点前一个元素
结构体定义
typedef struct LinkList
{
int data;
struct LinkList* next;
struct LinkList* prev;
}LinkList, LinkNode;
双向链表代码实现
初始化
//初始化
bool initLink(LinkList*& L)
{
L = new LinkList;
if (!L) return false;
L->data = 0;
L->next = NULL;
L->prev = NULL;
return true;
}
遍历和销毁
对于双向链表的遍历,可以更方便的实现逆向遍历输出
//遍历
void printLink(LinkList*& L)
{
if (!L) return;
cout << "正向输出" << endl;
LinkNode* p = L;
while (p->next)
{
cout << p->next->data << " ";
p = p->next;
}
cout << endl;
cout << "逆向输出" << endl;
while (p->prev)
{
cout << p->data << " ";
p = p->prev;
}
cout << endl;
}
//销毁
void destroy(LinkList*& L)
{
if (!L) return;
LinkNode* p = L;
while (p)
{
L = L->next;
delete p;
p = L;
}
}
插入
前插法
对于前插法要注意一点的是,当链表为空的时候要分情况考虑,防止出现访问空指针的情况
bool insert_front(LinkList*& L, int val)
{
if (!L) return false;
LinkNode* node = new LinkNode;
node->data = val;
if (L->next == NULL)
{
node->next = L->next;
node->prev = L;
L->next = node;
}
else
{
node->prev = L;
node->next = L->next;
node->next->prev = node;
L->next = node;
}
return true;
}
尾插法
尾插法很简单,用一个指针指向链表尾部进行插入就好
bool insert_back(LinkList*& L, int val)
{
if (!L) return false;
LinkNode* node = new LinkNode;
node->data = val;
node->next = NULL;
LinkNode* p = L;
while (p->next)
{
p = p->next;
}
node->prev = p;
p->next = node;
return true;
}
任意位置插入
对于任意位置的插入,这里不能对于一个空的链表进行插入
对于在任意位置插入操作要注意指针p指向的到底是哪个节点,建议画图理解(这里本人很容易出错)
bool insertLink(LinkList*& L, int index, int val)
{
if (!L || !L->next || index < 1) return false;
LinkNode* p = L;
int j = 0;
while (p && j < index)
{
p = p->next;
j++;
}
if (!p)
{
cout << "不存在节点" << endl;
return false;
}
LinkNode* node = new LinkNode;
node->data = val;
node->next = p;
node->prev = p->prev;
p->prev->next = node;
p->prev = node;
return true;
}
删除
bool deleteLink(LinkList*& L, int i)
{
if (!L || i < 1) return false;
int j = 0;
LinkNode* p = L;
while (p && j < i)
{
p = p->next;
j++;
}
if (!p) return false;
p->next->prev = p->prev;
p->prev->next = p->next;
delete p;
return true;
}
查找
bool findLink(LinkList*& L, int val)
{
if (!L) return false;
LinkNode* p = L->next;
while (p)
{
if (p->data == val)
{
return true;
}
p = p->next;
}
return false;
}
完整代码(简单测试)
#include <iostream>
using namespace std;
typedef struct LinkList
{
int data;
struct LinkList* next;
struct LinkList* prev;
}LinkList, LinkNode;
//初始化
bool initLink(LinkList*& L)
{
L = new LinkList;
if (!L) return false;
L->data = 0;
L->next = NULL;
L->prev = NULL;
return true;
}
//遍历
void printLink(LinkList*& L)
{
if (!L) return;
cout << "正向输出" << endl;
LinkNode* p = L;
while (p->next)
{
cout << p->next->data << " ";
p = p->next;
}
cout << endl;
cout << "逆向输出" << endl;
while (p->prev)
{
cout << p->data << " ";
p = p->prev;
}
cout << endl;
}
//销毁
void destroy(LinkList*& L)
{
if (!L) return;
LinkNode* p = L;
while (p)
{
L = L->next;
delete p;
p = L;
}
}
//前插法
bool insert_front(LinkList*& L, int val)
{
if (!L) return false;
LinkNode* node = new LinkNode;
node->data = val;
if (L->next == NULL)
{
node->next = L->next;
node->prev = L;
L->next = node;
}
else
{
node->prev = L;
node->next = L->next;
node->next->prev = node;
L->next = node;
}
return true;
}
//尾插法
bool insert_back(LinkList*& L, int val)
{
if (!L) return false;
LinkNode* node = new LinkNode;
node->data = val;
node->next = NULL;
LinkNode* p = L;
while (p->next)
{
p = p->next;
}
node->prev = p;
p->next = node;
return true;
}
//插入
bool insertLink(LinkList*& L, int index, int val)
{
if (!L || !L->next || index < 1) return false;
LinkNode* p = L;
int j = 0;
while (p && j < index)
{
p = p->next;
j++;
}
if (!p)
{
cout << "不存在节点" << endl;
return false;
}
LinkNode* node = new LinkNode;
node->data = val;
node->next = p;
node->prev = p->prev;
p->prev->next = node;
p->prev = node;
return true;
}
//删除
bool deleteLink(LinkList*& L, int i)
{
if (!L || i < 1) return false;
int j = 0;
LinkNode* p = L;
while (p && j < i)
{
p = p->next;
j++;
}
if (!p) return false;
p->next->prev = p->prev;
p->prev->next = p->next;
delete p;
return true;
}
//查找
bool findLink(LinkList*& L, int val)
{
if (!L) return false;
LinkNode* p = L->next;
while (p)
{
if (p->data == val)
{
return true;
}
p = p->next;
}
return false;
}
int main(void)
{
LinkList* l;
initLink(l);
cout << "前插法" << endl;
insert_front(l, 1);
insert_front(l, 2);
insert_front(l, 3);
printLink(l);
cout << "尾插法" << endl;
insert_back(l, 5);
insert_back(l, 6);
insert_back(l, 7);
printLink(l);
cout << "第四个位置插入4" << endl;
insertLink(l, 4, 4);
printLink(l);
cout << "删除第四个位置" << endl;
deleteLink(l, 4);
printLink(l);
cout << "查找5" << endl;
if (findLink(l, 5))
{
cout << "找到了" << endl;
}
else
{
cout << "找不到" << endl;
}
cout << "查找4" << endl;
if (findLink(l, 4))
{
cout << "找到了" << endl;
}
else
{
cout << "找不到" << endl;
}
destroy(l);
return 0;
}