1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #define TSZIE 45 struct film { char title[TSZIE]; int rating; struct film *next; }; int main(int argc, char const *argv[]) { struct film *head = NULL; struct film *prev, * current; char input[TSZIE]; puts("Enter the title"); while(gets(input) != NULL && input[0] != '\0') { current = (struct film *)malloc(sizeof(struct film)); strcpy(current->title, input); if (head == NULL) { head = current; } else { prev->next = current; } current->next = NULL; puts("Enter the rating<0-10>"); scanf("%d", ¤t->rating); while(getchar() != '\n') { continue; } puts("Enter the next title"); prev = current; } if (head == NULL) { puts("no films"); return 1; } current = head; while(current != NULL) { printf("title:%s\n", current->title); printf("rating:%d\n", current->rating); current = current->next; } current = head; while(current != NULL) { free(current); current = current->next; } puts("done"); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#ifndef LIST_H_ //标记变量,防止list.h被重复#include #define LIST_H_ #include <stdbool.h> //c99特性 #define TSZIE 45 //特定于程序的声明,存放影片名的数组大小 struct film { char title[TSZIE]; int rating; }; typedef struct film Item; //一般类型定义 typedef struct node { Item item; struct node * next; } Node; typedef Node * List; /* 操作:初始化一个列表 操作前:plist指向一个列表 操作后:该列表被初始化为空列表 */ void InitializeList(List * plist); /* 操作:确定列表是否为空列表 操作前:plist指向一个已初始化的列表 操作后:如果该列表为空则返回true,否则返回false */ bool ListIsEmpty(const List * plist); /* 操作:确定列表中的项目的个数 操作前:plist指向一个已初始化的列表 操作后:返回该列表中项目的个数 */ unsigned int ListItemCount(const List * plist); /* 操作:在列表尾部添加一个项目 操作前:item是要被增加到列表的项目 plist指向一个已初始化的列表 操作后:如果可能的话,在列表尾部添加一个新项目, 函数返回true,否则函数返回false */ bool AddItem(Item item, List * plist); /* 操作:把一个函数作用于列表中的每个项目 操作前:plist指向一个已初始化的列表 pfun指向一个函数,该函数接受 一个Item参数并且无返回值 操作后:pfun指向的函数被作用到 列表中的每个项目一次 */ void Traverse(const List * plist, void(* pfun)(Item item)); /* 操作:释放已分配的内存(如果有) 操作前:plist指向一个已初始化的列表 操作后:为该列表分配的内存已被释放 并且该列表被置为空列表 */ void EmptyTheList(List * plist); #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#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; } } 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; } 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 CopyToNode(Item item, Node * pnode) { pnode->item = item; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// gcc -c list.c // gcc main.c list.o #include <stdio.h> #include <stdlib.h> #include "list.h" void showmovies(Item item); int main(int argc, char const *argv[]) { List movies; Item temp; InitializeList(&movies); 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; } puts("Enter next movie title(epmty line to stop)"); } if (ListIsEmpty(&movies)) { puts("No data entered"); } else { puts("here is the movie list"); Traverse(&movies, showmovies); } printf("you entered %d movies\n", ListItemCount(&movies)); EmptyTheList(&movies); puts("done"); return 0; } void showmovies(Item item) { printf("movie:%s rating:%d\n", item.title, item.rating); } |