简易图书管理系统,devc++中运行
define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
ElemType *elem;
int length;
int listsize;
sizeof(ElemType)
SqList;
status InitList_Sq(SqList &L)
L.elem =(ElemType ) malloc (LIST_INIT_SIZEsizeof(ElemType) );
if (!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE; return OK;
InitList_Sq
你这代码应该是C++,不是C,C中没有引用,你的代码中用到了引用,所以是C++代码。
代码及运行结果如下:
#include <iostream>
#include <string>
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
struct ElemType
{
int id; //书号
char name[40]; //书名
//==运算符重载
int operator==(const ElemType& p)
{
return (id == p.id && strcmp(name,p.name)==0);
}
};
enum Status
{
OK=1,
ERROR=2
};
typedef struct {
ElemType* elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList& L)
{
L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem) exit(OVERFLOW); //这行可不写
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//插入函数
Status ListInsert_Sq(SqList& L, int i, ElemType e)
{
ElemType* newbase = 0;
int j;
if (i<1 || i>L.length + 1) return ERROR;
if (L.length >= L.listsize)
{
newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT));
if (!newbase) exit(OVERFLOW); //这行可不写
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
for (j = L.length - 1; j >= i-1; j--)
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;
++L.length;
return OK;
}
//删除函数
Status ListDelete_Sq(SqList& L, int i, ElemType& e)
{
int j;
if (i < 1 || i > L.length) return ERROR;
e = L.elem[i - 1];
for (j = i; j <= L.length - 1; j++)
L.elem[j - 1] = L.elem[j];
--L.length;
return OK;
}
//在线性表中查找元素
int LocateElem(SqList L, ElemType e)
{
for (int i = 0; i < L.length; i++)
{
if (L.elem[i] == e)
return i + 1;
}
return 0;
}
//显示
void ShowList_Sq(SqList L)
{
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i].id << " " << L.elem[i].name << endl;
}
}
int main()
{
SqList L;
InitList_Sq(L); //初始化L
//插入表格中的数据
ElemType es[5] = { 1001,"大学英语",1010,"大学物理",1560,"数据库原理",1892,"数据结构",1056,"C++程序设计" };
for (int i = 1; i <= 5; i++)
ListInsert_Sq(L, i,es[i - 1]);
ElemType einsert;
einsert.id = 1098;
strcpy(einsert.name,"Python 语言程序设计");
//指定位置插入元素
int pos = 0;
cout << "请输入插入位置:";
cin >> pos;
if (ListInsert_Sq(L, pos, einsert) == OK)
cout << "插入成功!" << endl;
else
cout << "插入失败!" << endl;
cout << "插入元素后链表中的元素:" << endl;
ShowList_Sq(L);
//删除指定位置的元素
cout << "请输入删除的位置:";
cin >> pos;
ElemType edel;
if (ListDelete_Sq(L, pos, edel) == OK)
{
cout << "删除成功,删除的元素是:" << endl;
cout << edel.id << " " << edel.name << endl;
}
else
cout << "删除失败" << endl;
cout << "删除元素后链表中的元素:" << endl;
ShowList_Sq(L);
return 0;
}
代码不完整呀,
//定义常量,LIST_INIT_SIZE=100
define LIST_INIT_SIZE 100
//定义常量,LISTINCREMENT=10
#define LISTINCREMENT 10
//定义结构体
typedef struct
ElemType *elem;
int length;
int listsize;
//计算结构体字节数
sizeof(ElemType)
SqList;
//初始化结构体
status InitList_Sq(SqList &L)
L.elem =(ElemType ) malloc (LIST_INIT_SIZEsizeof(ElemType) );
if (!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE; return OK;
InitList_Sq