链表练习

#include 
#include 
#include 

struct movie_type {
	int id;
	char name[30];
}mts[20];

struct movie {
	int id;
	char name[100];
	char director[100];
	char language[100];
	int year;
	int score;
	struct movie* next;
	int type_id;
}movie_sort[20];

int if_set_type = 0;
int if_initial_movie_list = 0;
int type_num;

struct movie* head;

int check()
{
	if (!if_set_type || !if_initial_movie_list)
	{
		printf("不满足初始条件\n");
		return 1;
	}
	return 0;
}

void show_all_movies()
{

	if (check())
	{
		return;
	}

	struct movie *p = head;

	p = p->next;

	while (p != NULL)
	{
		printf("id: %d  name: %s  director: %s  language: %s  year: %d  score: %d  type_id: %d\n", p->id, p->name, p->director, p->language,
			p->year, p->score, p->type_id);

		p = p->next;
	}
}

void delete_movie()
{
	if (check()) {
		return;
	}

	show_all_movies();

	printf("请输入要删除的电影的id:");

	int id;
	scanf("%d", &id);

	struct movie *p, *q;

	q = head;
	p = head->next;
	
	while (p != NULL && id != p->id)
	{
		q = p;
		p = p->next;
	}

	if (NULL == p)
	{
		printf("没有找到指定id的电影\n");
		return;
	}

	if (id == p->id)
	{
		q->next = p->next;
		free(p);

		printf("删除成功!\n");
		return;
	}
}


void insert_at_head(struct movie *p)
{
	p->next = head->next;
	head->next = p;
}

void add_movie()
{
	if (check())
	{
		return;
	}

	struct movie *p;

	p = (struct movie*)malloc(sizeof(struct movie));

	printf("请输入电影的id:");
	scanf("%d", &p->id);
	printf("请输入电影的名字:");
	scanf("%s", &p->name);
	printf("请输入电影的导演:");
	scanf("%s", &p->director);
	printf("请输入电影的语言:");
	scanf("%s", &p->language);
	printf("请输入电影的年:");
	scanf("%d", &p->year);
	printf("请输入电影的评分:");
	scanf("%d", &p->score);
	printf("请输入电影类型的id:");
	scanf("%d", &p->type_id);

	p->next = NULL;

	insert_at_head(p);

	printf("添加成功!\n");
}

void modify_movie()
{
	if (check())
	{
		return;
	}

	printf("请输入要修改信息的电影的id:");

	int id;
	scanf("%d", &id);

	struct movie *p;

	p = head->next;

	while (p != NULL && id != p->id)
	{
		p = p->next;
	}

	if (NULL == p)
	{
		printf("没有找到指定id的电影\n");
		return;
	}

	if (id == p->id)
	{
		printf("请输入电影的名字:");

		memset(p->name, '\0', 100);
		scanf("%s", &p->name);


		printf("请输入电影的导演:");
		memset(p->director, '\0', 100);
		scanf("%s", &p->director);

		printf("请输入电影的语言:");
		memset(p->language, '\0', 100);
		scanf("%s", &p->language);

		printf("请输入电影的年:");
		scanf("%d", &p->year);
		printf("请输入电影的评分:");
		scanf("%d", &p->score);
		printf("请输入电影类型的id:");
		scanf("%d", &p->type_id);

		return;
	}
}

void show_a_movie()
{
	if (check())
	{
		return;
	}

	printf("请输入要查看的电影的id:");

	int id;
	scanf("%d", &id);

	struct movie *p;

	p = head->next;

	while (p != NULL && id != p->id)
	{
		p = p->next;
	}

	if (NULL == p)
	{
		printf("没有找到指定id的电影\n");
		return;
	}

	if (id == p->id)
	{
		printf("id: %d  name: %s  director: %s  language: %s  year: %d  score: %d  type_id: %d\n", p->id, p->name, p->director, p->language,
			p->year, p->score, p->type_id);

		return;
	}
}

void show_movie_types()
{
	if (!if_set_type)
	{
		return;
	}

	printf("\n电影类别如下:\n");

	int i;
	for (i = 0; i < type_num; i++)
	{
		printf("id: %d  name: %s\n", mts[i].id, mts[i].name);
	}
}

void set_movie_types()
{
	if (if_set_type)
	{
		printf("已经设置电影类别信息,不可修改!");
		return;
	}
	else
	{
		printf("请输入电影类型数量: ");
		scanf("%d", &type_num);
		int i;
		for (i = 0; i < type_num; i++)
		{
			mts[i].id = i;
			printf("请输入类别:");
			scanf("%s", &mts[i].name);
		}
		printf("设置成功!\n");
	}

	if_set_type = 1;
}

void show_cmd()
{
	printf("*需要依次执行 1.设置电影类别 和 2.初始化电影链表 才能执行其他操作\n");
	printf("1.设置电影类别\n");
	printf("2.初始化电影链表\n");
	printf("3.添加电影信息\n");
	printf("4.删除电影信息\n");
	printf("5.显示某部电影的信息\n");
	printf("6.修改电影信息\n");
	printf("7.将某个类别的电影按评分进行排序\n");
	printf("8.显示全部电影信息\n");
	printf("0.退出系统\n");
}

void initial_movie_list()
{
	if (!if_set_type)
	{
		printf("不满足初始条件\n");
		return;
	}

	if (if_initial_movie_list)
	{
		printf("已经初始化电影列表,不能重复操作!\n");
		return;
	}

	int n;

	show_movie_types();

	printf("\n请输入电影数量:");
	scanf("%d", &n);
	printf("\n");

	int i;
	for (i = 0; i < n; i++)
	{
		struct movie *p;

		p = (struct movie*)malloc(sizeof(struct movie));
		printf("\n请输入电影的id:");
		scanf("%d", &p->id);
		printf("请输入电影的名字:");
		scanf("%s", &p->name);
		printf("请输入电影的导演:");
		scanf("%s", &p->director);
		printf("请输入电影的语言:");
		scanf("%s", &p->language);
		printf("请输入电影的年:");
		scanf("%d", &p->year);
		printf("请输入电影的评分:");
		scanf("%d", &p->score);
		printf("请输入电影类型的id:");
		scanf("%d", &p->type_id);

		printf("\n");

		p->next = NULL;

		insert_at_head(p);
	}

	printf("电影链表初始化成功!\n");

	if_initial_movie_list = 1;
}

void initial()
{
	head = (struct movie*)malloc(sizeof(struct movie));
	head->next = NULL;
}

void destroy_movie_list()
{
	// todo
	// 没有释放电影链表占用的内存空间,虽然并无影响。
}

void sort()
{

	if (check())
	{
		return;
	}

	show_movie_types();
	printf("请输入一个电影类型的id,程序会将该类型的电影按照评分进行升序排序: ");

	int id;
	scanf("%d", &id);

	int n = 0;

	// 找到所有指定类型的电影

	struct movie *p;

	p = head->next;

	while (p != NULL)
	{
		if (id == p->type_id)
		{
			memcpy(&movie_sort[n++], p, sizeof(struct movie));
		}

		p = p->next;
	}

	if (n == 0)
	{
		printf("没有找到该类型的电影\n");
		return;
	}

	int i, j;

	struct movie temp;

	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - 1 - i; j++)
		{
			if (movie_sort[j].score > movie_sort[j + 1].score)
			{
				temp = movie_sort[j];
				movie_sort[j] = movie_sort[j + 1];
				movie_sort[j + 1] = temp;
			}
		}
	}

	printf("共找到%d部类型的电影\n", n);

	for (i = 0; i < n; i++)
	{
		printf("id: %d  name: %s  director: %s  language: %s  year: %d  score: %d  type_id: %d\n", movie_sort[i].id, movie_sort[i].name, movie_sort[i].director, movie_sort[i].language,
			movie_sort[i].year, movie_sort[i].score, movie_sort[i].type_id);
	}
}

int main()
{
	initial();

	int cmd;
	while (1)
	{
		show_cmd();
		scanf("%d", &cmd);
		switch (cmd)
		{
		case 1:set_movie_types(); break;
		case 2:initial_movie_list(); break;
		case 3:add_movie(); break;
		case 4:delete_movie(); break;
		case 5:show_a_movie(); break;
		case 6:modify_movie(); break;
		case 7:sort(); break;
		case 8:show_all_movies(); break;
		case 0:return 0;
		}
		system("pause");
		system("cls");
	}
	return 0;
}

你可能感兴趣的:(c)