单链表存储电影信息

单链表的封装

#ifndef LIST_H_INCLUDEDa //防止别的头文件里包含此文件从而多包含
#define LIST_H_INCLUDEDa

#include <stdbool.h>
#define TSIZE 45
#include <stdio.h>
#include <stdbool.h>

typedef struct flim
{
    char title[TSIZE];
    int rating;
}Item;


typedef struct node
{
    Item item;
    struct node *next;
} Node,*List;




//局部函数原型//
static void CopyNode(Item item,List pnode);

//初始化链表
void initList(List *L)
{
    *L=NULL;
}

//判断链表是否为空
bool ListisEmpty(const List *L)
{
    if(*L==NULL)
        return true;
    else
        return false;
}

//尝试分配空间,看内存是否已满
bool ListisFull(void)
{
    List pt;
    bool full;

    pt=(List)malloc(sizeof(Node));
    if(pt)
        full=false;
    else
        full=true;
    free(pt);
    return full;
}

//将新的节点添加到末尾
bool AddItem(Item item,List *L)
{
    List pnew;
    List scan=*L;
    pnew=(List)malloc(sizeof(Node));
    if(pnew==NULL) return false;

    if(scan==NULL) *L=pnew;
   else
   {
       while(scan->next)      //若是没有分辨是否链表为空就遍历会因为头指针没有next而死循环
          scan=scan->next;
        scan->next=pnew;
   }

    CopyNode(item,pnew);
    pnew->next=NULL;     //这里要赋值NULL因为第一个元素的话L没有next

    return true;
}

//删除特定位置的结点
bool Deitem(List *L,int i)
{
    List q,s=*L;
    int k=1;
    while(k<i-1 && s)
    {
        s=s->next;
        k++;
    }
    if((i!=1 && s->next==NULL) || i<1) return false;
    if(i==1)
    {
        *L=s->next;
        free(s);
    }
    else
    {
        q=s->next;
        s->next=q->next;
        free(q);
    }
    return true;
}


//访问每个节点,执行特定函数
void Traverse(const List *L,void (*pfun)(Item item))
{
    List pnode=*L;

    while(pnode)
    {
        pfun(pnode->item);
        pnode=pnode->next;
    }

}

//清空所有结点
void EmptyTheList(List *L)
{
    List pnode=*L;
    List psave;

    while(pnode)
    {
        psave=pnode->next;
        free(pnode);
        pnode=psave;
    }

}

static void CopyNode(Item item,List pnode)
{
    pnode->item=item;
}

#endif

存储电影结构体

#ifndef LIST_H_INCLUDEDa //防止别的头文件里包含此文件从而多包含
#define LIST_H_INCLUDEDa

#include <stdbool.h>
#define TSIZE 45
#include <stdio.h>
#include <stdbool.h>

typedef struct flim
{
    char title[TSIZE];
    int rating;
}Item;


typedef struct node
{
    Item item;
    struct node *next;
} Node,*List;




//局部函数原型//
static void CopyNode(Item item,List pnode);

//初始化链表
void initList(List *L)
{
    *L=NULL;
}

//判断链表是否为空
bool ListisEmpty(const List *L)
{
    if(*L==NULL)
        return true;
    else
        return false;
}

//尝试分配空间,看内存是否已满
bool ListisFull(void)
{
    List pt;
    bool full;

    pt=(List)malloc(sizeof(Node));
    if(pt)
        full=false;
    else
        full=true;
    free(pt);
    return full;
}

//将新的节点添加到末尾
bool AddItem(Item item,List *L)
{
    List pnew;
    List scan=*L;
    pnew=(List)malloc(sizeof(Node));
    if(pnew==NULL) return false;

    if(scan==NULL) *L=pnew;
   else
   {
       while(scan->next)      //若是没有分辨是否链表为空就遍历会因为头指针没有next而死循环
          scan=scan->next;
        scan->next=pnew;
   }

    CopyNode(item,pnew);
    pnew->next=NULL;     //这里要赋值NULL因为第一个元素的话L没有next

    return true;
}

//删除特定位置的结点
bool Deitem(List *L,int i)
{
    List q,s=*L;
    int k=1;
    while(k<i-1 && s)
    {
        s=s->next;
        k++;
    }
    if((i!=1 && s->next==NULL) || i<1) return false;
    if(i==1)
    {
        *L=s->next;
        free(s);
    }
    else
    {
        q=s->next;
        s->next=q->next;
        free(q);
    }
    return true;
}


//访问每个节点,执行特定函数
void Traverse(const List *L,void (*pfun)(Item item))
{
    List pnode=*L;

    while(pnode)
    {
        pfun(pnode->item);
        pnode=pnode->next;
    }

}

//清空所有结点
void EmptyTheList(List *L)
{
    List pnode=*L;
    List psave;

    while(pnode)
    {
        psave=pnode->next;
        free(pnode);
        pnode=psave;
    }

}

static void CopyNode(Item item,List pnode)
{
    pnode->item=item;
}

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值