# 双向链表的实现

```#include <iostream>
#include <stdlib.h>

using namespace std;

typedef int Elemtype;
typedef struct lNode {
Elemtype data;
struct lNode *prior;
struct lNode *next;

//函数声明

int main()
{
cout << "输入要建立链表的长度：" << endl;
int length;
cin >> length;
cout << "输入要建立链表的数据值，共" << length << "个数 ：" << endl;

//测试插入
void insertNode(LinkedList , int , Elemtype);
int iInsertIndex;
cout << "请输入要插入的地址：" << endl;
cin >> iInsertIndex;
Elemtype eInsertValue;
cout << "请输入要插入的值：" << endl;
cin >> eInsertValue;

//测试删除
int iDeleteIndex;
cout << "请输入要删除的地址值，不小于1，不大于" << getLength(lHead) << "：" << endl;
cin >> iDeleteIndex;

//测试修改
/*void modifyNode(LinkedList , int , Elemtype);
cout << "输入要更新结点的地址值，不小于1，不大于" << getLength(lHead) << "：" << endl;
int iModifyIndex;
cin >> iModifyIndex;
cout << "输入要更新的结点值：" << endl;
Elemtype iModifyValue;
cin >> iModifyValue;

//测试查找
Elemtype eSearchResult;
cout << "输入要查找的结点的地址值：" << endl;
int iSearchIndex;
cin >> iSearchIndex;
cout << "链表中地址值为" << iSearchIndex << "的结点值为：" << endl;
cout << eSearchResult << endl;*/

cout << "顺序输出链表里面的结点值：" << endl;
//cout << "逆序输出链表里面的值：" << endl;

//获得链表长度测试
return 0;
}

//初始化结点
}

//链表展示
cout << head->data << " ";
cout << lCurr->data << " ";
lCurr = lCurr->next;
}
cout << endl;
}

cout << lCurr->data << " ";
lCurr = lCurr->prior;
}
}

//尾插法初始化链表
cin >> lCurr->data;
for(int i = 1; i < iSize; i++) {
cin >> lNew->data;
lCurr->next = lNew;
lNew->prior = lCurr;
lCurr = lNew;
}
}

int length = 1;
length++;
lCurr = lCurr->next;
}
return length;
}

//向链表中插入相应的数据
//判断插入地址的合法性
if(index <= 0 || index > getLength(head) + 1) {
cout << "插入的地址不存在！" << endl;
return;
}
//移动到要插入的位置
for(int i = 1; i < index; i++) {
lCurr = lCurr->next;
}
//生成新的结点并插入
lNew->data = value;
lNew->prior = lCurr->prior;
lCurr->prior->next = lNew;
lNew->next = lCurr;
lCurr->prior = lNew;
}

//删除链表中的元素
//判断插入地址的合法性
if(index <= 0 || index > getLength(head)) {
cout << "要删除的地址不存在！" << endl;
return lResult;
}
//移动到要插入的位置
for(int i = 1; i < index; i++) {
lCurr = lCurr->next;
}
lCurr->prior->next = lCurr->next;
lCurr->next->prior = lCurr->prior;
lResult = lCurr->next;
free(lCurr);
return lResult;
}

//修改链表中的结点值
//判断插入地址的合法性
if(index <= 0 || index > getLength(head)) {
cout << "要修改的地址不存在！" << endl;
return;
}
//移动到要插入的位置
for(int i = 1; i < index; i++) {
}
}

//查找结点值
//判断插入地址的合法性
if(index <= 0 || index > getLength(head)) {
cout << "查找的结点不存在！" << endl;
return -1;
}
//移动到要插入的位置
for(int i = 1; i < index; i++) {
}
}
```

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊