Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347157
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: C/C++

2010-08-01 17:44:38

/* ************************************************************************
 *       Filename:  list.h
 *    Description:  chengbin_liu
 *        Version:  1.0
 *        Created:  2010年08月01日 14时22分50秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  YOUR NAME (),
 *        Company: 
 * ************************************************************************/
#ifndef _LIST_H_
#define _LIST_H_
#include
#include
#include
#define TSIZE 45
typedef struct film
{
 char title[TSIZE];
 int rating;
}Item;
typedef struct node
{
 Item item;
 struct node *next;
}Node;
typedef Node *List;
void InitializeList(List *pList);//init list
bool ListIsEmpty(const List *pList);//is empty?
bool ListIsFull(const List *pList);//is full?
unsigned int ListItemCount(const List *pList);//item count in pList.
bool AddItem(Item item, List *pList);//insert the item to the pList.
void Traverse(const List *pList, void (*fun)(Item item));
void EmptyTheList(List *pList);//free list,

#endif
//===================================================================
/* ************************************************************************
 *       Filename:  list.c
 *    Description:  chengbin_liu
 *        Version:  1.0
 *        Created:  2010年08月01日 14时33分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  YOUR NAME (),
 *        Company: 
 * ************************************************************************/
#include "list.h"
static void CopyToNode(Item item,Node *pnode);
void InitializeList(List *pList)
{
 *pList=NULL;
}
bool ListIsEmpty(const List *pList)
{
 if(*pList==NULL)
  return true;
 else
  return false;
}
bool ListIsFull(const List *pList)
{
 Node *pt;
 bool full;
 pt=(Node *)malloc(sizeof(Node));
 if(pt==NULL)
  full=true;
 else
  full=false;
 free(pt);
 return full;
}
unsigned int ListItemCount(const List *pList)
{
 unsigned int count=0;
 Node *pnode=*pList;
 while(pnode!=NULL);
 {
  ++count;
  pnode=pnode->next;
 }
 return count;
}
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;
 else
 {
  while(scan->next!=NULL)
   scan=scan->next;
  scan->next=pnew;
 }
 return true;
}
static void CopyToNode(Item item,Mode *pnode)
{
 pnode->item=item;
}
void Traverse(const List *pList,void (*fun)(Item item))
{
 Node *pnode=*pList;
 while(pnode !=NULL)
 {
  (*fun)(pnode->item);
  pnode=pnode->next;
 }
}
void EmptyTheList(List *pList)
{
 Node *psave;
 while(*pList!=NULL)
 {
  psave=(*pList)->next;
  free(*pList);
  *pList=psave;
 }
}
//=====================================================================
阅读(1425) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~