数据结构-线性表
要求:实现以上操作。
代码部分:
#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;
}
}
}
上一篇: Python-反射机制
下一篇: C语言 指针数组进阶详解