我正在尝试在 C 中实现向量或数组列表。代码没有主代码,所以我用 gcc -c file.c 编译它。我有两个问题,第一个是如何为 arraylist 实现插入函数,第二个是为什么我收到 list-> is not a function 的错误。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
typedef struct ArrayList {
int *data;
int capacity;
int size;
ArrayList *list;
ArrayList ArrayList_init(int);
int insert(ArrayList *, int , int);
int set(ArrayList*, int, int);
int destroy(ArrayList *);
int print(ArrayList *);
int append(ArrayList *, int);
int valueOf( ArrayList *, int);
int size(ArrayList *);
int capacity(ArrayList *);
} ArrayList;
ArrayList * ArrayList_Init( int n )
{
ArrayList->data = malloc(sizeof(n) * ArrayList->capacity);
}
int append( ArrayList * list, int val )
{
if (list->size >= list->capacity)
return 1;
else
Arraylist->data[list->size++] = value;
return 0;
}
int print( ArrayList * list )
{
printf("%d\n", list->data);
return 0;
}
int set( ArrayList * list, int val, int index )
{
while(index >= list->size)
append(ArrayList, 0);
return 0;
if (index < 0 || index >= size)
return 1;
}
int destroy( ArrayList * list )
{
free(list->data);
return 0;
}
int insert(ArrayList * list, int val, int index)
{
return 0;
}
int valueOf( ArrayList * list, int element )
{
int x;
x = list->data[element];
return x;
}
int size(ArrayList * list)
{
return list->size;
}
int capacity(ArrayList * list)
{
return list->capacity;
}
int main(void)
{
int n = 3;
int i;
int stat; // Error code
int size;
int val = 0;
int capacity;
// allocate list
ArrayList *list = NULL;
list = ArrayList_Init(n);
printf("ArrayList initialized to %d elements\n", n);
printf("Size of List = %d\n", list->size(list));
printf("Capacity of List = %d\n", list->capacity(list));
// Fill initial values
list->set(list, val++, 0);
list->set(list, val++, 1);
list->set(list, val++, 2);
}
谢谢