与单链表相比只是修改略微的代码就可实现
为啥返回为h?
#include<stdio.h>
#include<malloc.h>
#define ListSize 100
struct Data{
int bookID;
char bookName[20];
double bookPrice;
};
typedef Data DataType;
typedef struct BookList{
DataType data;
struct BookList *next;
};
//单链表初始化
BookList * InitList(BookList *h){
h=(BookList *)malloc(sizeof(BookList));//创建头结点
if(h==NULL){
return 0;
}
h->next=h;
return h;
}
//创建循环单链表
BookList * InputList(BookList *h){
BookList *s,*r;
int num;
r=h;
printf("==============================创建图书信息==============================\n");
printf("请输入图书的数量:\n");
scanf("%d",&num);
for(int i=0;i<num;i++){
printf("请输入第%d个图书信息:\n",i+1);
s=(BookList*)malloc(sizeof(BookList));//创建存储数据的新结点
printf("请输入图书编号:\n");
scanf("%d",&s->data.bookID);
printf("请输入图书名称:\n");
scanf("%s",&s->data.bookName);
printf("请输入图书价格:\n");
scanf("%lf",&s->data.bookPrice);
r->next=s;
r=s;
}
r->next=h;
printf("创建成功!\n");
return h; //为啥返回h不返回的是r
}
//输出图书信息
void OutputList(BookList *L){
int i=0;
printf("==============================输出图书信息==============================\n");
printf("图书编号 图书名称 图书价格\n");
BookList *p,*d;
p=L->next;
while(p->next!=L->next){
printf(" %d %s %.2lf\n", p->data.bookID, p->data.bookName, p->data.bookPrice);
p=p->next;
}
printf("\n");
}
//增加图书信息表
int InsertList(BookList *L){
printf("==============================增加图书信息==============================\n");
BookList *p,*s;
int num,j=0;
p=L->next;
printf("请输入新增数据的位置:\n");
scanf("%d",&num);
while(p!=L&&j<num-1){
p=p->next;
j++;
}
if(j!=num-1){
printf("插入位置不存在\n");
return 0;
}
s=(BookList*)malloc(sizeof(BookList));
printf("请输入图书编号:\n");
scanf("%d",&s->data.bookID);
printf("请输入图书名称:\n");
scanf("%s",&s->data.bookName);
printf("请输入图书价格:\n");
scanf("%lf",&s->data.bookPrice);
s->next=p->next;
p->next=s;
printf("新增成功\n");
return 1;
}
//删除图书信息表
int DeleteList(BookList *L){
BookList *r,*pre;
int num,j=0;
r=L->next;
printf("==============================删除图书信息==============================\n");
printf("请输入删除的图书编号:\n");
scanf("%d",&num);
while(r!=L){
if(r->data.bookID==num){
pre->next=r->next;
free(r);
printf("删除成功\n");
return 1;
}
pre=r;
r=r->next;
}
return 0;
}
//修改图书信息表
int UpdateList(BookList *L){
BookList *pre,*r;
int num;
r=L->next;
printf("==============================修改图书信息==============================\n");
printf("请输入修改的图书编号:\n");
scanf("%d",&num);
while(r!=L){
if(r->data.bookID==num){
printf("请输入图书编号:\n");
scanf("%d",&r->data.bookID);
printf("请输入图书名称:\n");
scanf("%s",&r->data.bookName);
printf("请输入图书价格:\n");
scanf("%lf",&r->data.bookPrice);
return 1;
}
r=r->next;
}
return 0;
}
// 查找图书信息表
int SelectList(BookList *L){
BookList *r,*pre;
int num;
r=L->next;
printf("==============================查找图书信息==============================\n");
printf("请输入查找的图书编号:\n");
scanf("%d",&num);
while(r!=L){
if(r->data.bookID==num){
printf("图书编号 图书名称 图书价格\n");
printf(" %d %s %.2lf\n", r->data.bookID, r->data.bookName, r->data.bookPrice);
return 1;
}
r=r->next;
}
printf("sorry,不存在此图书\n");
return 0;
}
int main(){
BookList *h;
BookList *L=NULL;
DataType e;
int n,flag=0;
do{
printf("********************************************\n");
printf("* 1.创建图书信息表 *\n");
printf("* 2.输入图书信息表 *\n");
printf("* 3.输出图书信息表 *\n");
printf("* 4.增加图书信息表 *\n");
printf("* 5.删除图书信息表 *\n");
printf("* 6.修改图书信息表 *\n");
printf("* 7.查找图书信息表 *\n");
printf("* 0.退出系统 *\n");
printf("********************************************\n");
printf("请根据提示操作!\n");
scanf("%d",&n);
switch(n){
case 1:
h=InitList(h);
printf("初始化成功!!\n");
break;
case 2:
L=InputList(h);
break;
case 3:
OutputList(L);
break;
case 4:
InsertList(L);
break;
case 5:
DeleteList(L);
break;
case 6:
UpdateList(L);
break;
case 7:
SelectList(L);
break;
case 0:
flag=1;
break;
default:
printf("请输入正确的图书操作类型\n");
break;
}
}while(flag!=1);
}
一法通百法通