0

I am trying to implement a vector or arraylist in C. The code does not have a main, so I am compiling it with gcc -c file.c . I have two questions, the first being how to implement an insert function for the arraylist, and the second being why I receive the error that 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);
    }

Thank you

Robert Penfield
  • 15
  • 1
  • 1
  • 5
  • Why are your function prototypes inside the `typedef struct ArrayList` block? Does it give you a line number for that error? – Leigh Nov 12 '13 at 03:53
  • all instances of list-> were seen as problematic the minute I added a simple main function, so I think the problem lies in this code. – Robert Penfield Nov 12 '13 at 03:58
  • can you show us your `main()` function, as wells as the error message details from gcc ? – dpp Nov 12 '13 at 04:06
  • I've edited the code to include a sample main. Thanks. The errors were 'Arraylist' has no member named 'set' list->size is not a function, 'Arraylist' has no member named 'print', etc. – Robert Penfield Nov 12 '13 at 04:14
  • 1
    You're not reserving space for the rest of the `list`, only allocating space for `list->data`. – Leigh Nov 12 '13 at 04:14
  • I'm not as familiar with C as I am with C++, however I'm pretty sure that what you're doing would be more efficient with a Linked List. It would also be closer to a vector, since it has no limit to size. Here's a working C++ example (shouldn't be too hard to convert) http://sourceforge.net/p/sharplibs/code/ci/master/tree/llist.h –  Nov 12 '13 at 04:16

1 Answers1

1

First of all if you declare a function that returns something it must do so. I think your ArrayList_Init should more look like this (I am assuming you want to store integers)

ArrayList * ArrayList_Init( int n )
{
    ArrayList * list = malloc(ArrayList);
    list->data = malloc(sizeof(int) * n);
    list->capacity = n;
    list->size=0;
    return list;
}

In append as well as in Init you are trying to access a type.

 Arraylist->data[list->size++] = value;

but it is supposed to be

 list->data[list->size++] = value;

For your insert you can do something similiar to the following:

 int insert( ArrayList * list, int val ,int position)
 {
      if (list->size >= list->capacity){
        int * temp = malloc(sizeof(int) * list->capacity *2);
        int i;
        for(i=0;i<position-1;i++){
            temp[i]=list->data[i];
        }
        for(i=postion-1;i<list->size;i++){
            temp[i+1]=list->data[i];
        }
        free(list->data);
        list->data=temp;
        list->capacity *=2;
       }else{
         for(i=list->size;i>postion-1;i--){
            list->data[i]=list->data[i-1];
         }
       }
       list->data[position-1]=val;
       list->size++;

       return 1; 
  }

You probably want to do something similar in append because otherwise you just have a static array. The idea of a vector is that it grows and shrinks automatically.

Edit: In C you cannot just declare a prototype of a function inside a struct and than access it. The only way you can do something similar to what you want to do is by storing a pointer to each of the functions inside your struct. Have a look at the following post. Define functions in structs

Community
  • 1
  • 1
rex123
  • 371
  • 3
  • 16