抽象数据类型(ADT)--链表的实现
时间:2010-08-01 来源:chengbin_liu
/* ************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h> #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;
}
}
//=====================================================================
* 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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h> #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;
}
}
//=====================================================================
相关阅读 更多 +