我在编写如下程序时
work_film.c
//使用抽象数据类型(ADT)风格的链表
//与work_list.c一起编译
#include <stdio.h>
#include <stdlib.h> //提供exit()的原型
#include "work_list.h" //定义List、Item
void showmovies(Item item);
char *s_gets(char *st, int n);
int main(void)
{
List movies;
Item temp;
//初始化
InitializeList(&movies);
if (ListIsFull(&movies))
{
fprintf(stderr, "No memory available! Bye!\n");
exit(1);
}
//获取用户输入并存储
puts("Enter first movie title:");
while (s_gets(temp.title, TSIZE) != NULL && temp.title[0] != '\0')
{
puts("Enter your rating <0-10>:");
scanf("%d", &temp.rating);
while (getchar() != '\n')
continue;
if(AddItem(temp, &movies) == 0)
{
fprintf(stderr, "Problem allocating memory\n");
break;
}
if (ListIsFull(&movies))
{
puts("The list is now full.");
break;
}
puts("Enter next movie title (empty line to stop):");
}
//显示
if (ListIsEmpty(&movies))
printf("No data entered. ");
else
{
printf("Here is the movie list:\n");
Traverse(&movies, showmovies);
}
printf("You entered %d movies.\n", ListItemCount(&movies));
//清理
EmptyTheList(&movies);
printf("Bye!\n");
return 0;
}
void showmovies(Item item)
{
printf("Movie: %s Rating: %d\n", item.title, item.rating);
}
char *s_gets(char *st, int n)
{
char *ret_val;
char *find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); //查找换行符
if(find) //如果地址不是NULL
*find='\0'; //在此处放置一个空字符
else
while (getchar() != '\n')
continue; //处理输入行的剩余内容
}
return ret_val;
}
work_list.c
#include <stdio.h>
#include <stdlib.h>
#include "work_list.h"
//局部函数的原型
static void CopyToNode(Item item, Node *pnode);
//接口函数
//把链表置空
void InitializeList(List *plist)
{
(*plist).head=NULL;
(*plist).end=NULL;
//初始化列表,头尾双指针均设置为空
}
//如果链表为空,返回1
int ListIsEmpty(const List *plist)
{
if ((*plist).head==NULL)
return 1;
else
return 0;
}
//如果链表已满,返回1
int ListIsFull(const List *plist)
{
Node *pt;
int full;
pt=(Node *)malloc(sizeof(Node));
if (pt==NULL)
full=1;
else
full=0;
free(pt);
return full;
}
//返回节点数量
unsigned int ListItemCount(const List *plist)
{
unsigned int count=0;
Node *pnode=(*plist).head;
while (pnode != NULL)
{
++count;
pnode = pnode->next;
}
return count;
}
//创建存储项的节点,并将其添加至plist指向的链表末尾(较慢的实现)
int AddItem(Item item, List *plist)
{
Node *pnew;
Node *scan=(*plist).head;
pnew=(Node *)malloc(sizeof(Node));
if(pnew==NULL)
return 0; //在失败时退出
CopyToNode(item, pnew);
pnew->next = NULL;
if(scan==NULL) //空链表中,把pnew放到链表的开头
{
(*plist).head=pnew;
(*plist).end=pnew;
//空链表的第1个元素,首尾均指向该元素
}
else
{
(*plist).end->next=pnew;
(*plist).end=pnew;
//以上为添加尾部指针之后的操作方式。直接添加至末尾,末尾后移。
//可以对比原来的代码,原来的代码需要从头查到末尾,可以对比加了末尾指针之后的优势
//原代码:
//while(scan->next != NULL)
//scan=scan->next;
//scan->=pnew;
}
return 1;
}
//访问每个节点并执行pfun指向的函数
void Traverse(const List *plist, void (*pfun)(Item item))
{
Node *pnode=(*plist).head;
while(pnode != NULL)
{
(*pfun)(pnode->item);
pnode = pnode->next;
}
}
//释放由malloc分配的内存,设置链表指针为空
void EmptyTheList(List *plist)
{
Node *psave;
while ((*plist).head != NULL)
{
psave = (*plist).head->next; //保存下一个节点地址
free((*plist).head); //释放当前节点
(*plist).head=psave; //前进至下一个节点
}
}
//局部函数的定义,把一个项复制至节点中
static void CopyToNode(Item item, Node *pnode)
{
strpy(pnode->item.title, item.title);
pnode->item.rating = item.rating;
//该函数需要根据Item结构体进行修改,以上是针对struct film修改的例子
//pnode->item = item
}
work_list.h
//接口头文件,简单链表类型的头文件
#ifndef LIST_H_
#define LIST_H_
//特定程序的声明
#define TSIZE 45 //存储电影名的数组大小
struct film
{
char title[TSIZE];
int rating;
};
//一般类型定义
typedef struct film Item;
typedef struct node
{
Item item;
struct node *next;
} Node;
typedef struct list
{
Node *head; //指向列表首
Node *end; //指向列表尾
} List;
//函数原型
//操作:初始化一个链表
//前提条件:plist指向一个链表
//后置条件:链表初始化为空
void InitializeList(List *plist);
//操作:确定链表是否为空,plist指向一个已初始化的链表
//后置条件:如果链表为空,该函数返回true;否则返回false
int ListIsEmpty(const List *plist);
//操作:确定链表是否已满,plist指向一个已初始化的链表
//后置条件:如果链表已满,该函数返回true;否则返回false
int ListIsFull(const List *plist);
//操作:确定链表中的项数,plist指向一个已初始化的链表
//后置条件:该函数返回链表中的项数
unsigned int ListItemCount(const List *plist);
//操作:在链表的末尾添加项
//前提条件:item是一个待添加至链表的项,plist指向一个已初始化的链表
//后置条件:如果可以,该函数在链表末尾添加一个项,且返回ture,否则返回false
int AddItem(Item item, List *plist);
//操作:把函数作用于链表中的每一项
//plist指向一个已初始化的链表
//pfun指向一个函数,该函数接受一个Item类型的参数,且无返回值
//后置条件:pfun指向的函数作用于链表中的每一项一次
void Traverse(const List *plist, void(*pfun)(Item item));
//操作:释放已分配的内存(如果有的话)
//plist指向一个已初始化的链表
//后置条件:释放了为链表分配的所有内存,链表设置为空
void EmptyTheList(List *plist);
#endif
出现如下结果:
请问是什么原因?应该如何解决?