单链表的封装
#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