1、double_link_list.h
#ifndef _double_link_list_h
#define _double_link_list_h
#include "common.h"
typedef int elem_type;
struct node;
typedef struct node node;
typedef node * node_ptr;
typedef node * double_list;
struct node
{
elem_type data;
node_ptr prev;
node_ptr next;
};
status_code init_dblist( double_list * lst );
status_code clear_dblist( double_list lst );
status_code free_dblist( double_list * lst );
int is_dblist_empty( double_list lst );
int double_list_size( double_list lst );
void traverse_dblist( double_list lst );
status_code insert_elem_dblist( double_list * lst, int i, elem_type e );
status_code remove_elem_dblist( double_list * lst, int i, elem_type * e );
status_code get_elem_dblist( double_list lst, int i, elem_type * e );
int locate_elem_dblist( double_list lst, elem_type e );
node_ptr get_node_ptr( double_list lst, int i );
#endif
2、double_link_list.c
#include "dbl_circ_link_list.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
status_code init_dblist( double_list * lst )
{
*lst = ( node_ptr )malloc( sizeof( node ) );
assert( *lst != NULL );
(*lst)->data = 0;
(*lst)->prev = (*lst)->next = *lst;
return Success;
}
status_code clear_dblist( double_list lst )
{
node_ptr p = lst->next;
while( p != lst )
{
lst->next = p->next;
p->next->prev = lst;
free( p );
p = lst->next;
}
return Success;
}
status_code free_dblist( double_list * lst )
{
clear_dblist( *lst );
free( *lst );
*lst = NULL;
return Success;
}
int double_list_size( double_list lst )
{
int len = 0;
node_ptr p = lst->next;
while( p != lst )
{
++len; p = p->next;
}
return len;
}
int is_dblist_empty( double_list lst )
{
return lst->next == lst;
}
status_code insert_elem_dblist( double_list * lst, int i, elem_type e )
{
int j = 0, flag = 1;
node_ptr h = *lst, p = h, s = NULL;
while( j < i - 1 && ( flag || p != h ) )
{
flag = 0; p = p->next; ++j;
}
if( j > i - 1 || ( !flag && p == h ) )
return RangeError;
s = ( node_ptr )malloc( sizeof( node ) );
assert( s != NULL );
s->data = e;
s->next = p->next;
p->next = s;
s->prev = p;
s->next->prev = s;
return Success;
}
status_code remove_elem_dblist( double_list * lst, int i, elem_type * e )
{
int j = 0, flag = 1;
node_ptr h = *lst, p = h, s = NULL;
while( j < i - 1 && ( flag || p->next != h ) )
{
flag = 0; ++j; p = p->next;
}
if( j > i - 1 || ( !flag && p->next == h ) )
return RangeError;
s = p->next;
*e = s->data;
p->next = s->next;
s->next->prev = p;
free( s );
return Success;
}
status_code get_elem_dblist( double_list lst, int i, elem_type * e )
{
int j = 1;
node_ptr p = lst->next;
while( p != lst && j < i )
{
++j; p = p->next;
}
if( j > i || p == lst )
return RangeError;
*e = p->data;
return Success;
}
int locate_elem_dblist( double_list lst, elem_type e )
{
int pos = 1;
node_ptr p = lst->next;
while( p != lst && p->data != e )
{
p = p->next; ++pos;
}
return p == lst ? -1 : pos;
}
void traverse_dblist( double_list lst )
{
node_ptr p = lst->next;
while( p != lst )
{
printf( "%d ", p->data );
p = p->next;
}
putchar( '\n' );
}
node_ptr get_node_ptr( double_list lst, int i )
{
node_ptr p = lst;
int j = 0;
int flag = 1;
while( j < i && ( flag || p != lst ) )
{
flag = 0; p = p->next;
}
return ( j > i || ( !flag && p == lst ) ) ? NULL : p;
}