2016.09.28 list.c

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

/*局部函数原型*/
static void CopyToNode(Item item, Node * pnode);

/*接口函数*/
/*把列表设置为空列表*/
void InitializeList(List * plist)
{
    *plist = NULL;
}
/*如果列表为空则返回真*/
bool ListIsEmpty(List l)
{
    if (l == NULL)
        return true;
    else
        return false;
}
/*如果列表已满则返回真*/
bool ListIsFull(List l)
{
    Node * pt;
    bool full;

    pt = (Node *)malloc(sizeof(Node));
    if (pt == NULL)
        full = true;
    else
        full = false;
    free(pt);
    return full;
}
/*返回节点数*/
unsigned int ListItemCount(List l)
{
    unsigned int count = 0;

    while (l != NULL)
    {
        ++count;
        l = l->next;/*把l设置为下一个节点*/
    }
    return count;
}
/*创建存放项目的节点,并把它添加到plist指向的列表(较慢的实现方法)尾部*/
bool AddItem(Item item, List * plist)
{
    Node * pnew;
    Node * scan = *plist;

    pnew = (Node *)malloc(sizeof(Node));
    if (pnew == NULL)
        return false;/*失败时退出函数*/
    CopyToNode(item, pnew);
    pnew->next = NULL;
    if (scan == NULL)/*空列表,因此*/
        *plist = pnew;/*把pnew放在列表头部*/
    else
    {
        while (scan->next != NULL)
            scan = scan->next;/*找到列表结尾*/
        scan ->next = pnew;
    }
    return true;

}
/*访问每个节点并对它们分别执行由pfun指向的函数*/
void Traverse(List l, void(*pfun)(Item item))
{
    while (l != NULL)
    {
        (*pfun)(l->item);
        l = l->next;
    }
}
/*释放右malloc()分配的内存,并把列表指针设置为NULL*/
void EmptyTheList(List * plist)
{
    Node * psave;
    while (*plist != NULL)
    {
        psave = (*plist)->next;
        free(*plist);
        *plist = psave;
    }
}
/*局部函数定义*/
/*把一个项目复制到一个节点中*/
static void CopyToNode(Item item, Node * pnode)
{
    pnode->item = item;/*结构复制*/
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值