用链表实现图书管理系统,包括如下的功能:
查找:按不同属性查找所有等于给定值 的数据元素 ,找到并返回它们;
插入:在表中第 i (1=
删除:可删除表中第 i (1=
输出:依次打印表中的各个元素的值;
排序:可按某属性对表中的元素进行排序。
ManageSystem.h
文件#pragma once
#include
#include
#include
using namespace std;
class Book //书本类
{
friend class ListNode;
friend class ListChain;
protected:
long long ISBN; //书号
string name; //书名
int price; //价格
public:
Book(); //无参构造函数,创建一个Book对象
Book(long long, string, int); //含参构造函数,创建一个Book对象
Book(const Book&); //拷贝构造函数
~Book(); //析构函数
Book& operator=(const Book&); //重载=
friend istream& operator>>(istream&, Book&); //重载输入>>
friend ostream& operator<<(ostream&, Book&); //重载输出<<
long long GetISBN(); //返回书号
string Getname(); //返回书名
int Getprice(); //返回价格
void ChangeISBN(long long); //修改书号
void Changename(string); //修改书名
void Changeprice(int); //修改价格
};
class ListNode //结点类
{
public:
Book data; //数据域
ListNode *next; //指针域
};
class ListChain //链表类
{
protected:
ListNode *head; //头指针
int length; //长度
public:
ListChain(); //无参构造函数,建立一个空链表(只包含头结点)
void InputTail(const Book bl[], const int); //尾插法将数据读入
void ClearChain(); //删除链表
void ShowListChain(); //遍历链表
Book Find(long long); //依据书号查找
Book Find(string); //依据书名查找
bool Insert(const Book&, int); //插入
bool Delete(int); //删除
bool Sort(string); //排序
};
ManageSystem.cpp
文件#include "ManageSystem.h"
#include
#include
#include
using namespace std;
Book::Book() {}; //无参构造函数,创建一个Book对象
Book::Book(long long i, string n, int p):ISBN(i), name(n), price(p) {}; //构造函数,创建一个Book对象
Book::Book(const Book& temp):ISBN(temp.ISBN), name(temp.name), price(temp.price) {}; //拷贝构造函数
Book::~Book() {}; //析构函数
long long Book::GetISBN() {return ISBN;} //返回书号
string Book::Getname() {return name;} //返回书名
int Book::Getprice() {return price;} //返回价格
void Book::ChangeISBN(long long newisbn) {ISBN = newisbn;} //修改书号
void Book::Changename(string newname) {name = newname;} //修改书名
void Book::Changeprice(int newprice) {price = newprice;} //修改价格
istream& operator>>(istream& in, Book& temp) //重载输入>>
{
long long i; string n; int p;
in >> i >> n >> p;
temp.ISBN = i; temp.name = n; temp.price = p;
return in;
}
ostream& operator<<(ostream& out, Book& temp) //重载输出<<
{
out << temp.ISBN << " " << temp.name << " " << temp.price;
return out;
}
Book& Book::operator=(const Book& temp) //重载=
{
if(this == &temp)
return *this;
ISBN = temp.ISBN; name = temp.name; price = temp.price;
return *this;
}
ListChain::ListChain() //无参构造函数,建立一个空链表(只包含头结点)
{
head = new ListNode; //头指针指向头结点
head -> data.ISBN = 0; head -> data.name = ' '; head -> data.price = 0;
head -> next = nullptr;
length = 0;
}
void ListChain::InputTail(const Book bl[],const int n) //尾插法读入数据
{
head = new ListNode; //头指针指向头结点
head -> data.ISBN = 0; head -> data.name = ' '; head -> data.price = 0;
head -> next = nullptr;
length = 0;
ListNode *r = head; //辅助指针初始指向头结点
for(int i = 0; i < n; i++)
{
ListNode *p = new ListNode; //新建一个结点
p -> data.ISBN = bl[i].ISBN; p -> data.name = bl[i].name; p -> data.price = bl[i].price;
p -> next = nullptr;
r -> next = p; //新结点插入尾结点之后
r = p; //辅助指针r向后移动
length++;
}
}
void ListChain::ClearChain() //删除链表
{
ListNode *r = head -> next; //定义一个辅助指针,并指向链表的第一个结点
while(r != nullptr)
{
ListNode *temp = r -> next; //用 temp 暂存 r 所指向的结点的信息
delete r; //释放 r 所指向的结点的内存空间
r = temp; //r 移向下一个结点
}
head -> next = nullptr; //头结点的指针域设置为NULL
length = 0; //长度设置为0
}
void ListChain::ShowListChain() //遍历链表
{
ListNode *r = head -> next; //定义一个辅助指针,并指向链表的第一个结点
if(r == nullptr) //特判空链表的情况
{
cout << "没有书籍!" << endl;
return ;
}
while(r != nullptr)
{
cout << r -> data << endl; //输出当前结点的数据域
r = r -> next; //指针 p 移向下一个结点
}
cout << endl;
}
Book ListChain::Find(long long tisbn) //依据书号查找
{
ListNode *r = head -> next; //定义一个辅助指针,并指向链表的第一个结点
while(r -> next != nullptr) //遍历
{
if(r -> data.ISBN == tisbn) //比较书号,如果找到就返回这本书
return r -> data;
r = r -> next;
}
return Book(0, "0", 0); //没找到就返回空书
}
Book ListChain::Find(string tname) //依据书名查找
{
ListNode *r = head -> next; //定义一个辅助指针,并指向链表的第一个结点
while(r -> next != nullptr) //遍历
{
if(r -> data.name == tname) //比较书名,如果找到就返回这本书
return r -> data;
r = r -> next;
}
return Book(0, "0", 0); //没找到就返回空书
}
bool ListChain::Insert(const Book& book, int n) //插入
{
ListNode *r = head -> next; //定义一个辅助指针,并指向链表的第一个结点
if(n > length + 1 || n <= 0) //特判位置不合法的情况
{
cout << "插入位置不合法!" << endl;
return false;
}
for(int i = 1; i <= n - 2; i++) //辅助指针指向待插入位置 n 的前一个位置
r = r -> next;
ListNode *temp = new ListNode; //新建结点
temp -> data.ISBN = book.ISBN; temp -> data.name = book.name; temp -> data.price = book.price;
temp -> next = r -> next; //插入
r -> next = temp;
length++; //长度加一
return true;
}
bool ListChain::Delete(int n) //删除
{
ListNode *r = head -> next; //定义一个辅助指针,并指向链表的第一个结点
if(n > length || n <= 0) //特判位置不合法的情况
{
cout << "删除位置不合法!" << endl;
return false;
}
for(int i = 1; i <= n - 2; i++) //辅助指针指向待删除的位置n的前一个位置
r = r -> next;
ListNode *temp = r -> next; //临时结点保存被删除结点的信息
r -> next = r -> next -> next; //删除
delete temp;
length--; //长度减一
return true;
}
bool ListChain::Sort(string key) //排序
{
if(key == "ISBN")
{
if(length == 0) //特判长度为零的情况
{
cout << "没有书籍,无法排序!" << endl;
return false;
}
else
{
ListNode *r = head -> next;
Book temp(0, "0", 0);
for(int i = 1; i <= length - 1; i++) //冒泡法排序
{
r = head -> next;
for(int j = 1; j <= length - i; j++)
{
if(r -> data.ISBN > r -> next -> data.ISBN)
{
temp = r -> data;
r -> data = r -> next -> data;
r -> next -> data = temp;
}
r = r -> next;
}
}
}
return true;
}
else if(key == "name")
{
if(length == 0) //特判长度为零的情况
{
cout << "没有书籍,无法排序!" << endl;
return false;
}
else
{
ListNode *r = head -> next;
Book temp(0, "0", 0);
for(int i = 1; i <= length - 1; i++) //冒泡法排序
{
r = head -> next;
for(int j = 1; j <= length - i; j++)
{
if(r -> data.name > r -> next -> data.name)
{
temp = r -> data;
r -> data = r -> next -> data;
r -> next -> data = temp;
}
r = r -> next;
}
}
}
return true;
}
else if(key == "price")
{
if(length == 0) //特判长度为零的情况
{
cout << "没有书籍,无法排序!" << endl;
return false;
}
else
{
ListNode *r = head -> next;
Book temp(0, "0", 0);
for(int i = 1; i <= length - 1; i++) //冒泡法排序
{
r = head -> next;
for(int j = 1; j <= length - i; j++)
{
if(r -> data.price > r -> next -> data.price)
{
temp = r -> data;
r -> data = r -> next -> data;
r -> next -> data = temp;
}
r = r -> next;
}
}
}
return true;
}
else //特判给定标准不合法的情况
{
cout << "无法按此标准排序!"<< endl;
return false;
}
}
main.cpp
文件#include
#include
#include
#include "ManageSystem.h"
using namespace std;
Book booklist[9] = {{741258963101, "Data Structures", 76}, {741258963102, "Python Crash Course", 65},
{741258963103, "Core Java", 60}, {741258963104, "Computer Networking", 73}, {741258963105, "C++ Primer", 65},
{741258963106, "Computer Systems", 70}, {741258963107, "Algorithm Analysis", 76}, {741258963108, "Go In Action", 58},
{741258963109, "C Primer Plus", 62}};
void Menu()
{
cout << "请输入1 ~ 6 来指定想要的操作:" << endl;
cout << "1: 输出所有书籍信息" << endl;
cout << "2: 录入书籍信息" << endl;
cout << "3: 删除书籍信息" << endl;
cout << "4: 查找书籍" << endl;
cout << "5: 排序" << endl;
cout << "6: 退出系统" << endl;
}
int main()
{
ListChain L;
L.InputTail(booklist, 9);
Menu();
int op;
while(cin >> op && op != 6)
{
getchar();
switch(op)
{
case 1:
{
L.ShowListChain();
break;
}
case 2:
{
cout << "请输入待录入书籍的信息以及想放入的位置: ";
long long IS; string NA; int PR; int PL;
cin >> IS >> NA >> PR >> PL;
L.Insert({IS, NA, PR}, PL);
break;
}
case 3:
{
cout << "请输入待删除书籍的位置: ";
int PL;
cin >> PL;
L.Delete(PL);
break;
}
case 4:
{
cout << "请输入查找标准(ISBN or name):";
string tag;
cin >> tag;
getchar();
if(tag == "ISBN")
{
cout << "请输入书号: ";
long long IS;
cin >> IS;
Book result = L.Find(IS);
cout << result << endl;
}
else
{
cout << "请输入书名: ";
string NA;
getline(cin, NA);
Book result = L.Find(NA);
cout << result << endl;
}
break;
}
case 5:
{
cout << "请输入排序标准(1--ISBN or 2--name or 3--price):";
int tag;
cin >> tag;
if(tag == 1)
{
L.Sort("ISBN");
L.ShowListChain();
}
else if(tag == 2)
{
L.Sort("name");
L.ShowListChain();
}
else
{
L.Sort("price");
L.ShowListChain();
}
break;
}
}
}
return 0;
}
请输入1 ~ 6 来指定想要的操作:
1: 输出所有书籍信息
2: 录入书籍信息
3: 删除书籍信息
4: 查找书籍
5: 排序
6: 退出系统
1
741258963101 Data Structures 76
741258963102 Python Crash Course 65
741258963103 Core Java 60
741258963104 Computer Networking 73
741258963105 C++ Primer 65
741258963106 Computer Systems 70
741258963107 Algorithm Analysis 76
741258963108 Go In Action 58
741258963109 C Primer Plus 62
2
请输入待录入书籍的信息以及想放入的位置: 741258963110 Pytorch 69 5
1
741258963101 Data Structures 76
741258963102 Python Crash Course 65
741258963103 Core Java 60
741258963104 Computer Networking 73
741258963110 Pytorch 69
741258963105 C++ Primer 65
741258963106 Computer Systems 70
741258963107 Algorithm Analysis 76
741258963108 Go In Action 58
741258963109 C Primer Plus 62
3
请输入待删除书籍的位置: 5
1
741258963101 Data Structures 76
741258963102 Python Crash Course 65
741258963103 Core Java 60
741258963104 Computer Networking 73
741258963105 C++ Primer 65
741258963106 Computer Systems 70
741258963107 Algorithm Analysis 76
741258963108 Go In Action 58
741258963109 C Primer Plus 62
4
请输入查找标准(ISBN or name):ISBN
请输入书号: 741258963102
741258963102 Python Crash Course 65
5
请输入排序标准(1--ISBN or 2--name or 3--price):2
741258963107 Algorithm Analysis 76
741258963109 C Primer Plus 62
741258963105 C++ Primer 65
741258963104 Computer Networking 73
741258963106 Computer Systems 70
741258963103 Core Java 60
741258963101 Data Structures 76
741258963108 Go In Action 58
741258963102 Python Crash Course 65
6
Process returned 0 (0x0) execution time : 53.141 s
Press any key to continue.