01.#ifndef LIST_H_
02.#define LIST_H_
03.#include <stdbool.h>
04.
05.#define TSIZE 45
06.
07.struct film{
08. char title[TSIZE];
09. int rating;
10.};
11.
12.typedef struct film Item;
13.
14.typedef struct node{
15. Item item;
16. struct node * next;
17.} Node;
18.
19.typedef Node * List;
20.
21./*操作:初始化一个列表 */
22./*操作前: plist指向一个列表 */
23./*操作后:该列表初始化为空列表 */
24.void InitializeList(List *plist);
25.
26./*操作:确定列表是否为空 */
27./*操作前:plist指向一个已初始化的列表 */
28./*操作后:如果改列表为空则返回true,否则返回false */
29.bool ListIsEmpty(const List * plist);
30.
31./*操作:确定列表是否已满 */
32./*操作前:plist指向一个已初始化的列表 */
33./*操作后:如果改列表已满则返回true,否则返回false */
34.bool ListIsFull(const List * plist);
35.
36./*操作:确定列表中项目的个数 */
37./*操作前:plist指向一个已初始化的列表 */
38./*操作后:返回该列表中项目的个数 */
39.unsigned int ListItemCount(const List * plist);
40.
41./*操作:在列表尾部添加一个项目 */
42./*操作前: plist指向一个已初始化的列表 */
43./*操作后:如果可能的话,在列尾部添加一个列表 */
44./*函数返回true,否则返回false */
45.bool AddItem(Item item, List * plist);
46.
47./*操作:把一个函数作用于列表中的每个项目 */
48./*操作前: plist指向一个已初始化的列表 */
49./* pfun指向一个函数,该函数接受一个item参数并且无返回值 */
50./*操作后:pfun指向的函数被作用到列表中的每一个项目一次*/
51.void Traverse (const List * plist, void (* pfun) (Item item) );
52.
53./*操作:释放已分配的内存(如果有) */
54./*操作前: plist指向一个已初始化的列表 */
55./*操作后:为该列表分配的内存已被释放并且该列表被置为空列表*/
56.void EmptyTheList (List * plist);
57.
58.#endif
01.#include <stdio.h>
02.#include <stdlib.h>
03.#include "list.h"
04.
05./*局部函数原型 */
06.static void CopyToNode (Item item, Node * pnode);
07.
08./*接口函数: 把列表设置为空列表 */
09.void InitializeList(List * plist){
10. * plist = NULL;
11.}
12.
13./*接口函数: 判断列表是否为空*/
14.bool ListIsEmpty(const List * plist){
15. if(* plist == NULL){
16. return true;
17. }else{
18. return false;
19. }
20.}
21.
22./*接口函数: 判断列表是否已满*/
23.bool ListIsFull(const List * plist) {
24. Node *pt;
25. bool full;
26. pt = (Node *)malloc( sizeof(Node) ) ;
27. if(pt == NULL){
28. full = true;
29. }else{
30. full = false;
31. }
32. free(pt);
33. return full;
34.}
35.
36./*接口函数: 返回节点数*/
37.unsigned int ListItemCount(const List * plist) {
38. unsigned int count = 0;
39.// List l = *plist;
40. Node * l = * plist;
41. while(l != NULL){
42. count++;
43. l = l->next;
44. }
45. return count;
46.}
47.
48./*创建存放项目的节点,并把它存放在又plist指向的列表尾部*/
49.bool AddItem(Item item, List * plist) {
50. Node * pnew;
51. Node * scan = * plist;
52. pnew = (Node *)malloc( sizeof(Node) );
53. if(pnew == NULL){
54. return false; //失败时退出函数
55. }
56. CopyToNode(item,pnew);
57. pnew->next = NULL;
58. if(scan == NULL){ //空列表
59. * plist = pnew; //把pnew放在列表头部
60. }else{
61. while(scan -> next != NULL){
62. scan = scan->next; //找到列表尾部
63. }
64. scan -> next = pnew ; //把pnew添加到尾部
65. }
66. return true;
67.}
68.
69./*访问每个节点并对他们分别执行由pfun指向的函数*/
70.void Traverse(const List * plist, void (* pfun)(Item item)){
71. Node * pnode = *plist;
72. while(pnode != NULL){
73. (*pfun)(pnode->item);
74. pnode = pnode->next;
75. }
76.}
77.
78./*释放由malloc()分配的内存 把列表指针设置为NULL*/
79.void EmptyTheList(List *plist){
80. Node * psave;
81. while(*plist != NULL){
82. psave = (*plist) ->next;
83. free(*plist);
84. *plist = psave;
85. }
86.}
87.
88./*局部函数定义,把一个项目复制到一个节点*/
89.static void CopyToNode(Item item,Node * pnode) {
90.
91. pnode->item = item;
92.
93.}
01.#include <stdio.h>
02.#include <stdlib.h>
03.#include "list.h"
04.void showmovies(Item item);
05.
06.
07.int main(void) {
08. List movies;
09. Item temp;
10.
11. /*初始化*/
12. InitializeList(&movies);
13.
14. /*判断是否已满*/
15. if(ListIsFull(&movies)){
16. fprintf(stderr, "No memory available! bye! \n");
17. exit(1);
18. }
19.
20. /*收集并存储 */
21. puts("Enter first movie title");
22. while(gets(temp.title) != NULL && temp.title[0] != '\0'){
23. puts("enter your rating<0-10>:");
24. scanf("%d",&temp.rating);
25. while(getchar() != '\n'){
26. continue;
27. }
28. if(AddItem(temp,&movies) == false){
29. fprintf(stderr,"problem allocating memory.\n");
30. break;
31. }
32. if(ListIsFull(&movies)){
33. puts("The list is now full.");
34. break;
35. }
36. puts("Enter next movie title(empty line to stop);");
37. }
38.
39. /*显示*/
40. if(ListIsEmpty(&movies)){
41. printf("no data entered");
42. }else{
43. printf("Here is the movie list;\n");
44. Traverse(&movies,&showmovies);
45. }
46.
47. printf("You entered %d movies .\n",ListItemCount(&movies));
48. /*清除*/
49. EmptyTheList(&movies);
50.
51. printf("bye!\n");
52.
53. return 0;
54.}
55.
56.void showmovies(Item item){
57. printf("movie:%s rating:%d \n",item.title, item.rating);
58.}