# include<iostream>
# include<string>
using namespace std;
/*--------------------------------------
自定义单词含义
------------------------------------------*/
# define OK 0
# define ERROR 1
# define MAXSIZE 20
/*-------------------------------------
定义变量类型
---------------------------------------*/
typedef int Status;
typedef int Elemtype;
/*---------------------------------------
定义类
-----------------------------------------*/
typedef struct
{
Elemtype data[MAXSIZE];
int length;
}Sqlist;
/*-------------------------------------
声明函数
----------------------------------------*/
Status ElemGet(Sqlist, int, Elemtype *);
Status ListInsert(Sqlist *, int, Elemtype);
Status ListDelete(Sqlist *, int, Elemtype *);
/*------------------------------------------
主函数
--------------------------------------------*/
int main()
{
Sqlist Line;
Sqlist * pLine;
pLine = &Line;
Elemtype e;
Elemtype * pe;
pe = &e;
for (int i = 0; i < 10; i++)
{
Line.data[i] = i;
}
Line.length = 10;
for (auto c : Line.data)
cout << c << " " << endl;
// cout << Line.data[6] << " " << Line.data[7] << endl;
ElemGet(Line, 5, pe);
cout << e << "\n" << Line.data[6] << "\n"
<<Line.length<<"\n" << endl;
e = 11;
ListInsert(pLine, 5, e);
cout << Line.data[4] << "\n" <<Line.length
<<"\n" << endl;
ListDelete(pLine, 7, pe);
for (auto c : Line.data)
cout << c << endl;
system("pause");
return 0;
}
/*------------------------------------------
函数一:获得元素操作
--------------------------------------------*/
Status ElemGet(Sqlist L, int t, Elemtype * e)
{
if (t <= 0 || t > L.length)
return ERROR;
*e = L.data[t - 1];
return OK;
}
/*------------------------------------------
函数二:插入元素操作
---------------------------------------------*/
Status ListInsert(Sqlist * L, int t, Elemtype e)
{
if (t <= 0 || t > L->length)
return ERROR;
if (L->length == MAXSIZE)
return ERROR;
for (int i = L->length - 1; i >= t - 1; i--)
{
L->data[i + 1] = L->data[i];
}
L->data[t - 1] = e;
L->length++;
return OK;
}
/*-------------------------------------------
函数三:删除元素操作
---------------------------------------------*/
Status ListDelete(Sqlist *L, int t, Elemtype * e)
{
if (t <= 0 || t > L->length)
return ERROR;
*e = L->data[t - 1];
for (int i = t; i <= L->length - 1; i++)
L->data[i - 1] = L->data[i];
L->length--;
return OK;
}
# include<string>
using namespace std;
/*--------------------------------------
自定义单词含义
------------------------------------------*/
# define OK 0
# define ERROR 1
# define MAXSIZE 20
/*-------------------------------------
定义变量类型
---------------------------------------*/
typedef int Status;
typedef int Elemtype;
/*---------------------------------------
定义类
-----------------------------------------*/
typedef struct
{
Elemtype data[MAXSIZE];
int length;
}Sqlist;
/*-------------------------------------
声明函数
----------------------------------------*/
Status ElemGet(Sqlist, int, Elemtype *);
Status ListInsert(Sqlist *, int, Elemtype);
Status ListDelete(Sqlist *, int, Elemtype *);
/*------------------------------------------
主函数
--------------------------------------------*/
int main()
{
Sqlist Line;
Sqlist * pLine;
pLine = &Line;
Elemtype e;
Elemtype * pe;
pe = &e;
for (int i = 0; i < 10; i++)
{
Line.data[i] = i;
}
Line.length = 10;
for (auto c : Line.data)
cout << c << " " << endl;
// cout << Line.data[6] << " " << Line.data[7] << endl;
ElemGet(Line, 5, pe);
cout << e << "\n" << Line.data[6] << "\n"
<<Line.length<<"\n" << endl;
e = 11;
ListInsert(pLine, 5, e);
cout << Line.data[4] << "\n" <<Line.length
<<"\n" << endl;
ListDelete(pLine, 7, pe);
for (auto c : Line.data)
cout << c << endl;
system("pause");
return 0;
}
/*------------------------------------------
函数一:获得元素操作
--------------------------------------------*/
Status ElemGet(Sqlist L, int t, Elemtype * e)
{
if (t <= 0 || t > L.length)
return ERROR;
*e = L.data[t - 1];
return OK;
}
/*------------------------------------------
函数二:插入元素操作
---------------------------------------------*/
Status ListInsert(Sqlist * L, int t, Elemtype e)
{
if (t <= 0 || t > L->length)
return ERROR;
if (L->length == MAXSIZE)
return ERROR;
for (int i = L->length - 1; i >= t - 1; i--)
{
L->data[i + 1] = L->data[i];
}
L->data[t - 1] = e;
L->length++;
return OK;
}
/*-------------------------------------------
函数三:删除元素操作
---------------------------------------------*/
Status ListDelete(Sqlist *L, int t, Elemtype * e)
{
if (t <= 0 || t > L->length)
return ERROR;
*e = L->data[t - 1];
for (int i = t; i <= L->length - 1; i++)
L->data[i - 1] = L->data[i];
L->length--;
return OK;
}