C Peimer 第17章编程习题(部分)

高级数据结构,先前接触了许多类似的算法,有一些概念,但要说实现的话,其实对个人还是挺难的。本来这是一个机会,能够真正实现一下,以锻炼这点短处,可是每一小段程序都太费时间了,但实现出来收获却又不是特别的大,因此先放一下再说吧。

把题做了一半,等真正有时间再做吧(虽然觉得够呛了,等到真回头做的时候,应该又是还账的时候了)

1. 主调部分

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Chapter17_01.h"
#include <stdbool.h>

int main(void) {

	//1.a
	/*
	struct film *head = NULL;
	struct film *prev, *current;
	char input[TSIZE];
	struct film *freeTemp;
	puts("Enter:");
	while (gets(input) != NULL&&input[0] != '\0')
	{
	current = (struct film *)malloc(sizeof(struct film));
	if (head == NULL)
	{
	head = current;
	}
	else
	{
	prev->next = current;
	}
	current->next = NULL;
	strcpy(current->title, input);
	puts("Enter rating:");
	scanf("%d", ¤t->rating);
	while (getchar() != '\n')
	continue;
	puts("Enter the next:");
	prev = current;
	}
	if (head == NULL) {
	printf("No data entered!");
	}
	else
	{
	printf("Here is the list:\n");
	}
	ShowList(head);
	printf("\n************\n");
	ReverShow(head);
	current = head;
	while (current!=NULL)
	{
	freeTemp = current->next;
	free(current);
	current = freeTemp;
	}
	printf("BYE!");
	*/

	//1.b
	/*
	struct film2 *head = NULL;
	struct film2 *prev, *current;
	char input[TSIZE];
	struct film2 *freeTemp;
	puts("Enter:");
	while (gets(input) != NULL&&input[0] != '\0')
	{
	current = (struct film2 *)malloc(sizeof(struct film2));
	if (head == NULL)
	{
	head = current;
	head->before = NULL;
	head->next = NULL;
	}
	else
	{
	current->before = prev;
	prev->next = current;
	}
	current->next = NULL;
	strcpy(current->title, input);
	puts("Enter rating:");
	scanf("%d", ¤t->rating);
	while (getchar() != '\n')
	continue;
	puts("Enter the next:");
	prev = current;
	}
	if (head == NULL) {
	printf("No data entered!");
	}
	else
	{
	printf("Here is the list:\n");
	}
	current = head;
	while (current != NULL)
	{
	printf("Movie:%s Rating:%d\n", current->title, current->rating);
	current = current->next;
	}
	printf("\n************\n");
	current = head;
	while (current != NULL)
	{
	if (current->next==NULL)
	{
	break;	//先找到队尾
	}
	current = current->next;
	}
	while (current != NULL)
	{
	printf("Movie:%s Rating:%d\n", current->title, current->rating);
	current = current->before;
	}
	//下面清空
	current = head;
	while (current != NULL)
	{
	freeTemp = current->next;
	free(current);
	current = freeTemp;
	}
	printf("BYE!");
	*/

	//2
	/*
	void showmovies(Item item);
	List movies;
	Item temp;
	InitializeList(&movies);
	if (ListIsFull(&movies))
	{
	fprintf(stderr, "No memory");
	exit(1);
	}
	puts("Enter the first title:");
	while (gets(temp.title)!= NULL&&temp.title[0] != '\0')
	{
	puts("Enter your rating:");
	scanf("%d", &temp.rating);
	while (getchar() != '\n')
	{
	continue;
	}
	if (AddItem(temp, &movies) == false)
	{
	fprintf(stderr, "problem!\n");
	break;
	}
	if (ListIsFull(&movies))
	{
	puts("The list is now full");
	break;
	}
	puts("Enter next movies:");
	}

	if (ListIsEmpty(&movies))
	{
	printf("No data entered");
	}
	else
	{
	printf("Here is the movies list:\n");
	Traverse(&movies, showmovies);
	}
	printf("You entered  %d movies.\n", ListItemCount(&movies));
	EmptyTheList(&movies);
	printf("Bye!\n");
	*/

	//3
	/*
	void showmovies(Item item);
	List2 movies;
	Item temp;
	InitializeList2(&movies);
	if (ListIsFull2(&movies))
	{
	fprintf(stderr, "No memory");
	exit(1);
	}
	puts("Enter the first title:");
	while (gets(temp.title)!= NULL&&temp.title[0] != '\0')
	{
	puts("Enter your rating:");
	scanf("%d", &temp.rating);
	while (getchar() != '\n')
	{
	continue;
	}
	if (AddItem2(temp, &movies) == false)
	{
	fprintf(stderr, "problem!\n");
	break;
	}
	if (ListIsFull2(&movies))
	{
	puts("The list is now full");
	break;
	}
	puts("Enter next movies:");
	}
	if (ListIsEmpty2(&movies))
	{
	printf("No data entered");
	}
	else
	{
	printf("Here is the movies list:\n");
	Traverse2(&movies, showmovies);
	}
	printf("You entered  %d movies.\n", ListItemCount2(&movies));
	EmptyTheList2(&movies);
	printf("Bye!\n");
	*/

	//4
/*
//此题效果作出来了,但结果不对,因为对此模型没有好的想法,但程序上来讲没有太大问题了
	Queue line1;
	Queue line2;
	Item4 temp;
	bool newcustomer(double x);
	Item4 customertime(long when);
	int hours;
	int perhour;
	long cycle, cyclelimit;
	long turnaways = 0;
	long customers = 0;
	long customers2 = 0;
	long served = 0;
	long served2 = 0;
	long sum_line = 0;
	int wait_time = 0;
	double min_per_cust;
	long line_wait = 0;
	InitializeQueue(&line1);
	InitializeQueue(&line2);
	srand(time(NULL));
	puts("Enter hours:");
	scanf("%d", &hours);
	cyclelimit = MIN_PER_HR*hours;
	puts("Enter the average per hour:");
	scanf("%d", &perhour);
	min_per_cust = MIN_PER_HR / perhour;
	for (cycle = 0; cycle < cyclelimit; cycle++)
	{
		if (newcustomer(min_per_cust))
		{
			if (QueueIsFull(&line1) && QueueIsFull(&line2))
			{
				turnaways++;
			}
			else
			{
				if (!QueueIsFull(&line1))//这个情况下,优先去line1,此处还可优化,哪边人少去哪边……
				{
					printf("runtime!\n");
					customers++;
					temp = customertime(cycle);
					EnQueue(temp, &line1);
				}
				else if (!QueueIsFull(&line2))
				{//otherwise
					printf("runtime!********************\n");
					customers2++;
					temp = customertime(cycle);
					EnQueue(temp, &line2);
				}
			}
		}
		//此处有些问题,删除的东西不匹配,造成最后结果不对,但不想再在这里耗时间了
		if (wait_time <= 0 && !QueueIsEmpty(&line1))
		{
			DeQueue(&temp, &line1);
			wait_time = temp.processtime;
			line_wait += cycle - temp.arrive;
			served++;
		}
		if (wait_time <= 0 && !QueueIsEmpty(&line2))
		{
			DeQueue(&temp, &line2);
			wait_time = temp.processtime;
			line_wait += cycle - temp.arrive;
			served2++;
		}
		if (wait_time > 0)
		{
			wait_time--;
		}
		sum_line += QueueItemCount(&line1);
		sum_line += QueueItemCount(&line2);
	}
	if ((customers + customers2) >= 0)
	{
		printf("customers accepted:%ld \n", customers + customers2);
		printf("customers served:%ld\n", served + served2);
		printf("turnaways:%ld\n", turnaways);
		printf("average queue size:%.2f\n", (double)sum_line / cyclelimit);
		printf("average wait time:%.2f minutes\n", (double)line_wait / served);
	}
	else
	{
		puts("No customers!");
	}
	EmptyTheQueue(&line1);
	puts("Bye!");
	*/
//5-8略


	return 0;
}

bool newcustomer(double x) {
	if (rand()*x / RAND_MAX < 1) {
		return true;
	}
	else
	{
		return false;
	}
}
Item4 customertime(long when) {
	Item4 cust;
	cust.processtime = rand() % 3 + 1;
	cust.arrive = when;
	return cust;
}

void showmovies(Item item) {
	printf("Movies %s rating %d\n", item.title, item.rating);
}

2. 头文件

#pragma once
#include <stdio.h>
#include <stdbool.h>

//1.a
#define TSIZE 45

struct film
{
	char title[TSIZE];
	int rating;
	struct film *next;
};

void ReverShow(struct film *head);
void ShowList(struct film *head);
//1.b另一个实现,双向链表
struct film2
{
	char title[TSIZE];
	int rating;
	struct film2 *next;
	struct film2 *before;
};

//2
#ifndef LIST_H_
#define LIST_H_
#endif // !LIST_H_

//此处借用上一个film但多出一个*next
typedef struct film Item;

typedef struct node {
	Item item;
	struct node *next;
} Node;

//typedef Node * List;


typedef struct list {
	Node *head;
	Node *end;
} 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);

//3
#define MAXSIZE 100
typedef struct list2 {
	Item entries[MAXSIZE];
	int items;
} List2;
void InitializeList2(List2 *plist);
bool ListIsEmpty2(const List2 *plist);
bool ListIsFull2(const List2 *plist);
unsigned int ListItemCount2(const List2 *plist);
bool AddItem2(Item item, List2 *plist);
void Traverse2(const List2 *plist, void(*pfun)(Item item));
void EmptyTheList2(List2 *plist);

//4
#ifndef _QUEUE_H_
#define  _QUEUE_H_
#endif // !_QUEUE_H_
#define MIN_PER_HR 60.0
typedef struct item4
{
	long arrive;
	int processtime;

} Item4;
#define MAXQUEUE 10
#include <time.h>
typedef struct node4
{
	Item4 item;
	struct node4 *next;
} Node4;
typedef struct queue4
{
	Node4 *front;
	Node4 *rear;
	int items;
} Queue;
void InitializeQueue(Queue *pq);
bool QueueIsFull(const Queue *pq);
bool QueueIsEmpty(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Item4 item, Queue *pq);
bool DeQueue(Item4 *pitem, Queue *pq);
void EmptyTheQueue(Queue *pq);
3.第一题函数实现:

//1 realization:
#include "Chapter17_01.h"

void ReverShow(const struct film *head) {
	struct film *temp;
	temp = head;
	if ((temp->next)==NULL)
	{
		printf("Movie:%s Rating:%d\n", temp->title, temp->rating);
	}
	else
	{
		ReverShow(temp->next);
		printf("Movie:%s Rating:%d\n", temp->title, temp->rating);
	}
}

void ShowList(const struct film *head) {
	struct film *temp;
	temp = head;
	while (temp != NULL)
	{
		printf("Movie:%s Rating:%d\n", temp->title, temp->rating);
		temp = temp->next;
	}


}
4.第二题函数实现

#include "Chapter17_01.h"

static void CopyToNode(Item item, Node *pnode) {
	pnode->item = item;
}
void InitializeList(List *plist) {
	plist->head = NULL;
	plist->end = NULL;
}
bool ListIsEmpty(const List *plist) {
	if (plist->head == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool ListIsFull(const List *plist) {
	Node *ptemp;
	bool full;
	ptemp = (Node *)malloc(sizeof(Node));//此处量取节点的大小,而非list元素的大小
	if (ptemp == NULL)
	{
		full = true;
	}
	else {
		full = false;
	}
	free(ptemp);
	return full;
}
unsigned int ListItemCount(const List *plist) {
	unsigned int count = 0;
	Node *pnode = plist->head;
	while (pnode!=NULL)
	{
		++count;
		pnode = pnode->next;
	}
	return count;
}
bool AddItem(Item item, List *plist) {
	Node *pnew;
	Node *scan = plist->end;
	pnew = (Node *)malloc(sizeof(Node));
	if (pnew == NULL) {
		return false;
	}
	CopyToNode(item, pnew);
	pnew->next = NULL;
	if (scan==NULL)
	{
		plist->head = pnew;//直接加到尾巴上
		plist->end = pnew;//加到尾巴上后将最后一项标记为尾巴
	}
	else
	{
		scan->next = pnew;
		plist->end = pnew;
	}
	return true;
}
void Traverse(const List *plist, void(*pfun)(Item item)) {
	Node *pnode = plist->head;
	while (pnode != NULL)
	{
		(*pfun)(pnode->item);
		pnode = pnode->next;
	}
}
void EmptyTheList(List *plist) {
	Node *psave;
	Node *temp;
	psave = plist->head;
	while (psave != NULL)
	{
		temp = psave;
		psave = psave->next;
		free(temp);
	}
}
5.第三题函数实现

#include "Chapter17_01.h"

static void CopyToNode(Item item, Node *pnode) {
	pnode->item = item;
}
void InitializeList2(List2 *plist) {
	plist->items = 0;
}
bool ListIsEmpty2(const List2 *plist) {
	if (plist->items == 0)
	{
		return true;
	}
	return false;
}
bool ListIsFull2(const List2 *plist) {
	if (plist->items >= MAXSIZE)
	{
		return true;
	}
	return false;
}
unsigned int ListItemCount2(const List2 *plist) {
	return plist->items;//以0开始,需+1
}
bool AddItem2(Item item, List2 *plist) {
	plist->entries[plist->items] = item;
	plist->items++;
	return true;
}
void Traverse2(const List2 *plist, void(*pfun)(Item item)) {
	int count = 0;
	while (count<plist->items)
	{
		(*pfun)(plist->entries[count]);
		count++;
	}
}
void EmptyTheList2(List2 *plist) {
}
6.第四题函数实现

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

static void CopyToNode4(Item4 item, Node4 *node);
static void CopyToItem4(Node4 *pn, Item4 *pi);
void InitializeQueue(Queue *pq) {
	pq->front = pq->rear = NULL;
	pq->items = 0;
}
bool QueueIsFull(const Queue *pq) {
	return pq->items == MAXQUEUE;
}
bool QueueIsEmpty(const Queue *pq) {
	return pq->items == 0;
}
int QueueItemCount(const Queue *pq) {
	return pq->items;

}
bool EnQueue(Item4 item, Queue *pq) {
	Node4 *pnew;
	if (QueueIsFull(pq))
	{
		return false;
	}
	pnew = (Node4 *)malloc(sizeof(Node4));
	if (pnew == NULL)
	{
		fprintf(stderr, "Unable to allocate memory!\n");
		exit(1);
	}
	CopyToNode4(item, pnew);
	pnew->next = NULL;
	if (QueueIsEmpty(pq))
	{
		pq->front = pnew;
	}
	else
	{
		pq->rear->next = pnew;
	}
	pq->rear = pnew;
	pq->items++;
	return true;
}
bool DeQueue(Item4 *pitem, Queue *pq) {
	Node4 *pt;
	if (QueueIsEmpty(pq))
	{
		return false;
	}
	CopyToItem4(pq->front, pitem);
	pt = pq->front;
	pq->front = pq->front->next;
	free(pt);
	pq->items--;
	if (pq->items == 0) {
		pq->rear = NULL;
	}
	return true;
}
void EmptyTheQueue(Queue *pq) {
	Item4 dummy;
	while (!QueueIsEmpty(pq))
	{
		DeQueue(&dummy, pq);
	}
}
static void CopyToNode4(Item4 item, Node4 *pn) {
	pn->item = item;
}
static void CopyToItem4(Node4 *pn, Item4 *pi) {
	*pi = pn->item;
}
后记:

没想到不到一个月的时间,竟然真的将这本大有名气的书看完了。收获颇丰。虽然仍旧是基础部分,但是相对于其他书来讲,技术上又有了一个层次的提升。接下来有点想法,看看是不是能够实现出来。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值