
Ok, using a linked list for grid isn't really to draw the grid, but it can be. It use to save positions in a list that describe a square. Use any system you want, the code I given you wasn't exactly for created large grids it was just meant to help teach you understand how it works. There are easier methods that make creating the linked list faster, less lines of code.
Also, I don't use negative numbers when doing coordinates, the reason being because the natural first position on a coordinate map is 0,0. Then continue outward positively. A pixel grid on a computer doesn't use negative, and usually negative numbers represent off the map, or void area.
Using linked list for grids is perfect because you can add more properties to a single element than an array, where in an array you would have to create multiple arrays, they would have to be of set size, or you would have to allocate more memory for the array and do expensive transfers of values to all of your arrays. Both arrays and linked lists require you allocate memory, but the difference being that when you allocate more memory for the new element in the list you don't have to create an array of larger size, then transfer all of the information, then free the old array. Not to mention you would have to do this for every array that has values pertaining to, what could have been, a single element in a linked list.
Also, with arrays you literally need double the memory capacity just to add a single element to the array. Where as a linked list you only need enough memory to hold the new structure you are creating, and if you don't have enough memory for the new structure you know right away, whereas an array system you would have to first try to create every array, test to make sure you have enough memory to create those new arrays.
Example of array system versus linked list system:
- Code: Select all
int xValueArray [100];
int yValueArray [100];
char NameArray [100] [27];
Type TypeArray1 [100];
Type TypeArray2 [100];
int CreateNewArrays(int oldSize, int newSize)
{
int xValueArrayTemp;
Allocate memory for xValueArrayTemp of newSize;
check to see if Memory was allocated if not return failed;
Transfer values from xValueArray to xValueArrayTemp;
free xValueArray;
Allocate memory for xValueArray of newSize;
check to see if Memory was allocated if not try allocating space of oldSize;
check to see if Memory was allocated if not free xValueArrayTemp, return failed and begin ending game;
The game must end because there is no array for xValueArray so functions cannot run properly;
Otherwise:
Transfer values from xValueArrayTemp to xValueArray;
Then start the process over for every array;
}
Linked List
- Code: Select all
typedef struct MyElement
{
int x;
int y;
char name;
Type TypeVar1;
Type TypeVar2;
struct MyElement * NextElement;
} MyElement;
MyElement * FirstElement;
MyElement * LastElement;
int NewElement(int x, int y, char *name, Type TypeVar1, TypeVar2)
{
Allocate space for new element
check to see if space was allocated if not return failed
Set values of the struct
LastElement->NextElement = NewElement;
return success
}
void InitializeLinkedList()
{
Allocate space for FirstElement if failed endgame because if there isn't enough space to create the first item then there isn't a game, we just started.
LastElement = FirstElement;
for(number of elements)
{
if(!NewElement(variables))
{ end game because there isn't enough memory to run the game}
}
}
Its easy to use linked lists and they are very useful for doing exactly what you want to do.
Arrays have their use to but for creating grid structures that hold values such as this it just depends on the way you code it. If you are comfortable with using many arrays and aren't going to be resizing them a lot and need fast random access to any position in the grid then arrays can be used for just that. But the problem is arrays require more memory when handling them if not accessed through reference.
I like to use a hybrid Linked List/Array system that allows for random access to the linked list but doesn't require many arrays. You can also use Structs with arrays so you don't have to create multiple arrays for single values such as:
- Code: Select all
typedef struct MyElement
{
int x;
int y;
char name;
Type TypeVar1;
Type TypeVar2;
} MyElement;
MyElement ElementArray [100];
The issue being if you were to want to resize the array then you would have to still allocate a temporary array of the same size then transfer the values, then free the old array and allocate the space for the new array size. Its a taxing process but can be made also simpler if you just use an array of pointers to the structures rather then having an array of structures themselves.
- Code: Select all
typedef struct MyElement
{
int x;
int y;
char name;
Type TypeVar1;
Type TypeVar2;
} MyElement;
MyElement * ElementArray [100];
But this process requires that you allocate space for each element of the array and pass the address of the element to the position in the array. This makes resizing the array less taxing and more memory friendly. But you still have to allocate each struct in the array which if you run out of memory then you have to make sure that if your array isn't full that when you are accessing pointers in the array that are null that you don't dereference them.
All these methods are essentially the same.