简单的单链表

来源:互联网 发布:恢复手机联系人软件 编辑:程序博客网 时间:2024/06/07 15:53
头文件 list.h
#ifndef LIST_H_#define LIST_H_#include <stdbool.h>#define TSIZE 45struct 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指向一个已初始化的列表 *//*操作后:如果改列表为空则返回true,否则返回false  */bool ListIsEmpty(const List * plist);/*操作:确定列表是否已满*//*操作前:plist指向一个已初始化的列表*//*操作后:如果改列表已满则返回true,否则返回false*/bool ListIsFull(const List * plist);/*操作:确定列表中项目的个数*//*操作前:plist指向一个已初始化的列表*//*操作后:返回该列表中项目的个数*/unsigned int ListItemCount(const List * plist);/*操作:在列表尾部添加一个项目*//*操作前: 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  

具体方法文件list.h

#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;//List l = *plist;Node * l = * plist;while(l != NULL){count++;l = l->next;}return count;} /*创建存放项目的节点,并把它存放在又plist指向的列表尾部*/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; } /*访问每个节点并对他们分别执行由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;while(*plist != NULL){psave = (*plist) ->next;free(*plist);*plist = psave;}}  /*局部函数定义,把一个项目复制到一个节点*/static void CopyToNode(Item item,Node * pnode) {pnode->item = item;} 
应用文件 film.c
#include <stdio.h>#include <stdlib.h>#include "list.h"void showmovies(Item item);int main(void) {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 entered %d movies .\n",ListItemCount(&movies));/*清除*/ EmptyTheList(&movies);printf("bye!\n");return 0;}void showmovies(Item item){printf("movie:%s rating:%d \n",item.title, item.rating);}

原创粉丝点击