数据结构-线性表

0
11

要求:实现以上操作。

代码部分:

#include<iostream>
#include<string.h>
#include<stdlib.h>
#define maxSize 100
using namespace std;
typedef struct {
char ISBN[20];
char name[50];
float price;
}Book;
typedef struct {
Book data[maxSize];
int length;
}Sqlist;
//插入函数
int insertElsm(Sqlist &L, int p/*插入位置*/, Book b) {
int i;
if (p<0 || p>L.length || L.length == maxSize) {
return 0;
}
for (i = L.length – 1; i >= p; –i) {
L.data[i + 1] = L.data[i];
}
L.data[p] = b;
++(L.length);
return 1;
}
void insert(Sqlist &L) { //插入操作
Book b1;
cout << “请输入数的编码:”;
cin >> b1.ISBN;
cout << “请输入书名:”;
cin >> b1.name;
cout << “请输入书的定价:”;
cin >> b1.price;
int p;//插入位置
cout << “选择插入位置:”;
cin >> p;
if (insertElsm(L, p, b1)) {
cout << “插入成功!”;
}
else
{
cout << “插入失败!”;
}
}
//查找函数
int findElem(Sqlist L, char e[20]/*查找的信息*/) {
int i;
for (i = 0; i < L.length; i++) {
if (0==strcmp(L.data[i].ISBN,e))
{
return i;
}
}
return -1;
}
void find(Sqlist &L) { //查找操作
char a[20];
cout << “输入查找的编号:”;
cin >> a;
int b = findElem(L, a);
if (b==-1)
{
cout << “查询失败!” << endl;
}
else
{
cout << “查询的编号:” << L.data[b].ISBN << endl;
cout << “查询的书名:” << L.data[b].name << endl;
cout << “查询的价格:” << L.data[b].price << endl;
cout << “查询编号当前所处位置:” << b << endl;
}
}
//删除函数
int del(Sqlist &L,int p,Book &b) {
int i;
if (p<0 || p>L.length-1) {
return 0;
}
b = L.data[p];
cout << “您删除的编号:” << L.data[p].ISBN << endl;
cout << “您删除的书名:” << L.data[p].name << endl;
cout << “您删除的价格:” << L.data[p].price << endl;
int is_bool;
cout << “确认请输入1:”;
cin >> is_bool;
if (is_bool)
{
for (i = p; i < L.length – 1; ++i) {
L.data[i] = L.data[i + 1];
}
–(L.length);
return 1;
}
else {
return 0;
}
}
void delet(Sqlist &L) {
int p;
cout << “输入删除的位置:”;
cin >> p;
Book b1;
int i = del(L, p, b1);
if (i)
{
cout << “删除成功!” << endl;
}
else {
cout << “删除失败!” << endl;
}
}
//修改函数
int cha(Sqlist &L, int p) {
int i;
cout << “您修改的编号:” << L.data[p].ISBN<<endl;
cout << “您修改的书名:” << L.data[p].name<<endl;
cout << “您修改的价格:” << L.data[p].price<<endl;
cout << “确定请输入1:”;
cin >> i;
if (i) {
cout << “输入您修改的编号:”;
cin >> L.data[p].ISBN;
cout << “输入您修改的书名:”;
cin >> L.data[p].name;
cout << “输入您修改的价格:”;
cin >> L.data[p].price;
return 1;
}
return 0;
}
void change(Sqlist &L) {
int q;
cout << “输入需要修改的位置:”;
cin >> q;
cha(L, q);
}
//排序部分
int ord(Sqlist &L,int p,int q) {
if (p>=L.length||p<0||q>=L.length||q<0)
{
return 0;
}
Book b1;
if (p > q)
{
b1 = L.data[p];
for (int i = p; i > q; i–)
{
L.data[i] = L.data[i – 1];
}
L.data[q] = b1;
return 1;
}if (p < q) {
b1 = L.data[p];
for (int i = p; i < q; i++)
{
L.data[i] = L.data[i+1];
}
L.data[q] = b1;
return 1;
}
return 0;
}
void order(Sqlist &L) {
int m,n;
cout << “请输入需要排序的数据位置:”;
cin >> m;
cout << “请输入排序到的位置:”;
cin >> n;
int a = ord(L, m, n);
if (a)
{
cout << “排序成功!” << endl;
}
else
{
cout << “排序失败!” << endl;
}
}
//计数部分
void total(Sqlist L) {
cout << “目前表内数据共:” << L.length << “个!” << endl;
if (L.length != 0)
{
for (int i = 0; i < L.length; i++) {
cout << “编号:” << L.data[i].ISBN << ” “;
cout << “书名:” << L.data[i].name << ” “;
cout << “价格:” << L.data[i].price << endl;
}
}
else
{
cout << “当前无储存数据!” << endl;
}
}
//创建部分
void creat(Sqlist &L) {
Book b;
int num; //创建列表数
cout << “输入书的总数:”;
cin >> num;
for (int i = 0; i < num; i++)
{
cout << “输入书的编号:”;
cin >> b.ISBN;
cout << “输入书的书名:”;
cin >> b.name;
cout << “输入书的价格:”;
cin >> b.price;
cout << endl;
L.data[i] = b;
L.length++;
}
}
int main() {
Sqlist book_sqlist;
book_sqlist.length = 0;
int select;
while (true)
{
cout << “—–1.插入数据操作—–” << endl;
cout << “—–2.查找数据操作—–” << endl;
cout << “—–3.删除数据操作—–” << endl;
cout << “—–4.修改数据操作—–” << endl;
cout << “—–5.排序数据操作—–” << endl;
cout << “—–6.计数显示 —–” << endl;
cout << “—–7.创建线性表 —–” << endl;
cout << “—-数据第一个位置为0—-“<<endl;
cout << “选择操作:”;
cin >> select;
switch (select)
{
case 1: //插入
insert(book_sqlist);
system(“pause”);
system(“cls”);
break;
case 2: //查找
find(book_sqlist);
system(“pause”);
system(“cls”);
break;
case 3: //删除
delet(book_sqlist);
system(“pause”);
system(“cls”);
break;
case 4: //修改
change(book_sqlist);
system(“pause”);
system(“cls”);
break;
case 5: //排序
order(book_sqlist);
system(“pause”);
system(“cls”);
break;
case 6: //计数
total(book_sqlist);
system(“pause”);
system(“cls”);
break;
case 7: //创建线性表
creat(book_sqlist);
system(“pause”);
system(“cls”);
break;
}
}
}

<

发布回复

请输入评论!
请输入你的名字