C primer Plus上的电影列表题,用VC++6.0编译,弹出调试窗口,怎么办呢?
list.h文件:#ifndef LIST_H_
#define LIST_H_
#define bool int
#define true 1
#define false 0
#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);
bool Listisempty(const List *plist);
bool Listisfull(const List *plist);
unsigned int Listitemcount(const List *plist);
bool Additem(Item item,List *plist);
void Traverse(const List *plist,void(*pfun)(Item item));
void Emptythelist(List *plist);
#endif
list.c文件:
#include<stdio.h>
#include<stdlib.h>
#include"list.h"
static void CopyToNode(Item item,List *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)
*plist=pnew;
else
{
while(scan->next!=NULL)
scan=scan->next;
scan->next=pnew;
}
return true;
}
void Traverse(const List *plist,void(*pfun)(Item item))
{
Node *pnode=*plist;
while(pnode!=NULL)
{
(*pfun)(pnode->item);
pnode=pnode->next;
}
}
void Emptythelist(List *plist)
{
Node *psave;
while(*plist!=NULL)
{
psave=(*plist)->next;
free(*plist);
*plist=psave;
}
}
static void Copytopnode(Item item,Node *pnode)
{
pnode->item=item;
}
film3文件:
#include<stdio.h>
#include<stdlib.h>
#include "List.h"
void showmovies(Item item);
int main()
{
List movies;
Item temp;
Initializelist(&movies);
if(Listisfull(movies))
{
fprintf(stderr,"No memory available! Bye!\n");
exit(1);
}
puts("Enter first movie title: ");
while(gets(temp.title)!=NULL&&temp.title[0]!='\0')
{
puts("enter your rating<0-10>: ");
scanf("%d",&temp.rating);
while(getchar()!='\n')
continue;
if(Additem(temp,&movies)==false)
{
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 enter %d movies.\n",Listitemcount(&movies));
Emptythelist(&movies);
printf("Byb!\n");
return 0;
}
void showmovies(Item item)
{
printf("movie: %s rating:%d\n",item.title,item.rating);
}