求大佬修改一下或者解释一下
如何构建一个总目录,选择录入项目,然后提示录入,完成后返回总目录,在显示总目录 然后在选着别的项目比如查看 删除项目;怎么搭建这个要求 解释下理论也行 没思路
程序代码:
/* * films3.c * * Created on: 2018年12月19日 * Author: Administrator */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include "List.h" /* */ /*void showmoives(Item item); */ void Showmovies(Item item); char * s_gets(char * st,int n); /*main */ int main(void) { List movies; Item temp; InitializeList(&movies); if(ListIsfull(&movies)) { fprintf(stderr,"No memory list available! Bye! \n"); exit(1); } puts("Enter first movie title: \n"); while (s_gets(temp.title,TSIZE) != NULL && temp.title[0] != '\0') { puts("Enter your rating<0-10> \n"); scanf("%d",&temp.rating); while(getchar() !='\n') continue; if(AddItem(temp,&movies) == false) { fprintf(stderr,"Problem allocation memory!\n"); // stderr mean break; } if(ListIsfull(&movies)) { puts("The List is now full!! \n"); break; } puts("Enter next movies 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("Movies: %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; } /*List.c*/ /* * List.c * * Created on: 2018年12月19日 * Author: Administrator */ /*#include<stdio.h> #include[color=#800000]<stdlib.h> #include <stdbool.h> #include"List.h" [/color]*/ /* * typedef int BOOL; #define true 1 #define false 0 * */ /*接口函数 */ static void CopyToNode(Item item,Node *pnod); /*void InitializeList( List * plist) { *plist == NULL; } */ void InitializeList(List * plist) { *plist = NULL; } /*链表为空,返回true */ _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; 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 ,* scan; scan = (*plist); pnew=(Node *)malloc(sizeof(Node)); if(pnew == NULL) return false; CopyToNode(item,pnew); pnew->next=NULL; if(scan==NULL) /*空链表所以把pnew放到链表的开头*/ *plist = pnew; else { while(scan->next != NULL) scan=scan->next;/*找到链表末尾*/ scan->next=pnew;/* 把pnew赋值scan.next*/ } return true; } /*局部函数定义*/ //static void CopyToNode(item,pnew) /* * { pnew->next = NULL; if(scan == NULL) *plist = pnew; else { while(scan->next != NULL) scan =scan->next; scan->next = pnew; } retun true; } */ /*访问每个函数并执行pfun指向的函数 */ void Traverse(const List * plist ,void (*pfun)(Item item)) { Node *pnode = *plist; while(pnode != NULL) { (*pfun)(pnode->item);/*把函数应运于链表的每一项 */ pnode= pnode->next;/*前进到下一项 */ } } /*释放malloc分配的内存*/ /* */ void EmptyTheList(List * plist) { Node * psave; while(*plist != NULL) { psave = (*plist)->next; free(plist); *plist=psave; } } static void CopyToNode(Item item,Node *pnod) { pnod->item =item; }
程序代码:
/* * List.h * * Created on: 2018年12月19日 * Author: Administrator */ #ifndef LIST_H_ #define LIST_H_ #include <stdbool.h> /*enum bool {false,true};*/ /*特定程序声明*/ #define TSIZE 45 struct film { char title[TSIZE]; unsigned 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_H_ */