来自 c primer 中的电影评分功能
程序清单 17.2 films2.c 程序
----------------------------------------------------------
/* films2.c -- 使用结极链表 */
#include <stdio.h>
#include <stdlib.h> /* 掔供 malloc() 原垄 */
#include <stdring.h> /* 掔供 strcpy() 原垄 */
#define TSIZE 45 /* 存放片名癿数组大小 */
struct film {
char title[TSIZE];
int rating;
struct film * next; /* 指向链表癿下一个结极 */
};
int main (void)
{
struct film * head = NULL; /* 定丿指向结极癿央部指针 */
struct film * prev, * current;
char input[TSIZE];
/* 收集幵存偹信息 */
puts ("Enter first movet title : ");
while (gets(input) != NULL && input[0] != '\0')
{
/* 输入为真, malloc() 获叏存偹穸闱, 幵将首地址赋给指针发量 current */
current = (struct film *) malloc (sizeof (struct film));
if (head == NULL) /* 刞断是否第一个结极 */
head = current; /* head 指向分配后内存坑癿首地址 */
else /* 后续癿结极 */
prev->next = current; /* 将下一个结极癿首地址赋给前一个结
极癿 next 成员 */
current->next = NULL; /* 表示弼前结极是列表中癿最后一个 */
strcpy (current->title, input); /* 复刢输入刡结极内癿成员*/
puts ("Enter your rating <0-10> : ");
scanf ("%d", ¤t->rating);
while (getchar() != '\n') /* 如果输入丌 = 换行 */
continue;
puts ("Enter next movie title (empty line to stop) : ");
prev = current; /* 为下一轮输入做准备 */
}
/* 给出电影列表 */
if (head == NULL) /* 如果输入为穸 */
printf ("No data entered ");
else
printf ("Here is the movie list : \n");
current = head; /* 设置 current 指向第一个结极 */
while (current != NULL) /* 循环过程 直刡 指向为穸字节 */
{
printf ("movie: %s Rating: %d \n", current->title, current->rating);
current = current->next; /* 重设 current 仌指向列表中癿下一个结极
*/
}
/* 仸务巫完成, 因此释放所分配癿内存 */
//current = head; 加这个有错误,应该head->next 没法释放
while (current != NULL)
{
PrevBook = currenBook->next; //先把next传给其他指针,再消除
free(currenBook);
currenBook = PrevBook;
}
printf ("Bye \n");
return 0;
}
模仿得到下面图书馆书本输入系统,保存到文件
/* library.c -- 图书馆书籍录入系统 */
#include <stdio.h>
#include <stdlib.h> /* 提供 malloc() 原型 */
#include <string.h> /* 掔供 strcpy() 原型 */
#define TSIZE 45 /* 存放片名的数组大小 */
typedef struct BOOK
{
char bookname[TSIZE] ;
char writername[TSIZE];
int ser_num;
struct BOOK * next;
}LibraryBook;
int main()
{
FILE *Bookfp;
char ch;
LibraryBook *head = NULL;
LibraryBook *currenBook ,*PrevBook;
char inBookName[TSIZE];
/*开始图书录入*/
puts("please input a book's name:\n ");
while (gets(inBookName) != NULL && inBookName[0] != NULL)
{
currenBook = (LibraryBook*)malloc(sizeof(struct BOOK));
if(head == NULL)
head = currenBook;
else
{
PrevBook->next = currenBook;
}
currenBook->next = NULL;
strcpy(currenBook->bookname,inBookName);
puts("please input writer'name of your book :\n");
gets(currenBook->writername);
printf("please input serial number of your book:\n");
scanf("%d",¤Book->ser_num);
while (getchar() != '\n')
continue;
PrevBook = currenBook;
puts("please input next book's name(empty line to stop):\n ");
}
/*打印录入的图书信息*/
if(head == NULL)
printf("No book entered");
else
printf("Here is the book list:\n");
currenBook = head;
while (currenBook != NULL)
{
printf("BOOK:%s WRITER:%s NUMBER:%d\n",currenBook->bookname,currenBook->writername,\
currenBook->ser_num);
currenBook = currenBook->next;
}
/*存储录入的书信息到文件*/
if((Bookfp = fopen("string","at+")) == NULL)
{
printf("Cannot open file of book");
getchar();
exit(1);
}
currenBook = head;
/*if( (ch = ftell(Bookfp)) == 0) 这段想判断文件是否有数据,但失败了
{
fputs("BOOK\t\tWRITER\t\tNUMBER\t\t\n",Bookfp);
}*/
fputs("BOOK\t\tWRITER\t\tNUMBER\t\t\n",Bookfp);
while (currenBook != NULL)
{
fprintf(Bookfp,"%s\t\t%s\t\t%d\n",currenBook->bookname,currenBook->writername,currenBook->ser_num);
currenBook = currenBook->next;
}
fclose(Bookfp);
/*释放指针*/
currenBook = head;
while (currenBook != NULL)
{
PrevBook = currenBook->next;
free(currenBook);
currenBook = PrevBook;
}
printf("Bye!\n");
while (1);
return 0;
}