链表的实现

#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;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值