Question:
Help with Creating an ArrayList using C (Not Java/C++, etc)?
John
2009-09-23 14:09:53 UTC
Any suggestions creating an ArrayLiist Using C. I've been told this is an ArrayList (or Vector) that is basically an array that can grow as needed. Any help? Thank you.
Six answers:
2009-09-30 23:29:56 UTC
Hm....



















Example link list:

#include

#include

struct llist{

int value;

llist *next;

llist *prev;

};



int main(){

long sum=0;

float mean=0.0;

llist *mylist,*start;

clrscr();

start=new llist;

mylist=start;

cout<<"Get data"<
for(int i=0;i<4;i++){

cin>>mylist->value;

mylist->next=new llist;

mylist=mylist->next;

}

mylist->value=0;

mylist->next=NULL;

cout<<"Outdata"<
mylist=start;

while(mylist){

sum+=mylist->value;

cout<value<
mylist=mylist->next;

}

mean=sum/100.0;

cout<<"Average "<
delete start->next;

delete start;

return 0;}



Im using C++, but this is C style.
Joseph
2009-09-24 15:18:33 UTC
Hi, I'm not exactly sure what you need with an "ArrayList", since it could mean a list that's accessible as an array, or a list embedded inside an array, etc. If you want to make your own vector, and you're using C, not C++, you can do something like this:



Start with a struct to hold all your data.

struct ArrayList

{

unsigned capacity, size;

float growth_rate;

int * array;

};





Now make some methods to access the data properly. Your teacher probably provided a list of functions you were to implement, right?



ArrayList Create_ArrayList( size );

This should return an ArrayList struct with a size and capacity equal to the size given.



Delete_ArrayList( ArrayList * list );

Free the memory and set the array members appropriately.



int Get( ArrayList * list, unsigned index );

Retrieve the value of an element in the array. The client can also use the array member of the struct, but that cannot check for out of bounds.



int Set( ArrayList * list, unsigned index, int );

Set the value of an element in the array, and return the set value for convenience. The client can also use the array member, but it's unsafe.



void Resize ( ArrayList * list, unsigned new_size );

Change the size of the array. If the new size is bigger than the current capacity, call the Reserve() function to grow the array.



void Reserve( ArrayList * list, unsigned new_capacity );

Change the capacity of the array. If the new capacity is bigger, allocate a new array, copy everything over, then delete the old array and set the array member to this newly allocated array.



void Add ( ArrayList * list, int new_item );

Add an item to the end of the array. If the array is full, call Reserve() with capacity * growth_rate.



There are many more members you could write, but do whatever you're assigned. Ask another question if you need help implementing any particular part of the assignment.
Peter
2009-09-23 14:24:46 UTC
This is something that's been done before. So if you want to redo it, you should probably consider your needs.



There are a number of ways to implement an expanding array. For instance, you can do a single or double-linked list implementation, where each node connects to the next node and so on. That allows for quick inserts and deletes, but slower accesses. So it's good for applications that need to modify their data a lot but not necessarily access that data often.



You could also allocate space, then re-allocate as needed. This would allow contiguous data storage, allowing for super-fast access to data. But it leads to much slower inserts and deletes. Also, if the limit of the allocated space is reached, you'll need to reallocate a larger space and move the data, which will take time. This is how the C++ Vector implementation works [1].
annino
2016-12-04 00:56:30 UTC
first factor: Hardware isn't a language. device Language is purely binary meaning a million's and 0's. assembly is purely one step up from device Language. it is English words which could be fairly actually translated into binary. additionally: seen uncomplicated and uncomplicated are just about a similar factor. it's time-honored for giving programmers undesirable behavior. i do no longer advise it. Smalltalk is meant to be enormously cool, yet i've got by no capacity have been given the possibility to play with it. no one makes use of Pascal anymore. C++ has way too many features, which will sound like a sturdy factor, in spite of the undeniable fact that it makes the code harder to envision. Java is sturdy in that it has a go-platform digital device, however the language is criticized for being to verbose and lacking features that different programming languages have. which would be unfair because of the fact Java does have between the biggest widely used libraries. C# is made by capacity of Microsoft and is made to run purely on abode windows. there's a Linux port, yet no longer something for OS X that i understand of. It suffers from verbosity like Java, even with the incontrovertible fact that it is meant to be a splash extra suited. human beings look to love C, even with the incontrovertible fact that it is a low-point language that makes you do your guy or woman memory administration. while you're no longer careful, you would be affected by capacity of issues like memory leaks, buffer overflows, and segmentation faults.
?
2009-09-23 14:32:27 UTC
There is no elegant want to do this in C, since it lacks the OOP features of C++ and Java. All you can really do is use malloc and realloc to dynamically shrink and grow your array, but to do this you'll need to access the array elements via functions rather than directly.
▐▀▀♦▀▀▌ ♦Oprah♦ ▐▄▄♦▄▄▌
2009-09-23 14:32:29 UTC
You can implement it as an array, or a linked list (more efficient, but also a bit more complex)



An array implementation is straightforward: start by pre-allocating a dynamic array of some initial capacity (e.g. 10 elements). Keep adding things to it, until the capacity is exceeded; then re-allocate the array to twice the capacity.



int len = 0, capacity=10;

int* list = (int*) malloc(capacity*sizeof(int)); // Pre-allocate storage



int[len++] = 77; // Add 77 to list

/* .. add more elements ... */



if (len >= capacity) { // Capacity reached! Double capacity and reallocate!

   capacity *= 2;

   list = (int*) realloc(list, capacity*sizeof(int));

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...