#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void initList(SqList *L) {
L->length = 0;
}
int insertList(SqList *L, int i, int e) {
if (i < 1 || i > L->length + 1 || L->length == MAXSIZE) {
return 0;
}
for (int j = L->length; j >= i; j--) {
L->data[j] = L->data[j - 1];
}
L->data[i - 1] = e;
L->length++;
return 1;
}
int deleteList(SqList *L, int i, int *e) {
if (i < 1 || i > L->length) {
return 0;
}
*e = L->data[i - 1];
for (int j = i; j < L->length; j++) {
L->data[j - 1] = L->data[j];
}
L->length--;
return 1;
}
int getLength(SqList L) {
return L.length;
}
int locateElement(SqList L, int e) {
for (int i = 0; i < L.length; i++) {
if (L.data[i] == e) {
return i + 1;
}
}
return 0;
}
void printList(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
void destroyList(SqList *L) {
}
int main () {
SqList L;
initList(&L);
insertList(&L, 1, 2);
insertList(&L, 2, 6);
insertList(&L, 3, 9);
insertList(&L, 4, 8);
printList(L);
int e;
deleteList(&L, 3, &e);
printf("删除的元素为:%d\n", e);
printList(L);
int length = getLength(L);
printf("线性表长度为:%d\n", length);
int pos1 = locateElement(L, 2);
printf("元素 2 的位置为:%d\n", pos1);
int pos2 = locateElement(L, 6);
printf("元素 6 的位置为:%d\n", pos2);
return 0;
}