昨天我们给出了一个简单的List的接口,但因为太晚了,所以,当时我们并没有实现接口,所以今天我们的任务就是完成这些简单的接口的实现,现在相信大家都有一定的基础了,就算大家一开始是0基础来关注我们的课,只要跟着一步步的走到现在,相信对于下面的实现不会觉得理解不过来。
下面是我们的实现清单:
-------------------------------------------
//my_list.c
#include
#include
#include 'my_list.h'
//全局函数,把元素添加进列表
static void CopyToNode(Node * pNode,iTem item)
{
pNode->item = item;
}
//初始化List对象
void InitializeList(List *plist)
{
plist->head = NULL;
plist->size = 0;
}
//确认列表是否为空
bool ListIsEmpty(const List* plist)
{
if(plist == NULL)
return true;
return false;
}
//确认列表是否已满
bool ListisFull(const List* plist)
{
plist = (List*)malloc(sizeof(List));
if(plist == NULL)
return true;
else
return false;
}
//返回元素个数
unsigned int ListItemCount(const List * plist)
{
unsigned int count = 0;
while(plist->head != NULL)
{
count++;
plist->head = plist->head->next;
}
return count;
}
//添加元素,由于我们实现的是单向链表,所以
使用从尾部添加
bool AddItem (iTem item, List * plist)
{
Node *pNew,*temp;
pNew = (Node*)malloc(sizeof(Node));
if(pNew == NULL)
return false;
CopyToNode(pNew,item);
pNew->next = NULL;
temp = pNew->next;
if(plist->head != NULL)
{
while(plist->head->next != NULL)
{
plist->head = plist->head->next;
}
plist->head->next = pNew;
}
else
{
plist->head = pNew;
}
return true;
}
//显示列表中的元素
void ShowListItem(const List* plist)
{
Node *pNode = plist->head;
while(pNode != NULL)
{
printf('%s:%s\n',
pNode->item.Name,pNode->item.TelNumber);
pNode = pNode->next;
}
}
//释放内存
void FreetheList (List * plist)
{
while(plist->head != NULL)
{
Node* pNode;
pNode = plist->head->next;
free(plist->head);
plist->head = pNode;
}
}
----------------------------------------------
这就是我们接口的实现,为了验证我们的接口,我们通过下面这个例子来看看:
-------------------------------------------
//ListTest.c
#include
#include
#include
#include 'my_list.h'
int main()
{
List BookPhone ;
Node* TempPhone = (Node*)malloc(sizeof(Node));;
unsigned int count;
//使用初始化接口
InitializeList(&BookPhone);
//使用ListisFull来检查List的状态
if(ListisFull(&BookPhone))
{
printf('内存已满,请退出!');
exit(1);
}
printf('请输入联系人的姓名:\n');
while(gets_s(TempPhone->item.Name) != NULL &&TempPhone->item.Name[0] != '\0')
{
printf('请输入联系人的电话号码:\n');
gets_s(TempPhone->item.TelNumber);
if(ListisFull(&BookPhone))
{
printf('内存已满!!!');
break;
}
//使用添加元素接口
if(!AddItem(TempPhone->item,&BookPhone))
{
printf('联系人添加失败!!!');
break;
}
while(getchar() != '\n')
continue;
printf('若还要储存,请输入下一个联系人(空行退出):\n');
}
//接下来我们该显示我们通信录里面的联系人信息了
//使用判断状态接口
if(ListIsEmpty(&BookPhone))
printf('通信录没有数据!!!');
else
{
printf('通信录:\n');
//使用打印接口
ShowListItem(&BookPhone);
}
//使用联系人数量接口
count = ListItemCount(&BookPhone);
printf('联系人数目:%d\n',count);
//使用释放内存接口
FreetheList(&BookPhone);
free(TempPhone);
system('PAUSE');
return 0;
}
---------------------------------------
然后我们随便试一试结果怎么样吧:
嗯,好像还不错,似乎可以行,不过我告诉大家,这个程序有个缺陷,而且是大问题,大家自己去发现吧,也算是留给大家这段时间学习C语言的一个检验吧,关于第二十六里面的缺陷我们今天其实已经说了,如果大家认真些,不难发现问题的所在。
C语言就算是告一段落了,有朋友说尽快开讲C++,有朋友说尽快讲些win32的编程知识,让我考虑考虑下吧。
还有,我们今天和昨天实现的接口是可以重用的,只要以后需要用到类似的接口,我们可以随时拉出来使用,当然,等我们以后说到 动态链接库(dll)的时候,我们可以把他封装成一个dll文件,想要使用的时候可以动态加载,不过,目测这会是很久以后的事了。
======================
回复D(不区分大小写)直接查看目录。
联系客服