#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define MAXSIZE 11
typedef int elemType;
typedef struct {
elemType *list;
int length;
}sqList;
//init
void initList(sqList *L){
L -> length = 0;
L -> list = malloc(MAXSIZE * sizeof(elemType));
//printf("%d", sizeof(elemType));
//printf("%d", L -> list);
if(!L->list){
printf("failure!\n");
exit(0);
}
printf("init success!\n");
return;
}
//destroy
void destroyList(sqList *L){
//printf("%d", L -> list);
if(!L -> list){
printf("not exit!\n");
exit(0);
}
free(L -> list);
L -> list = NULL;
L -> length = 0;
printf("destroy successful ! \n");
return;
}
//clear
void clearList(sqList *L){
if(L -> list != NULL){
L -> length = 0;
}
return;
}
//isEmpty
int listEmpty(sqList *L){
return L -> length == 0 ? TRUE : FALSE;
}
//return the count of L
int listLength(sqList *L){
return L -> length;
}
//return element of the position
elemType getElem(sqList *L, int i){
if(i<1 || i > listLength(L)){
printf("out of bound!");
exit(0);
}
return L -> list[i-1];
}
//return index of the element, if not exit return 0
int locateElem(sqList *L, elemType e){
elemType *p = L -> list;
int i = 0;
while(i < listLength(L) && *p != e){
p++;
i++;
}
if(i == listLength(L)){
i=-1;
}
return i+1;
}
//return previous element
elemType preElem(sqList *L, elemType cur_e){
int i;
elemType *p = L -> list;
for(i=1; i<listLength(L); i++, p++){
if(*p == cur_e){
break;
}
}
if(i == listLength(L)){
printf("not exit in this sqList");
exit(0);
}
if(i == 1){
printf("no previous element\n");
exit(0);
}
return L -> list[i-2];
}
//return next element
elemType nextElem(sqList *L, elemType cur_e){
int i;
elemType *p = L -> list;
for(i=1; i<listLength(L); i++, p++){
if(*p == cur_e){
break;
}
}
if(i == listLength(L)){
printf("not exit in this sqList\n");
exit(0);
}
if(i == listLength(L)){
printf("no next element\n");
exit(0);
}
return L -> list[i];
}
//insert element
void listInsert(sqList *L, int i, elemType e){
if(i < 1 || i > listLength(L) + 1){
printf("out of bound, insert failure !\n");
exit(0);
}
if(listLength(L) >= MAXSIZE){
printf("full, insert failure !\n");
exit(0);
}
int j = listLength(L);
while(j >= i){
L -> list[j] = L -> list[j-1];
j--;
}
L -> list[i-1] = e;
L -> length++;
return;
}
//delete element and return this element
elemType listDelete(sqList *L, int i, elemType e){
if(i < 1 || i > listLength(L)){
printf("out of bound delete failure !\n");
exit(0);
}
e = L -> list[i-1];
while(i < listLength(L) + 1){
L -> list[i-1] = L -> list[i];
i++;
}
L -> length--;
return e;
}
//traverse sqList
void listTraverse(sqList *L){
int i;
for(i=0; i<listLength(L); i++){
printf("%d ", L -> list[i]);
}
printf("\n");
return;
}
main(){
sqList L;
initList(&L);
listInsert(&L, 1, 23);
listInsert(&L, 1, 12);
listInsert(&L, 1, 01);
printf("共有%d个元素\n", listLength(&L));
listTraverse(&L);
elemType e;
e = listDelete(&L, 1, e);
printf("被删除元素是:%d\n", e);
printf("共有%d个元素\n", listLength(&L));
listTraverse(&L);
e = L.list[0];
printf("元素%d下一个元素是:%d\n", e, nextElem(&L, e));
//printf("元素%d上一个元素是:%d\n", e, preElem(&L, e));
printf("元素%d的位置是:%d\n", e, locateElem(&L, e));
e = getElem(&L, 1);
printf("1号元素是:%d\n", e);
destroyList(&L);
return 0;
}
如有错误,请指出。谢谢。