代码
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct List{
E * array;
int capacity;
int size;
};
typedef struct List * ArrayList;
_Bool initList(ArrayList list){
list->array = malloc(sizeof (E) * list->capacity);
if(list->array == NULL) return 0;
list->capacity = 10;
list->size=0;
return 1;
}
_Bool insertList(ArrayList list, E element, int index){
if(index < 1 || index > list->size + 1) return 0;
if(list->size == list->capacity) {
int newCapacity = list->capacity + (list->capacity >> 1);
E * newArray = realloc(list->array, newCapacity * sizeof(E));
if(newArray == NULL) return 0;
list->array = newArray;
list->capacity = newCapacity;
}
for (int i = list->size; i > index - 1; --i)
list->array[i] = list->array[i - 1];
list->array[index - 1] = element;
list->size++;
return 1;
}
void printList(ArrayList list){
for (int i = 0; i < list->size; ++i)
printf("%d ", list->array[i]);
printf("\n");
}
_Bool deleteList(ArrayList list, int index){
if(index < 1 || index > list->size) return 0;
for (int i = index - 1; i < list->size - 1; ++i)
list->array[i] = list->array[i + 1];
list->size--;
return 1;
}
int sizeList(ArrayList list){
return list->size;
}
E * getList(ArrayList list, int index){
if(index < 1 || index > list->size) return NULL;
return &list->array[index - 1];
}
int findList(ArrayList list, E element){
for (int i = 0; i < list->size; ++i) {
if(list->array[i] == element) return i + 1;
}
return -1;
}
int main(){
struct List list;
if(initList(&list)){
for (int i =0; i<30; i++){
insertList(&list, i*10, i+1);
}
deleteList(&list, 10);
deleteList(&list, 10);
printList(&list);
printf("获取数组容量:%d\n", list.capacity);
printf("按位置获取元素:%d\n", *getList(&list, 3));
printf("按值查找元素位置:%d\n", findList(&list, 30));
} else{
printf("顺序表初始化失败,无法启动程序!");
}
}
运行效果