/*
电影评分表程序,可以增加新电影,删除电影,显示电影评分列表
实现方式:无头节点单链表
主要数据:
Film, FilmNode, FilmList三种类型
全局变量 FilmList g_L
主要函数:
1.链表操作函数
void filmListInit(FilmList * L);
void filmListDestory(FilmList * L);
void filmListInsert(FilmList * L, int pos, const Film * e);
void filmListDelete(FilmList * L, int pos, Film * e);
void filmListTraverse(FilmList L);
int filmListLength(FilmList L);
2.框架函数
char film_welcome();
void film_insert_mod();
void film_delete_mod();
void film_display_mod();
void film_main_mod();
*/
#include
#include
#define FILM_NAME_LEN 50
const char * const prompt = "=";
//Film数据元素
typedef struct Film{
char name[FILM_NAME_LEN];
float score;
}Film;
//Film数据节点
typedef struct FilmNode{
FilmNode * next;
Film data;
} FilmNode;
//Film表
typedef FilmNode* FilmList;
FilmList g_L = NULL;
void filmListInit(FilmList * L);
void filmListDestory(FilmList * L);
void filmListInsert(FilmList * L, int pos, const Film * e);
void filmListDelete(FilmList * L, int pos, Film * e);
void filmListTraverse(FilmList L);
int filmListLength(FilmList L);
char film_welcome();
void film_insert_mod();
void film_delete_mod();
void film_display_mod();
void film_main_mod();
int main()
{
film_main_mod();
return 0;
}
void filmListInit(FilmList * L)
{
*L = NULL;
}
void filmListDestory(FilmList * L)
{
FilmList p;
while(*L){
p = *L;
*L = p->next;
free(p);
}
*L = NULL;
}
void filmListInsert(FilmList * L, int pos, const Film * e)
{
int i = 1;
FilmList p = *L, d;
d = (FilmList)malloc(sizeof(FilmNode));
d->data = *e;
if(1 == pos){
d->next = *L;
*L = d;
}
else{
while(p && i < pos -1){
p = p->next;
++i;
}
if(NULL == p){
printf("wrong position\n");
return;
}
else{
d->next = p->next;
p->next = d;
}
}
}
void filmListDelete(FilmList * L, int pos, Film * e)
{
int i = 1;
FilmList p, q = NULL;
p = *L;
if(NULL == L || NULL == *L)
return;
if(1 == pos){
*L = p->next;
free(p);
return;
}
else{
while(p->next && i < pos - 1){
p = p->next;
++i;
}
if(NULL == p->next || i != pos - 1){
return;
}
else{
q = p->next;
p->next = q->next;
free(q);
return;
}
}
}
void filmListTraverse(FilmList L)
{
int i = 1;
FilmList p = L;
while(p){
printf("%-2d %-30s %2.1f\n", i, p->data.name, p->data.score);
p = p->next;
++i;
}
}
int filmListLength(FilmList L)
{
int i = 0;
while(L){
L = L->next;
++i;
}
return i;
}
void film_main_mod()
{
char flag = '\0';
do{
flag = film_welcome();
switch(flag){
case '1':film_insert_mod();break;
case '2':film_delete_mod();break;
case '3':film_display_mod();break;
case '0':flag = '\0';break;
default:
printf("-[!error]-:wrong input,please use 1,2,3,0\n");
break;
}
}while(flag != '\0');
filmListDestory(&g_L);
}
char film_welcome()
{
printf("----\n");
printf("operate film table: 1.insert 2.delete 3.display 0.exit\n%s", prompt);
char flag = '\0';
fflush(stdin);
scanf("%c", &flag);
return flag;
}
void film_insert_mod()
{
printf("-------\n");
Film t;
printf("the name of new film:\n%s", prompt);
fflush(stdin);
scanf("%s",t.name);
printf("the score of new film:\n%s", prompt);
fflush(stdin);
scanf("%f", &t.score);
filmListInsert(&g_L, 1, &t);
}
void film_delete_mod()
{
printf("-------\n");
int film_num = 0;
Film tmp;
printf("enter film number:\n%s", prompt);
fflush(stdin);
scanf("%d", &film_num);
if(film_num < 1 || film_num > filmListLength(g_L)){
printf("-[!error]-:wrong position\n");
return;
}
filmListDelete(&g_L, film_num, &tmp);
}
void film_display_mod()
{
printf("-------\n");
printf("---------new film list:\n");
filmListTraverse(g_L);
printf("-------------------------------------\n");
}
修改了下界面
/*
电影评分表程序,可以增加新电影,删除电影,显示电影评分列表
实现方式:无头节点单链表
主要数据:
Film, FilmNode, FilmList三种类型
全局变量 FilmList g_L
主要函数:
1.链表操作函数
void filmListInit(FilmList * L);
void filmListDestory(FilmList * L);
void filmListInsert(FilmList * L, int pos, const Film * e);
void filmListDelete(FilmList * L, int pos, Film * e);
void filmListTraverse(FilmList L);
int filmListLength(FilmList L);
2.框架函数
char film_welcome();
void film_insert_mod();
void film_delete_mod();
void film_display_mod();
void film_main_mod();
*/
#include
#include
#define FILM_NAME_LEN 50
const char * const prompt = "=";
//Film数据元素
typedef struct Film{
char name[FILM_NAME_LEN];
float score;
}Film;
//Film数据节点
typedef struct FilmNode{
FilmNode * next;
Film data;
} FilmNode;
//Film表
typedef FilmNode* FilmList;
FilmList g_L = NULL;
void filmListInit(FilmList * L);
void filmListDestory(FilmList * L);
void filmListInsert(FilmList * L, int pos, const Film * e);
void filmListDelete(FilmList * L, int pos, Film * e);
void filmListTraverse(FilmList L);
int filmListLength(FilmList L);
char film_welcome();
void film_insert_mod();
void film_delete_mod();
void film_display_mod();
void film_main_mod();
int main()
{
film_main_mod();
return 0;
}
void filmListInit(FilmList * L)
{
*L = NULL;
}
void filmListDestory(FilmList * L)
{
FilmList p;
while(*L){
p = *L;
*L = p->next;
free(p);
}
*L = NULL;
}
void filmListInsert(FilmList * L, int pos, const Film * e)
{
int i = 1;
FilmList p = *L, d;
d = (FilmList)malloc(sizeof(FilmNode));
d->data = *e;
if(1 == pos){
d->next = *L;
*L = d;
}
else{
while(p && i < pos -1){
p = p->next;
++i;
}
if(NULL == p){
printf("wrong position\n");
return;
}
else{
d->next = p->next;
p->next = d;
}
}
}
void filmListDelete(FilmList * L, int pos, Film * e)
{
int i = 1;
FilmList p, q = NULL;
p = *L;
if(NULL == L || NULL == *L)
return;
if(1 == pos){
*L = p->next;
free(p);
return;
}
else{
while(p->next && i < pos - 1){
p = p->next;
++i;
}
if(NULL == p->next || i != pos - 1){
return;
}
else{
q = p->next;
p->next = q->next;
free(q);
return;
}
}
}
void filmListTraverse(FilmList L)
{
int i = 1;
FilmList p = L;
while(p){
printf("%-2d %-30s %2.1f\n", i, p->data.name, p->data.score);
p = p->next;
++i;
}
}
int filmListLength(FilmList L)
{
int i = 0;
while(L){
L = L->next;
++i;
}
return i;
}
void film_main_mod()
{
char flag = '\0';
do{
flag = film_welcome();
switch(flag){
case '1':film_insert_mod();break;
case '2':film_delete_mod();break;
case '3':film_display_mod();break;
case '0':flag = '\0';break;
default:
printf(" !error\n wrong input,please use 1,2,3,0\n");
break;
}
}while(flag != '\0');
filmListDestory(&g_L);
}
char film_welcome()
{
printf(" -
printf("operate film table: 1.insert 2.delete 3.display 0.exit\n%s", prompt);
char flag = '\0';
fflush(stdin);
scanf("%c", &flag);
return flag;
}
void film_insert_mod()
{
printf(" -
Film t;
printf("the name of new film:\n%s", prompt);
fflush(stdin);
scanf("%s",t.name);
printf("the score of new film:\n%s", prompt);
fflush(stdin);
scanf("%f", &t.score);
filmListInsert(&g_L, 1, &t);
}
void film_delete_mod()
{
printf(" -
int film_num = 0;
Film tmp;
printf("enter film number:\n%s", prompt);
fflush(stdin);
scanf("%d", &film_num);
if(film_num < 1 || film_num > filmListLength(g_L)){
printf(" !error\n wrong position\n");
return;
}
filmListDelete(&g_L, film_num, &tmp);
}
void film_display_mod()
{
printf(" -
printf(" -
filmListTraverse(g_L);
printf("-------------------------------------\n");
}