数据结构-双向链表
本文内容
- 初始化双向链表-创建头节点
- 创建双向链表
- 销毁双向链表
- 双向链表长度
- 按值检索双向链表
- 按索引检索双向链表
- 插入双向链表
- 删除双向链表
- 合并双向链表
- 输出双向链表
dulist.h 声明文件
#ifndef _DuList_H
#define _DuList_H
struct DuLNode;
typedef struct DuLNode *duLinkList;
typedef duLinkList DuLinkList;
typedef duLinkList DuLinkPos;
DuLinkList InitList_DuL();
DuLinkList CreateAutoList_DuL01( int length );
DuLinkList CreateAutoList_DuL02( int length );
DuLinkList CreateHandList_DuL( int length );
void DestroyList_DuL( DuLinkList L );
int LengthList_DuL( DuLinkList L );
int GetList_DuL( DuLinkList L, int index );
int LocateList_DuL( DuLinkList L, int e );
DuLinkPos FindList_DuL( DuLinkList L, int e );
void InsertList_DuL( DuLinkList L, int index, int e );
void DeleteList_DuL( DuLinkList L, int index, int *e );
void MergeList_DuL( DuLinkList La, DuLinkList Lb );
void ConvertList_DuL( DuLinkList L );
void PrintList_DuL( DuLinkList L);
#endif
dulist.c 实现文件
#include "dulinklist.h"
#include <stdio.h>
#include <stdlib.h>
struct DuLNode
{
int Element;
duLinkList Prior;
duLinkList Next;
};
DuLinkList InitList_DuL()
{
DuLinkList L = (duLinkList)malloc( sizeof( struct DuLNode ) );
L->Element = -1;
L->Prior = NULL;
L->Next = NULL;
return L;
}
DuLinkList CreateAutoList_DuL01( int length )
{
int i;
DuLinkPos n;
DuLinkList L = InitList_DuL();
for(i = 1 ; i<=length ; i++)
{
n = (duLinkList)malloc( sizeof( struct DuLNode ) );
n->Element = i;
n->Prior = L;
n->Next = L->Next;
L->Next = n;
}
return L;
}
DuLinkList CreateAutoList_DuL02( int length )
{
int i;
DuLinkPos n, p;
DuLinkList L = InitList_DuL();
p = L;
for(i = 1 ; i<=length ; i++)
{
n = (duLinkList)malloc( sizeof( struct DuLNode ) );
n->Element = i;
p->Next = n;
p = n;
}
p->Next = NULL;
return L;
}
DuLinkList CreateHandList_DuL( int length )
{
int i;
DuLinkPos n;
DuLinkList L = InitList_DuL();
for(i = length ; i>0 ; --i)
{
n=(duLinkList)malloc( sizeof( struct DuLNode ) );
scanf("%d",& n->Element);
n->Next = L->Next;
L->Next = n;
}
return L;
}
void DestroyList_DuL( DuLinkList L )
{
DuLinkPos P, q;
P = L->Next;
L->Next = NULL;
while( P != NULL )
{
q = P->Next;
free( P );
P = q;
}
free(L);
}
int LengthList_DuL( DuLinkList L )
{
int len = 0;
DuLinkPos p;
p = L->Next;
while( p )
{
len++;
p = p->Next;
}
return len;
}
int GetList_DuL( DuLinkList L, int index )
{
int count = 0;
DuLinkPos p;
p = L;
while( p->Next && count < index)
{
p = p->Next;
count++;
}
return p->Element;
}
int LocateList_DuL( DuLinkList L, int e )
{
int index = 1;
DuLinkPos p;
p = L->Next;
while( p )
{
if( p->Element == e ) break;
p = p->Next;
index++;
}
if(p == NULL) return 0;
else return index;
}
DuLinkPos FindList_DuL( DuLinkList L, int e )
{
DuLinkPos P;
P = L->Next;
while( P && P->Element != e ) P = P->Next;
return P;
}
void InsertList_DuL( DuLinkList L, int index, int e )
{
int j = 0;
DuLinkPos p, n;
p = L;
while( p && j < index-1 )
{
p = p->Next;
j++;
}
if( p && j <= index )
{
n = (duLinkList)malloc( sizeof(struct DuLNode) );
n->Element = e;
n->Next = p->Next;
p->Next->Prior = n;
n->Prior = p;
p->Next = n;
}
}
void DeleteList_DuL( DuLinkList L, int index, int *e )
{
int j = 0;
DuLinkPos p, q;
p = L;
while( p->Next && j < index-1 )
{
p = p->Next;
j++;
}
if( p->Next && j <=index )
{
*e = p->Next->Element;
q = p->Next;
p->Next = q->Next;
q->Next->Prior = p;
free(q);
}
}
void MergeList_DuL( DuLinkList La, DuLinkList Lb )
{
DuLinkPos pa, pb, pc;
pa = La->Next;
pb = Lb->Next;
pc = La;
while(pa && pb)
{
if( pa->Element <= pb->Element)
{
pc->Next = pa;
pa->Prior = pc;
pc = pa;
pa = pa->Next;
}
else
{
pc->Next = pb;
pb->Prior = pc;
pc = pb;
pb = pb->Next;
}
}
pc->Next = pa ? pa : pb;
free(Lb);
}
void PrintList_DuL( DuLinkList L)
{
DuLinkPos p;
int index = 0;
p = L->Next;
printf("LENGTH %d, PRINT linkList.../n", LengthList_DuL(L));
while(p)
{
printf("INDEX = %3d, VALUE = %4d/n", index++, p->Element);
p = p->Next;
}
printf("END./n/n");
}