把图书表抽象成一个线性表,每本图书(包括ISBN、书名、定价)作为线性表中的一个元素。在图书信息管理系统中要求实现
①构造一个空的单链表;
②创建含有n个元素的单链表;
③输出单链表中所有图书的信息;
④输入i,输出第i本书的信息;
⑤输入一本图书的价格,输出第一本满足这个价格的图书的名字,没有则显示没找到;
⑥输入插入的位置i,并输入待插入图书的ISBN、书名、定价信息,将其插入到单链表的第i个位置上;
⑦输入i,第i书最功在一个菜单
代码如下,如有帮助,请帮忙采纳一下,谢谢。
#include <iostream>
using namespace std;
struct StBook
{
char isbn[20];
char name[20];
int price;
};
struct Node
{
struct StBook stu;
struct Node* next;
};
//1创建空的单链表
struct Node* createEmptyList()
{
struct Node* t = new Node;
t->next = NULL;
return t;
}
//2创建链表
struct Node* createList(struct Node* head)
{
int i,n;
struct Node *p,*t;
p = head;
cout << "请输入个数:";
cin >> n;
for (i=0;i<n;i++)
{
cout << "请输入第" << i+1 <<"本书的ISBN、书名、价格:";
t = new Node;
cin >> t->stu.isbn >>t->stu.name >> t->stu.price;
t->next = NULL;
p->next = t;
p = t;
}
return head;
}
//3显示链表
void printList(struct Node* head)
{
struct Node* p;
if(head == NULL) return;
p = head->next;
while(p)
{
cout << p->stu.isbn << " : " << p->stu.name << " : " << p->stu.price << endl;
p = p->next;
}
}
//获取链表长度(不含头结点)
int getListLength(struct Node* head)
{
int len = 0;
struct Node* p;
if(head == NULL) return len;
p = head->next;
while(p)
{
len++;
p = p->next;
}
return len;
}
//4.显示第i本书的信息
void showIbook(struct Node* head,int pos)
{
int len;
struct Node* p;
len = getListLength(head);
if (pos < 1 || pos > len)
{
cout << "查看位置不合适" <<endl;
return ;
}
//
p = head;
//pos--;
while(pos-- && p )
{
p = p->next;
}
if (p)
{
cout << p->stu.isbn << " : " << p->stu.name << " : " << p->stu.price << endl;
}else
cout << "error" << endl;
}
//5.根据价格查找
void findbyprice(struct Node* head,int price)
{
struct Node* p = head->next;
while(p)
{
if(p->stu.price == price)
{
cout <<p->stu.isbn << " : " << p->stu.name << " : " << p->stu.price << endl;
break;
}
p = p->next;
}
if(p == NULL)
cout << "未找到该价格的书"<<endl;
}
//6插入链表
struct Node* insertList(struct Node* head)
{
int len;
struct Node* p,*t;
int pos;
struct StBook stu;
cout << "请输入插入位置:";
cin >> pos;
len = getListLength(head);
if (pos < 1 || pos > len+1)
{
cout << "插入位置不合适,插入失败" <<endl;
return head;
}
cout << "请输入书的ISBN、书名、价格:";
cin >> stu.isbn >> stu.name >> stu.price;
t = new Node;
t->stu = stu;
t->next = NULL;
p = head;
pos--;
while(pos-- && p )
{
p = p->next;
}
if (p)
{
t->next = p->next;
p->next = t;
}
return head;
}
int main()
{
int pos,price;
//1创建空链表
struct Node* head = createEmptyList();
//2创建链表(填充数据)
head = createList(head);
//3显示创建的链表数据
cout << "链表数据为:"<< endl;
printList(head);
//4输出第i本书的信息
cout << "请输入查找第几本书(从1开始):";
cin >> pos;
showIbook(head,pos);
//5根据价格查找
cout << "请输入价格:";
cin >> price;
findbyprice(head,price);
//6.插入
head = insertList(head);
return 0;
}
int main()
{
printf("=============================\n");
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"); int tmp;
int count = 1;
LNode list;
InitList(&list);
while (count){
printf("请输入你的操作\n");
scanf_s("%d", &tmp);
switch (tmp)
{
case 1:
{
InitList(&list);
printf("初始化完成!\n");
count = 1;
break;
}
case 2:
{
Show(&list);
count = 1;
break;
}
case 3:
{
int ret = 0;
printf("你想输入多少本书籍?");
scanf_s("%d", &ret);
for (int i = 0; i < ret; i++)
{
Book book;
printf("书籍的ISBN号:");
scanf("%s", book.ISBN);
printf("书名:");
scanf("%s", book.bookName);
printf("书籍价格:");
scanf_s("%d", &book.Price);
InsertTailList(&list, book);
printf("录入完成!\n");
}
count = 1;
break;
}
case 4:
{
char arr[10] = { NULL };
printf("请输入要查找书籍的书名:");
scanf("%s", arr);
printf("%s", nameFind(&list, arr)->data.ISBN);
printf("%10s ", nameFind(&list, arr)->data.bookName);
printf("%6d ", nameFind(&list, arr)->data.Price);
printf("\n");
break;
}
case 5:
{
int val = 0;
printf("请输入想删除第几本书!");
scanf_s("%d", &val);
Delete(&list, val);
printf("删除成功!");
count = 1;
break;
}
case 6:
{
printf("当前书库有%d本书籍!\n",Getlength(&list));
count = 1;
break;
}
case 7:
{
int val = 0;
Book book;
printf("请输入你想修改第几本书籍!");
scanf("%d", &val);
printf("书籍的ISBN号:");
scanf("%s", book.ISBN);
printf("书名:");
scanf("%s", book.bookName);
printf("书籍价格:");
scanf_s("%d", &book.Price);
Modify(&list, val,book);
printf("修改成功!");
count = 1;
break;
}
case 0:
{
printf("退出系统成功!\n");
count = 0;
}
default:
break;
}
}
}