双循环链表的实现(ANSI C)

55 篇文章 0 订阅
37 篇文章 0 订阅

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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值