c primer plus 链表的接口代码

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;

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.c的文件代码

/* 简单链表*/
/* 可以储存一系列项*/
/* 初始化链表为空*/
/* 确定链表为空*/
/* 确定链表为满*/
/* 确定链表中的项数*/
/* 在链表末尾添加项*/
/* 遍历链表,处理链表中的项*/
/* 清空链表*/

/*------------------------------------------------------------*/

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

/* 拷贝函数声明*/
static void CodeToNode(Item item, Node * pnode);

/* 接口函数*/
/* 把链表设置为空*/
void InitializeList(List *plist)
{
	*plist = NULL;
}

/* 如果链表为空则返回真*/
bool ListIsEmpty(const List *plist)
{
	if(*plist == NULL)
		return true;
	else 
		return false;
}

/* 如果链表已满则返回true*/
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;
}

/* 创建储存项的结点,并将其添加至由plist指向的链表末尾*/
bool AddItem(Item item, List *plist)
{
	Node * pnew;
	Node * scan = *plist;
	
	pnew = (Node *)malloc(sizeof(Node));
	if(pnew == NULL)
		return false;/* 失败时退出函数*/
	
	CodeToNode(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 CodeToNode(Item item, Node * pnode)
{
	pnode->item = item;
}

main.c的文件代码

 

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

void showmovies(Item item);
char *s_gets(char *st,int n);

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(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,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);
	
}

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)
		*find = '\0';
	else 
		while(getchar() != '\n')
			continue;
	}
	return ret_val;
} 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值