________________________c_list.h__________________
#ifndef C_LIST_H
#define C_LIST_H
#ifndef ERR
#define ERR -1
#define OK 1;
#endif
#ifndef MAX
#define MAX 100
#define MIN 0
#endif
typedef int status;
typedef int type;
typedef struct listitem {
type date;//节点数据
struct listitem *next;//指向下个节点
} list_node;//链表节点
typedef struct {
struct listitem *ptr;//链表头指针
int size;//链表长度
} list;//链表
list* list_init ( void );//初始化
status list_destroy( list* );//销毁
status add_node( list*, const type );//加入一个节点
status delete_all( list* );//清空
status delete_node( list*, list_node* );//删除一个节点
status insert_node( list*, const type );//插入一个节点
list_node* find_node( const list*, const type );//查找
status list_print( const list* );//打印
#endif
—————————c_list.c——————————————————
#include
#include
#include "C_list.h"
list* list_init ( void )
{
list *p = ( list* )malloc( sizeof( list ) );
if( p == 0 )
return 0;
p->ptr = 0;
p->size = 0;
return p;
}
status list_destroy( list *pev )
{
if( pev == 0 )
return ERR;
delete_all( pev );
free( pev );
return OK;
}
status add_node( list *p, const type date )
{
list_node *pev =
( list_node* )malloc( sizeof( list_node ) );
if( pev == 0 )
return ERR;
pev->date = date;
pev->next = p->ptr;
p->ptr = pev;
p->size++;
return OK;
}
status delete_node( list *p, list_node *pev )
{
list_node *temp = pev;
if( pev == 0 )
return ERR;
pev = temp->next;
free( temp );
p->size--;
return OK;
}
status delete_all( list *pev )
{
int ix;
if( pev == 0 )
return ERR;
if( pev->size = 0 )
return ERR;
for( ix = 0; ix < pev->size; ++ix, ++pev->ptr )
delete_node( pev, pev->ptr );
return OK;
}
status insert_node( list *p, const type date )
{
list_node *pev = p->ptr; ;
if( p == 0 )
return ERR;
pev = find_node( p, date );
if( pev == 0 )
{
type ia;
printf( "输入要插入的数\n" );
scanf( "%d", &ia );
add_node( p, ia );
}
else
{
type ia;
list_node *pv =
( list_node* )malloc( sizeof( list_node ) );
if( pev == 0 )
return ERR;
printf( "输入要插入的数\n" );
scanf( "%d", &ia );
pv->date = ia;
pv->next = pev->next;
pev->next = pv;
p->size++;
}
return OK;
}
list_node* find_node( const list *pev , const type date )
{
int ix;
list_node *p = pev->ptr;
for( ix = 0; ix < pev->size; ++ix )
if( p->date == date )
return p;
else
p = p->next;
return 0;
}
status list_print( const list *pev )
{
int ix;
list_node *p = pev->ptr;
if( pev == 0 )
return ERR;
if( pev->size == 0 )
return OK;
for( ix = 0; ix < pev->size; ++ix )
{
printf( "%d\t", p->date );
p = p->next;
}
printf( "\n" );
return OK;
}