双向链表写的少，总是忘记指定一个结点的前驱。
头文件：

#include <iostream>
using namespace std;

typedef struct Node
{//结点结构
int value;
struct Node *pre,*next;//前驱指针和后继指针

typedef struct List
{//链表结构
int length;

{//构造一个空的链表
list=new List;
list->length=0;
return list;
}

int ListLength(List list)
{
return list.length;
}

bool IsListEmpty(List list)
{
if(list.length==0)
return true;
return false;
}

{//清空链表，并释放每个结点的空间
while(p!=list->tail)
{
q=p->next;
free(p);
p=q;
}
free(p);//释放尾结点
list->length=0;
}

{
ClearList(list);
free(list->tail);
}

{
p=new Node;
p->value=elem;
if(pos<1 || pos>ListLength(*list)+1)
cout<<"The position is invalidate."<<endl;
else
{
if(pos==1)
{//插在表头
if(IsListEmpty(*list))
{
p->next=NULL;
}
else
{
}
}
else if(pos==ListLength(*list)+1)
{//插在表尾
list->tail->next=p;
p->pre=list->tail;
list->tail=p;
p->next=NULL;
}
else
{
for(int i=1;i<pos-1;++i)
{//找到pos-1位置的结点
q=q->next;
}
p->next=q->next;
p->pre=q;
q->next->pre=p;
q->next=p;
}
++list->length;
}
}

{
if(IsListEmpty(*list))
{
cout<<"The list is empty,you can't delete element."<<endl;
}
else
{
if(pos<1 || pos>ListLength(*list))
{//删除位置不正确
cout<<"The position is invalidate."<<endl;
return;//如果条件不成立，就结束
}
else
{
if(pos==1)
{//删除表头
if(ListLength(*list)==1)
{//表长为1
elem=p->value;
free(p);
}
else
{//表长大于1
p->next->pre=NULL;
elem=p->value;
free(p);
}
}
else if(pos==ListLength(*list))
{//删除表尾
list->tail=q->pre;
list->tail->next=NULL;
elem=q->value;
free(q);
}
else
{
for(int i=1;i<pos;++i)
{//找到pos位置的结点
p=p->next;
}
p->pre->next=p->next;
p->next->pre=p->pre;
elem=p->value;
free(p);
}
--list->length;
}
}
}

bool PreElem(List list,int pos,int &elem)
{//判断 pos位置的结点有无前驱，若有则将其值存入elem
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
if(pos==1)
{
cout<<"This position doesn't have a pre_element."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{//找到pos位置处结点
p=p->next;
}
elem=p->pre->value;
return true;
}
}
}

bool NextElem(List list,int pos,int &elem)
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
if(pos==ListLength(list))
{
cout<<"This position doesn't have a next_element."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{//找到pos位置处结点
p=p->next;
}
elem=p->next->value;
return true;
}
}
}

bool CurrentElem(List list,int pos,int &elem)
{
if(pos<1 || pos>ListLength(list))
{
cout<<"The position is invalidate."<<endl;
return false;
}
else
{
for(int i=1;i<pos;++i)
{
p=p->next;
}
elem=p->value;
return true;
}
}

{//如果链表不空，则返回表头元素，用elem接收
if(IsListEmpty(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
return true;
}
}

bool GetTail(List list,int &elem)
{//如果链表不空，则返回表尾元素，用elem接收
if(IsListEmpty(list))
{
cout<<"The list is empty."<<endl;
return false;
}
else
{
elem=list.tail->value;
return true;
}
}
void VisitElemFromList(List list)
{
if(!IsListEmpty(list))
{
while(p)
{
cout<<p->value<<' ';
p=p->next;
}
cout<<endl;
}
}
测试代码：

#include "stdafx.h"
#include <conio.h>
#include "double_direction_list.h"

int _tmain(int argc, _TCHAR* argv[])
{
list=NewList();
if(IsListEmpty(*list))
cout<<"The list is empty."<<endl;
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
InsertElemToList(list,1,3);
InsertElemToList(list,1,5);
InsertElemToList(list,ListLength(*list)+1,8);
VisitElemFromList(*list);
InsertElemToList(list,-1,2);
InsertElemToList(list,9,21);
int elem;
DeleteElemFromList(list,1,elem);
VisitElemFromList(*list);
InsertElemToList(list,1,22);
InsertElemToList(list,1,45);
InsertElemToList(list,4,23);
InsertElemToList(list,ListLength(*list),90);
InsertElemToList(list,ListLength(*list)+1,88);
VisitElemFromList(*list);
int e;
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The tail_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,2,elem);
VisitElemFromList(*list);
DeleteElemFromList(list,ListLength(*list),elem);
VisitElemFromList(*list);
DeleteElemFromList(list,1,elem);
VisitElemFromList(*list);
{
cout<<"The head_elem of the list is:"<<e<<endl;
}
if(GetTail(*list,e))
{
cout<<"The tail_elem of the list is:"<<e<<endl;
}
cout<<"The length of the list is:"<<ListLength(*list)<<endl;
DeleteElemFromList(list,ListLength(*list)+1,elem);
DeleteElemFromList(list,-2,elem);
int temp;
if(PreElem(*list,4,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(PreElem(*list,1,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(PreElem(*list,12,temp))
cout<<"The pre_element of this position is:"<<temp<<endl;
if(NextElem(*list,2,temp))
cout<<"The nex_element of this position is:"<<temp<<endl;
if(NextElem(*list,ListLength(*list),temp))
cout<<"The next_element of this position is:"<<temp<<endl;
if(NextElem(*list,-3,temp))
cout<<"The next_element of this position is:"<<temp<<endl;
if(CurrentElem(*list,2,temp))
cout<<"The current_element of this position is:"<<temp<<endl;
if(CurrentElem(*list,ListLength(*list)+2,temp))
cout<<"The current_element of this position is:"<<temp<<endl;
ClearList(list);
DeleteElemFromList(list,1,temp);
getch();
return 0;
}