数据结构线性表的顺序和实现
语言:C++
IDE:vs
#include "pch.h"
#include "string.h"
#include "conio.h"
#include <iostream>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSIZE 100
typedef struct Book
{
char no[20];//BISN
char name[50];//书名
float price;//价格
bool operator==(const Book& anotherbook)
{
return(!strcmp(no, anotherbook.no));
}
}ElemType;//别名
typedef struct
{
ElemType* elem;//存储空间的基地址
int length;//当前长度
}SqList;//顺序表的结构类型为SqList
Status InitList(SqList& L);
Status GetElem(SqList L, int i, ElemType& e);
int LocateElem(SqList L, ElemType e);
Status ListInsert(SqList& L, int i, ElemType e);
Status ListDelete(SqList& L, int i);
int show(SqList& L);
//初始化链表
Status InitList(SqList &L)
{//构造一个空的顺序表L
L.elem = new ElemType[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
if (!L.elem) {
exit(OVERFLOW);
}
else {
L.length = 0;
cout << "初始化成功" << endl;
}
return OK;
}
//顺序表的取值
Status GetElem(SqList L, int i, ElemType& e)
{
if (i<1 || i>L.length)
{
return ERROR;
}
else
{
e = L.elem[i - 1];
}
return OK;
}
//顺序表的查找
int LocateElem(SqList L, ElemType e)
{//在顺序表L中查找值为e的数据元素,返回其序号。
int i;
for (i = 0; i < L.length; i++)
{
if (L.elem[i] == e)
{
cout << "查找成功,在第" << i+1 << "个位置" << endl;
return i + 1;
}
}
}
//顺序表的插入
Status ListInsert(SqList& L, int i, ElemType e)
{
int j;
//cout << "执行0" << endl;
if ((i < 0) || (i > L.length + 1)) {
cout << "error1" << endl;
return ERROR;
}
else if(L.length==MAXSIZE)
{
cout << "error2" << endl;
return ERROR;
}
else
{
if (i==0)
{
L.elem[L.length] = e;
++L.length;
cout << "元素插入在末尾,第" << L.length << "个位置成功" << endl;
}
else
{
//cout << "L的长度" << L.length<<endl;
for (j = L.length -1;j>=i-1;j--)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[i - 1] = e;
++L.length;
cout << "元素插入第" << i << "个位置成功" << endl;
}
return OK;
}
}
//顺序表的删除
Status ListDelete(SqList& L, int i)
{
int j;
if ((i < 1) || (i > L.length))
{
return ERROR;
}
else
{
for (j = i; j < L.length - 1; j++)
{
L.elem[j - 1] = L.elem[j];
}
--L.length;
return OK;
}
}
//显示
int show(SqList& L) {
if (L.length == 0) {
cout << "没有书" << endl;
}
else {
cout <<"此时链表长度"<< L.length << endl;
cout << "编号\t\t" << "名字\t\t" << "价格" << endl;
for (int i = 0; i < L.length; i++) {
cout << L.elem[i].no << "\t\t" << L.elem[i].name << "\t\t" << L.elem[i].price << endl;
}
}
return 0;
}
int main()
{
SqList L;
ElemType e,m;
InitList(L);
char n;
char ch;
while (1)
{
cout << "------------------------------------------------------------------------------------------------------------------" << endl;
cout << "请选择要进行的操作(回车选下一个 操作) <a>显示 <b>插入元素 <c>删除指定元素 <d>查找 <x>退出" << endl;
cout << "------------------------------------------------------------------------------------------------------------------" << endl;
cin >> n;
switch (n)
{
case 'a':
show(L);
ch = _getch();
system("cls");
break;
case 'b':
int x;
cout << "请输入插入的位置:" << endl;
cin >> x;
cout << "请输入书的IBSN:" << endl;
cin >> e.no;
cout << "请输入书的书名:" << endl;
cin >> e.name;
cout << "请输入书的价格:" << endl;
cin >> e.price;
ListInsert(L,x,e);
ch = _getch();
system("cls");
break;
case 'c':
int i;
cout << "请输入要删除的元素位置:" ;
cin >> i;
ListDelete(L, i);
ch = _getch();
system("cls");
break;
case 'd':
cout << "请输入书的IBSN:";
cin >> e.no;
LocateElem(L, e);
ch = _getch();
system("cls");
break;
case 'x':
exit(0);
break;
default:
cout << "你输入的命令有错,请输入a、b、c、d、x的小写字母。" << endl;
break;
}
}
return 0;
}