如图运行并输入信息后报错,希望可以找出问题,并调试可以运行。
《list.h》
#ifndef LIST_H_
#define LIST_H_
#include <stdbool.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 Node* List;
/*函数原型*/
/*操作: 初始化一个链表*/
/*前提条件: plist指向一个链表*/
/*后置条件: 链表初始化为空*/
void InitializeList(List* plist);
/*操作: 确认链表是否为空,plist指向一个已初始化指针*/
/*后置条件: 链表为空返回ture,否则返回flase*/
bool ListIsEmpty(const List* plist);
/*操作: 确认链表是否已满,plist指向一个已初始化指针*/
/*后置条件: 链表为空返回ture,否则返回flase*/
bool ListIsFull(const List* plist);
/*操作: 确认链表中的项数,plist指向一个已初始化指针*/
/*后置条件: 返回链表中的项数*/
unsigned int ListItemCount(const List* plist);
/*操作: 在链表末尾添加项*/
/*前提条件: item是一个待添加至链尾的项,plist指向一个已初始化的链表*/
/*后置条件: 若可以添加,在链表末尾添加一个项,并返回ture,否则返回flase*/
bool AddItem(Item item,List*plist);
/*操作: 把函数作用于链表中的每一项*/
/*前提条件: plist指向一个已初始化的链表,pfun指向一个函数*/
/*后置条件: pfun指向的函数作用于链表中每一项一次*/
void Traverse(const List* plist, void(*pfun)(Item item));
/*操作: 释放已分配的内存*/
/*前提条件: plist指向一个已初始化的链表*/
/*后置条件: 释放为链表分配的所有内存,链表设置为空*/
void EmptyTheList(List* plist);
#endif
17.3.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void showmovies(Item item);
char* s_gets(char* st, int n);
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 (s_gets(temp.title, TSIZE) != NULL && temp.title[0] != '\0')
{
puts("Enter your rating<0-10>:");
scanf_s("%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 movies title(Empty line to stop)");
}
/*显示 */
if (ListIsEmpty(&movies))
printf("No date entered.");
else
{
printf("Here is the movie list:\n");
Traverse(&movies, showmovies);
}
printf("You Enter %d movies.\n", ListItemCount(&movies));
/*清理 */
EmptyTheList(&movies);
printf("bye!\n");
return 0;
}
void showmovies(Item item)
{
printf("Movies: %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)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
list.c
#include <stdio.h>
#include<stdlib.h>
#include"list.h"
/*局部函数原型*/
static void CopyToNode(Item item, Node* Pnode);
/*接口函数*/
/*设置链表为空*/
void InitializeList(List* plist)
{
plist = NULL;
}
/*如果链表为空,返回true*/
bool ListIsEmpty(const List* plist)
{
if (*plist == NULL)
return true;
else
return false;
}
/*链表已满,返回true*/
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;
}
/*创建存储项的列表,并将其添加至plist指向的链表末尾*/
bool AddItem(Item item, List* plist)
{
Node* pnew=NULL;
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;
}
/*访问每个节点并执行pfun指向的函数*/
void Traverse(const List* plist, void(*pfun)(Item item))
{
Node* pnode = *plist;
while (pnode != NULL)
{
(*pfun)(pnode->item);
pnode = pnode->next;
}
}
/*释放由malloc()分配的内存*/
/*设置链表指针为NULL*/
void EmptyTheList(List* plist)
{
Node* psave=NULL;
while (*plist != NULL)
{
psave = (*plist)->next;
free(*plist);
*plist = psave;
}
}
/*局部函数定义*/
/*把上一个函数定义到节点中*/
static void CopyToNode(Item item, Node* pnode)
{
pnode->item = item;
}