#include<stdio.h>
#define MAXSIZE 100
typedef struct{
int *elem
;
int length
;
}SqList
;
bool
InitList(SqList
&L
){
L
.elem
=new
int[MAXSIZE
];
if(!L
.elem
)
return false
;
L
.length
=0;
return true
;
}
bool
GetElem(SqList L
,int i
,int &e
){
if(i
<1 || i
>L
.length
|| L
.length
==0)
return false
;
e
=L
.elem
[i
-1];
printf("取出的值为:%d\n",e
);
return true
;
}
int LocateElem(SqList L
,int e
){
for(int i
=0;i
<L
.length
;i
++){
if(L
.elem
[i
]==e
)
printf("%d是第%d号元素\n",e
,i
+1);
}
return 0;
}
bool
ListInsert(SqList
&L
,int i
,int e
){
if((i
<1)||(i
>L
.length
+1)||L
.length
==MAXSIZE
)
return false
;
for(int j
=L
.length
-1;j
>=i
-1;j
--)
{
L
.elem
[j
+1]=L
.elem
[j
];
}
L
.elem
[i
-1]=e
;
++L
.length
;
printf("插入元素:%d\n",e
);
return true
;
}
bool
ListDelete(SqList
&L
,int i
) {
if((i
<1)||(i
>L
.length
)||L
.length
==0)
return false
;
printf("删除的元素:%d\n",L
.elem
[i
-1]);
for(int j
=i
;j
<=L
.length
;j
++){
L
.elem
[j
-1]=L
.elem
[j
];
}
--L
.length
;
return true
;
}
void BianLi(SqList L
){
for(int i
=0;i
<L
.length
;i
++)
printf("%d ",L
.elem
[i
]);
}
int main()
{
SqList s
;
int e
;
InitList(s
);
ListInsert(s
,1,1);
ListInsert(s
,2,2);
ListInsert(s
,1,10);
ListInsert(s
,3,3);
ListInsert(s
,4,11);
GetElem(s
,1,e
);
printf("遍历:");
BianLi(s
);
printf("\n");
ListDelete(s
,1);
GetElem(s
,1,e
);
printf("遍历:");
BianLi(s
);
printf("\n");
LocateElem(s
,2);
}
转载请注明原文地址: https://lol.8miu.com/read-5836.html