#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define OVER 0
#define MAXSIZE 100
typedef int ElemType;
typedef struct {
ElemType *elem;
int length;
}SqList;
int InitList(SqList &L){
L.elem = (ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
if(!L.elem) exit(OVER);
L.length = 0;
return OK;
}
void WriteList(SqList &L){
printf("请输入顺序表中元素的个数:");
scanf("%d",&L.length);
printf("请输入顺序表中的元素:");
for(int i = 0;i<L.length;i++){
scanf("%d",&L.elem[i]);
}
}
int IsEmpty(SqList &L){
if(L.elem==0) return 0;
return 1;
}
int GetLength(SqList &L){
int i = L.length;
printf("顺序表的长度为%d\n",i);
}
int GetElem(SqList &L){
int i;
int e;
printf("请输入所要查找第几个元素:");
scanf("%d",&i);
if(i<1||i>L.length + 1){
printf("查找失败");
return 0;
}
e = L.elem[i-1];
printf("所查找的元素为%d\n",e);
}
int ListInsert(SqList &L){
int i , e;
printf("请输入要在哪个位置插入的元素为多少:");
scanf("%d%d",&i,&e);
if(i<1||i>L.length + 1){
return false;
}
for(int j = L.length;j>=i;j--)
{
L.elem[j] = L.elem[j-1];
}
L.elem[i-1]=e;
L.length++;
printf("插入的元素是%d,插入的位置是%d",e,i);
}
int PrintList(SqList &L){
printf("顺序表中的元素有:");
for(int i = 0;i<L.length;i++){
printf("%d",L.elem[i]);
}
}
int DestoryList(SqList &L){
if(L.elem) delete L.elem;
}
int main(){
SqList L;
InitList(L);
WriteList(L);
IsEmpty(L);
GetLength(L);
GetElem(L);
ListInsert(L);
PrintList(L);
DestoryList(L);
}