实现代码如下:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int ElemType, Status;
typedef struct DuLNode //定义双向链表的节点结构
{
ElemType data;
struct DuLNode* prev; //指向前一个节点
struct DuLNode* next; //指向下一个节点
}DuLNode, *DuLinkList;
typedef struct DuListInfo //双向链表信息
{
DuLinkList head; //头结点
DuLinkList tail; //尾结点
DuLNode* pCurNode; //当前节点指针位置
int length; //双向链表长度
}DuListInfo;
Status InitList(DuListInfo& L) //初始化双向链表
{
L.head = (DuLNode*)malloc(sizeof(DuLNode));
if (L.head == NULL) exit(0);
L.head->next = NULL;
L.head->prev = NULL;
L.tail = L.head;
L.pCurNode = L.head;
L.length = 0;
return OK;
}
Status DestroyList(DuListInfo& L) //销毁双向链表
{
DuLNode* p = L.head;
DuLNode* q;
while (p)
{
q = p;
p = p->next;
free(q);
}
L.head = NULL;
L.length = 0;
L.pCurNode = NULL;
L.tail = NULL;
return OK;
}
Status ListEmpty(DuListInfo L) //双向链表判空
{
if (L.length == 0) return OK;
return ERROR;
}
Status GetElem(DuListInfo L, int i, ElemType& e) //双向链表元素获取
{
if (L.length == 0) return ERROR;
if (i < 1 || i > L.length) return ERROR;
DuLNode* p = L.head;
int j = 0;
while (j < i)
{
p = p->next;
j++;
}
e = p->data;
return OK;
}
Status LocateElem(DuListInfo L, ElemType e) //双向链表元素查找
{
if (L.length == 0) return ERROR;
int i = 0;
DuLNode* p = L.head;
while (p)
{
p = p->next;
i++;
if (p->data == e) return i;
}
return ERROR;
}
Status PriorElem(DuListInfo L, ElemType cur_e, ElemType& pre_e) //双向链表元素前驱
{
if (L.length == 0) return ERROR;
DuLNode* p = L.head;
while (p)
{
p = p->next;
if (p->data == cur_e)
{
pre_e = p->prev->data;
return OK;
}
}
return ERROR;
}
Status NextElem(DuListInfo L, ElemType cur_e, ElemType &next_e) //双向链表元素后继
{
if (L.length == 0) return ERROR;
DuLNode* p = L.head;
while (p)
{
p = p->next;
if (p->data == cur_e)
{
next_e = p->next->data;
return OK;
}
}
return ERROR;
}
Status ListTraverse(DuListInfo L) //遍历双向链表
{
if (L.length == 0) return ERROR;
DuLNode* p = L.head->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status SetElem(DuListInfo& L, int i, ElemType& e) //双链表元素替换
{
if (L.length == 0) return ERROR;
if (i < 1 || i > L.length) return ERROR;
int j = 0;
DuLNode* p = L.head;
while (j < i)
{
p = p->next;
j++;
}
ElemType temp = e;
e = p->data;
p->data = temp;
return OK;
}
Status InsertElem(DuListInfo& L, int i, ElemType e) //双向链表插入元素
{
if (i < 1 || i > L.length + 1) return ERROR;
DuLNode* p = L.head;
if (i == L.length + 1)
{
DuLNode* q = (DuLNode*)malloc(sizeof(DuLNode));
if (q == NULL) exit(0);
q->data = e;
q->next = NULL;
q->prev = L.tail;
L.tail->next = q;
L.tail = q;
L.length = L.length + 1;
return OK;
}
int j = 0;
while (j < i - 1)
{
p = p->next;
j++;
}
DuLNode* q = (DuLNode*)malloc(sizeof(DuLNode));
if (q == NULL) exit(0);
q->data = e;
q->next = p->next;
q->prev = p;
p->next = q;
q->next->prev = q;
L.length = L.length + 1;
return OK;
}
Status DeleteElem(DuListInfo& L, int i, ElemType& e) //双向链表删除元素
{
if (L.length == 0) return ERROR;
if (i < 1 || i > L.length) return ERROR;
if (i == L.length)
{
e = L.tail->data;
DuLNode* p = L.tail;
L.tail->prev->next = NULL;
L.tail = L.tail->prev;
L.length = L.length - 1;
free(p);
return OK;
}
int j = 0;
DuLNode* p = L.head;
while (j < i)
{
p = p->next;
j++;
}
e = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
free(p);
L.length = L.length - 1;
return OK;
}
Status CreateList(DuListInfo& L) //创建双向链表
{
L.head = (DuLNode*)malloc(sizeof(DuLNode));
if (L.head == NULL) exit(0);
L.head->prev = NULL;
L.head->next = NULL;
L.length = 0;
L.pCurNode = L.head;
L.tail = L.head;
int n;
printf("双向链表长度:");
scanf_s("%d", &n);
int i = 0;
printf("链表元素:");
while (i < n)
{
DuLNode* q = L.pCurNode;
DuLNode* p = (DuLNode*)malloc(sizeof(DuLNode));
if (p == NULL) exit(0);
ElemType e;
scanf_s("%d", &e);
p->data = e;
p->next = NULL;
L.pCurNode->next = p;
L.pCurNode = p;
L.pCurNode->prev = q;
L.tail = p;
L.length = L.length + 1;
i++;
}
return OK;
}
int main()
{
DuListInfo L;
CreateList(L);
ListTraverse(L);
InsertElem(L, 1, 0);
ListTraverse(L);
InsertElem(L, 7, 6);
ListTraverse(L);
ElemType e;
DeleteElem(L, 7, e);
ListTraverse(L);
PriorElem(L, 1, e);
printf("1的前驱元素:%d\n", e);
NextElem(L, 1, e);
printf("1的后继元素:%d\n", e);
e = 0;
SetElem(L, 2, e);
ListTraverse(L);
return 0;
}
运行结果如下: