双向链表 只是 比 单链表 多了 一个 指向 前驱的 指针,在插入 和 删除 元素的 时候 得多处理一些。其余 没什么 区别。
而循环链表 的 尾指针 不再 指向 NULL,而是 指向 头指针,这样 既可以循环遍历,又节省 存储空间 。
每种链表 都有 好处,至于 如何 取舍,得看需求。
下面 奉上 双向链表的实现代码:
// DoubleLinkList.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
typedef int Elelment;
enum E_STATE
{
E_STATE_ERROR = 0,
E_STATE_Ok = 1,
};
//双链表
struct DoubleList
{
Elelment data;
DoubleList * pre;
DoubleList * next;
};
E_STATE listInit(DoubleList & list){
list.next = NULL;
list.pre = NULL;
return E_STATE_Ok;
}
E_STATE listDestory(DoubleList & list){
DoubleList * next = list.next;
while (next)
{
DoubleList * freeNode = next;
next = next->next;
free(freeNode);
}
list.next = NULL;
return E_STATE_Ok;
}
bool listEmpty(DoubleList list){
return list.next == NULL ? true : false;
}
int listLen(DoubleList list){
int len = 0;
DoubleList * next = list.next;
while (next)
{
next = next->next;
len ++;
}
return len;
}
E_STATE listGetElement(DoubleList list,int index,DoubleList & listElement){
int times = 1;
DoubleList * next = list.next;
while (next && times < index)
{
next = next->next;
times ++;
}
if (next == NULL || times > index)
{
return E_STATE_ERROR;
}
listElement = *next;
return E_STATE_Ok;
}
DoubleList * listLocate(DoubleList list,Elelment e){
DoubleList * next = list.next;
while (next)
{
if (next->data == e)
{
return next;
}
next = next->next;
}
return NULL;
}
DoubleList * listPreElement(DoubleList list,Elelment e){
DoubleList * pLocate = listLocate(list,e);
return pLocate ? pLocate->pre:NULL;
}
DoubleList * listNextElement(DoubleList list,Elelment e){
DoubleList * pLocate = listLocate(list,e);
return pLocate ? pLocate->next : NULL;
}
E_STATE listInsert(DoubleList & list,int index ,Elelment e){
DoubleList * pre = &list;//前驱
int times = 0;
while (pre && times < index - 1)
{
pre = pre ->next;
times ++;
}
if (pre == NULL || times > index - 1)
{
return E_STATE_ERROR;
}
DoubleList * newNode = (DoubleList *)malloc(sizeof(DoubleList));
newNode->pre = pre;
newNode->next = pre->next;
newNode->data = e;
if (pre->next)//必须后继不为空
{
//后继节点的前驱
pre->next->pre = newNode;
}
pre->next = newNode;
return E_STATE_Ok;
}
E_STATE listDelete(DoubleList & list,int index){
DoubleList * next = list.next;
int times = 1;
while (times < index && next)
{
next = next->next;
times++;
}
if (times > index || next == NULL)
{
return E_STATE_ERROR;
}
next->pre->next = next->next;
if (next->next)
{
next->next->pre = next->pre;
}
free(next);
return E_STATE_Ok;
}
void listTraverse(DoubleList list){
DoubleList * next = list.next;
printf("-------------------\n");
while (next)
{
printf("%d\n",next->data);
next = next->next;
}
printf("-------------------\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
DoubleList list;
//初始化
listInit(list);
for (int i = 1; i < 11; i++)
{
listInsert(list,i,i);
}
listTraverse(list);
//插入,删除测试
listDelete(list,1);
listDelete(list,listLen(list));
listInsert(list,listLen(list),111);
listInsert(list,listLen(list)+1,222);
listTraverse(list);
//其他函数测试.
printf("表长为:%d\n",listLen(list));
DoubleList eList;
listGetElement(list,5,eList);
printf("第五个节点值为:%d\n",eList.data);
printf("前驱值为:%d\n",eList.pre->data);
printf("后继值为:%d\n",eList.next->data);
return 0;
}
原文地址:http://blog.csdn.net/fuming0210sc/article/details/43835561