1. list.h
/* 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);
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_H_
2. list.c
/*list.c --支持链表操作的函数*/
#include <stdio.h>
#include <stdlib.h>
#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;//空链表,所以把pnew放在链表的开头
}
else
{
while(scan->next != NULL)
{
scan = scan->next;//找到链表的末尾
}
scan->next = pnew;//把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 CopyToNode(Item item, Node *pnode)
{
pnode->item = item;
}
3. film.c
/*film.c --使用抽象数据类型(ADT)风格的链表*/
/*与list.c一起编译*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
void show_movies(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("%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, show_movies);
}
printf("You entered %d movies.\n", ListItemCount(&movies));
//清理
EmptyTheList(&movies);
printf("Bye!\n");
return 0;
}
void show_movies(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) //如果地址不是NUNLL,在此处放置一个空字符
{
*find = '\0';
}
else
{
while(getchar() != '\n')
{
continue;//处理输入行的剩余内容
}
}
}
return ret_val;
}
C11编译,运行结果:
Enter first movie title:
before you
Enter your rating <0-10>:
8
Enter next movie title(empty line to stop):
the pursuit to happiness
Enter your rating <0-10>:
9
Enter next movie title(empty line to stop):
pride and prejudice
Enter your rating <0-10>:
9
Enter next movie title(empty line to stop):
Here is the movie list:
Movie: before you Rating: 8
Movie: the pursuit to happiness Rating: 9
Movie: pride and prejudice Rating: 9
You entered 3 movies.
Bye!