数据结构实验课结课考核作业,新手上路,做得不好的地方还请多多指教
添加新书(直接加在末尾)
int Initialization(LinkList L, Book e) { LNode *p; p= L; while(p->next) { p=p->next; } LNode *s; s = (LNode *)malloc(sizeof(LNode)); s->elem = e; s->next = p->next; p->next= s; return OK; }
插入(在任意位置插入)
int Insert(LinkList L, Book e,int Location)//设想插入3 已有两个数据 L - A:[1||A->B] - B:[2||B->...] - null { int location=1;//位置标识 LNode *p; LNode * s; s = (LNode *)malloc(sizeof(LNode)); // s: [NULL||->...] p= L->next;//L-A <-p if(p==NULL) { printf("当前书架为空,将为您添加新书.\n"); system("pause"); goto Add; } while(p&&location<Location)//定位插入点 1 2 { p=p->next; // 1 2 移动两格 L-A <-p || L-A-B <-p 定位到B if(p==NULL) { system("cls"); printf("插入成功!\n"); goto Add; } if(p->next==NULL) { printf("书架中插入的位置不存在,将帮您放在末位.\n"); system("pause"); goto Add; } location++; // 2 3 } s->next=p->next;//继承前驱 L-A-B s: [NULL||s->B] p->next=s;//对接上一节点 L - [1||A->s] - [NULL||s->B] -> B s->elem=p->elem;//交换数据 L-A- [2||s->p] - [NULL||p->...] p->elem=e; //接入新元素 L - A - s - [e||p->...] 新数据已插入的第3位 if(0)//独立空间 { Add://这里引入添加新书模块 LNode *p; p= L; while(p->next) { p=p->next; } LNode *s; s = (LNode *)malloc(sizeof(LNode)); s->elem = e; s->next = p->next; p->next= s; Stop(5000); Stop(5000); system("cls"); TraverseList(L); system("pause"); return OK; } if(p==NULL) { printf("插入失败!\n"); system("pause"); return ERROR; } else { printf("插入成功!\n"); Stop(5000); TraverseList(L); system("pause"); return OK; } }
查找(全遍历查找)
int Serch(LinkList L,char *bookname) { int mark=0; printf("\n"); Stop(5000); system("cls"); LNode *p; p = L->next; while(p) { while(p&& strcmp(p->elem.title,bookname)!=0) { p = p->next; } if(p) { mark++; printf("have this book.\n"); printf("ISBN:%s Title:%s Author:%s Press House:%s Price:%f\n",p->elem.ISBN,p->elem.title,p->elem.author,p->elem.pressHouse,p->elem.price); p=p->next; } } if(mark) { printf("\nWe find that it has %d\n",mark); } else { printf("Cann't find it!\n"); } return OK; }
删除
int Delete(LinkList L,char *ISBN) { LNode *p; LNode *prior; p= L; while(p && strcmp(p->elem.ISBN,ISBN)!=0) { prior = p; p = p->next; } if(p==NULL) { printf("没有找到该图书…\n"); } else { prior->next = p->next; printf("已删除!\n"); Stop(5000); TraverseList(L); } return OK; }
修改
int Modify(LinkList L,char *ISBN,char *title,char *author,char *pressHouse,float price) { LNode *p; p = L->next; while(p&& strcmp(p->elem.ISBN,ISBN)!=0) { p = p->next; } if(p==NULL) { printf("Can't find ISBN!\n"); system("pause"); return ERROR; } else { strcpy(p->elem.title,title); strcpy(p->elem.author,author); strcpy(p->elem.pressHouse,pressHouse); p->elem.price = price; } return OK; }
统计
int Count(LinkList L) { LNode *p; int cnt = 0; p = L->next; while(p) { cnt++; p = p->next; } return cnt; }
排序
int bubbleSort(LinkList L) { if(L->next == NULL || L->next->next == NULL)return OK; int flag = 0; LNode *cur; //游标节点 LNode *pre; //游标节点的前驱节点 LNode *tail = NULL; //尾指针节点 LNode *temp; //临时节点 while(L->next != tail) { cur = L->next; pre = L; //确保pre是cur前面的那个节点 while(cur->next != tail ) { if(strcmp(cur->elem.ISBN,cur->next->elem.ISBN)>0) //根据ISBN的大小进行排序 { temp = cur->next; cur->next = temp->next; temp->next = cur; pre->next = temp; pre = temp; flag = 1; } else { pre = pre->next; cur=cur->next; } } if(flag == 0) { printf("->\n"); return OK; } tail = cur; //每次循环结束后,最后一个节点就是最大的元素,尾节点往前挪一位 }
完整代码
//图书管理系统(数据结构基于c语言) #include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 1 #define ERROR 0 typedef struct//书 { char ISBN[20];//编号 char title[20];//书名 char author[20];//作者 char pressHouse[20];//出版社 float price;//价格 }Book; typedef struct LNode//链表 { Book elem; struct LNode *next; }LNode, *LinkList; //设计功能函数预定义区 void StarShow(LinkList &L);//开始界面设计 void Stop(int i);//延迟函数 int signCheck(char x);//选择序号监测 输入已有选项则返回假 否则返回真 //小程序功能函数预定义区 int Initialization(LinkList L, Book e);//添加新书 int Insert(LinkList L, Book e, int Location);//插入 int TraverseList(LinkList L);//遍历 int Serch(LinkList L,char *bookname);//查找 int Delete(LinkList L,char *title);//删除 int Modify(LinkList L,char *ISBN,char *title,char *author,char *pressHouse,float price);//修改 int Count(LinkList L);//统计 int bubbleSort(LinkList L);//排序 int check(LinkList L,char *ISBN);//检查是否编码重复 是则返回真 否则返回假 //主函数 int main() { char sign;//char选择序号 存入键盘输入的字符 int Sign;//int 选择序号 储存转化为int后的sign int Location;//位置 用于定位插入点 LinkList L;//表对象 char ISBN[20];//编号 暂存键盘输入的信息 把ISBN当作特征变量 要求每本书的编码都不一样 char title[20];//书名 暂存键盘输入的信息 char author[20];//作者 暂存键盘输入的信息 char pressHouse[20];//出版社 暂存键盘输入的信息 float price;//价格 暂存键盘输入的信息 Book e;//书对象,用于储存输入的book信息 int cnt;// 用于存储书本的数量信息 L = (LNode *)malloc(sizeof(LNode));//为链表开辟空间 L->next = NULL;//把指针域赋值为空,避免不必要的错误 comeStar://程序入口 system("cls");//清屏 重置 StarShow(L);//展示一些必要的提示信息 scanf("%c",&sign);//获取用户选择 while(getchar()!=10)getchar();//吸收无用字符串 if(!signCheck(sign))goto comeStar;//判断选择序号是否正确 是则继续 否则回到入口点 while((Sign=signCheck(sign)))//当选择正确时进入循环体 { switch (Sign) { case 1://添加新书 system("cls"); printf("1 添加新书\n\n\n"); printf("请输入书的编号、名称、作者、出版社、价格(用空格隔开):"); scanf("%s %s %s %s %f",ISBN,title,author,pressHouse,&price); while(getchar()!=10)getchar();//吸收无用字符串 if(check(L,ISBN))goto comeStar;//检测ISBN是否已经存在 strcpy(e.ISBN,ISBN); strcpy(e.title,title); strcpy(e.author,author); strcpy(e.pressHouse,pressHouse); e.price = price; Initialization(L,e); system("cls"); printf("添加成功!\n"); TraverseList(L); Stop(5000); system("pause"); goto comeStar; break; case 2://插入 system("cls"); printf("2 插入\n\n\n"); printf("请输入要插入的位置(输入0返回):"); scanf("%d",&Location); if(Location==0)goto comeStar; while(getchar()!=10)getchar();//吸收无用字符串 printf("请输入书的编号、名称、作者、出版社、价格(用空格隔开):"); scanf("%s %s %s %s %f",ISBN,title,author,pressHouse,&price); while(getchar()!=10)getchar();//吸收无用字符串 if(check(L,ISBN))goto comeStar;//检测ISBN是否已经存在 strcpy(e.ISBN,ISBN); strcpy(e.title,title); strcpy(e.author,author); strcpy(e.pressHouse,pressHouse); e.price = price; Insert(L,e,Location); goto comeStar; break; case 3://查找 system("cls"); printf("3 查找\n\n\n"); if(TraverseList(L)) { system("pause"); goto comeStar; } printf("请输入要查找的书名(输入weback返回):"); scanf("%s",title); if(!strcmp(title,"weback"))goto comeStar; while(getchar()!=10)getchar();//吸收无用字符串 Stop(5000); Serch(L,title); system("pause"); goto comeStar; break; case 4://删除 system("cls"); printf("4 删除\n\n\n"); printf("书架:\n"); if(TraverseList(L)) { system("pause"); goto comeStar; } printf("请输入要删除的书的编码(输入weback返回):"); scanf("%s",ISBN); if(!strcmp(ISBN,"weback"))goto comeStar; while(getchar()!=10)getchar();//吸收无用字符串 Stop(5000); system("cls"); Delete(L,ISBN); system("pause"); goto comeStar; break; case 5://修改 system("cls"); printf("5 修改\n\n\n"); printf("书架:\n"); if(TraverseList(L)) { system("pause"); goto comeStar; } printf("\n请输入需要修改的书的编码(输入weback返回):"); scanf("%s",ISBN); if(!strcmp(ISBN,"weback"))goto comeStar; while(getchar()!=10)getchar(); printf("请输入修改后的名称、作者、出版社、价格(空格隔开):"); scanf(" %s %s %s %f",title,author,pressHouse,&price); while(getchar()!=10)getchar(); if(!Modify(L,ISBN,title,author,pressHouse,price))goto comeStar; Stop(5000); printf("\n\n修改完成!\n"); Stop(5000); TraverseList(L); system("pause"); goto comeStar; break; case 6://统计 system("cls"); printf("6 统计\n\n\n"); if(TraverseList(L)) { system("pause"); goto comeStar; } Stop(5000); cnt = Count(L); printf("\nWe find that it has %d\n",cnt); Stop(5000); system("pause"); goto comeStar; break; case 7://排序 system("cls"); printf("7 排序\n\n\n"); if(TraverseList(L)) { system("pause"); goto comeStar; } bubbleSort(L); TraverseList(L); Stop(5000); system("cls"); printf("排序完成!\n"); Stop(5000); system("pause"); goto comeStar; break; case 8: system("cls"); printf("退出中.......\n\n\n"); Stop(5000); system("cls"); Stop(5000); break; } break; } return 0; } //设计功能函数定义区 void StarShow(LinkList &L) { printf("书架:\n"); TraverseList(L); 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("请选择功能:"); } void Stop(int i) { int x,y; for(x=0;x<i*i*i;x++) for(x=0;x<i*i*i;x++){} } int signCheck(char x) { int y; if(x=='1'||x=='2'|| x=='3'||x=='4'||x=='5'||x=='6'||x=='7'||x=='8') { y=x-48; return y; } else { system("cls"); printf("请输入正确的序号!\n"); system("pause"); return ERROR; } } //小程序功能函数定义区 int Initialization(LinkList L, Book e) { LNode *p; p= L; while(p->next) { p=p->next; } LNode *s; s = (LNode *)malloc(sizeof(LNode)); s->elem = e; s->next = p->next; p->next= s; return OK; } int Insert(LinkList L, Book e,int Location)//设想插入3 已有两个数据 L - A:[1||A->B] - B:[2||B->...] - null { int location=1;//位置标识 LNode *p; LNode * s; s = (LNode *)malloc(sizeof(LNode)); // s: [NULL||->...] p= L->next;//L-A <-p if(p==NULL) { printf("当前书架为空,将为您添加新书.\n"); system("pause"); goto Add; } while(p&&location<Location)//定位插入点 1 2 { p=p->next; // 1 2 移动两格 L-A <-p || L-A-B <-p 定位到B if(p==NULL) { system("cls"); printf("插入成功!\n"); goto Add; } if(p->next==NULL) { printf("书架中插入的位置不存在,将帮您放在末位.\n"); system("pause"); goto Add; } location++; // 2 3 } s->next=p->next;//继承前驱 L-A-B s: [NULL||s->B] p->next=s;//对接上一节点 L - [1||A->s] - [NULL||s->B] -> B s->elem=p->elem;//交换数据 L-A- [2||s->p] - [NULL||p->...] p->elem=e; //接入新元素 L - A - s - [e||p->...] 新数据已插入的第3位 if(0)//独立空间 { Add://这里引入添加新书模块 LNode *p; p= L; while(p->next) { p=p->next; } LNode *s; s = (LNode *)malloc(sizeof(LNode)); s->elem = e; s->next = p->next; p->next= s; Stop(5000); Stop(5000); system("cls"); TraverseList(L); system("pause"); return OK; } if(p==NULL) { printf("插入失败!\n"); system("pause"); return ERROR; } else { printf("插入成功!\n"); Stop(5000); TraverseList(L); system("pause"); return OK; } } int TraverseList(LinkList L) { LNode *p; p=L->next; if(p==NULL) { printf("书架上没有书!\n"); return OK; } else while(p) { printf("ISBN:%s Title:%s Author:%s Press House:%s Price:%f\n",p->elem.ISBN,p->elem.title,p->elem.author,p->elem.pressHouse,p->elem.price); p = p->next; } return ERROR; } int Serch(LinkList L,char *bookname) { int mark=0; printf("\n"); Stop(5000); system("cls"); LNode *p; p = L->next; while(p) { while(p&& strcmp(p->elem.title,bookname)!=0) { p = p->next; } if(p) { mark++; printf("have this book.\n"); printf("ISBN:%s Title:%s Author:%s Press House:%s Price:%f\n",p->elem.ISBN,p->elem.title,p->elem.author,p->elem.pressHouse,p->elem.price); p=p->next; } } if(mark) { printf("\nWe find that it has %d\n",mark); } else { printf("Cann't find it!\n"); } return OK; } int Delete(LinkList L,char *ISBN) { LNode *p; LNode *prior; p= L; while(p && strcmp(p->elem.ISBN,ISBN)!=0) { prior = p; p = p->next; } if(p==NULL) { printf("没有找到该图书…\n"); } else { prior->next = p->next; printf("已删除!\n"); Stop(5000); TraverseList(L); } return OK; } int Modify(LinkList L,char *ISBN,char *title,char *author,char *pressHouse,float price) { LNode *p; p = L->next; while(p&& strcmp(p->elem.ISBN,ISBN)!=0) { p = p->next; } if(p==NULL) { printf("Can't find ISBN!\n"); system("pause"); return ERROR; } else { strcpy(p->elem.title,title); strcpy(p->elem.author,author); strcpy(p->elem.pressHouse,pressHouse); p->elem.price = price; } return OK; } int Count(LinkList L) { LNode *p; int cnt = 0; p = L->next; while(p) { cnt++; p = p->next; } return cnt; } int bubbleSort(LinkList L) { if(L->next == NULL || L->next->next == NULL)return OK; int flag = 0; LNode *cur; //游标节点 LNode *pre; //游标节点的前驱节点 LNode *tail = NULL; //尾指针节点 LNode *temp; //临时节点 while(L->next != tail) { cur = L->next; pre = L; //这样确保pre是cur前面的那个节点 while(cur->next != tail ) { if(strcmp(cur->elem.ISBN,cur->next->elem.ISBN)>0) //这里是根据ISBN的大小进行排序 { temp = cur->next; cur->next = temp->next; temp->next = cur; pre->next = temp; pre = temp; flag = 1; } else { pre = pre->next; cur=cur->next; } } if(flag == 0) { printf("->\n"); return OK; } tail = cur; //使得每次循环结束后,最后一个节点就是最大的元素,尾节点往前挪一位 } } int check(LinkList L,char *ISBN) { LNode *p; p= L; while(p && strcmp(p->elem.ISBN,ISBN)!=0) { p = p->next; } if(p==NULL) { return ERROR; } else { system("cls"); printf("编码已存在!!\n"); system("pause"); return OK; } }