typedef struct NodeStruct NODE;
typedef struct ListStruct LIST;
struct NodeStruct {
void *item;
NODE *previous;
NODE *next;
int location;
};
struct ListStruct {
int ListSize;
NODE *HeadNode;
NODE *CurrentNode;
NODE *TailNode;
NODE NodeArray[MaxNode];
};
NODE *HeadArray[MaxHead];
static int HeadCount = -1;
int HeadCurrent = 0;
int i;
Fairly new at C, sorry to sound so very inexperienced ahead of time...
I would like to implement the LIST abstract data type without the use of malloc() or free(), so by storing the nodes in statically allocated arrays. Each list element is a NODE, and can hold one "item". I need to be able to traverse through the nodes using next and previous, and have the notion of "current", "tail", "head".
There are also two arrays... One array of pointers to the "heads", and so then for each head, there is an array of "nodes". I also need to be able to change the current item to one anywhere the middle (or front or end) of the list, be able to delete that node, and then somehow (this is the most confusing part to me) be able to use that spot in the array again, BUT, I can't traverse through the list to find a 'free' node because that's not efficient.
I also need to do the same thing for the "head" array, so, there is a notion of max number of lists (which would mean the head array is full), and I also need to be able to delete an entire array of nodes, (which in turn, means I have to be able to delete a head node from that list as well and somehow be able to put one back there if i need that space).
Problem is I've had a horribly awful time trying to implement this! The above code is the structure I have been working with so far, and I've tried two different ideas:
1: Every time we "remove" a node from the array (which as far as I'm concerned is just setting it's ITEM to NULL and switching it's connected nodes nexts' and previous' to each other), we move the rightmost node in the array to this spot. When we add a new node, we always add it to that spot. So, no searching, but absolutely a nightmare to implement (in my experience).
2: Have a separate list (stack) of "free nodes", which I can take from when I need a new one. However, this means that I'd have to create all those nodes initially and maybe not use them which would not be good memory-wise.
What I would really like to do, and can't figure out, is as follows:
For the "heads" array: we have an array of pointers which points to the "head" of each node. For the each "heads" we have, initially, an empty array of nodes. For each empty array of nodes, we also have a list of "free" pointers, which are pointers to each free spot in the array, and when we insert a new item, we take the top address of this pointer array and that's where the new node will be placed in the node array. When we delete from the node array, we place that nodes address on top of the stack.
However, I'm stuck on what ListCreate will look like, because that's just an empty array of nodes, and the head array won't actually be pointing to anything then.
These are the specifications of what I'm working on so I can't change the requirements too much. ie, i can't not have an array of head pointers.
Any ideas on how I can implement this efficiently and to these requirements? I just need help setting up the structures and what 'list create' will look like, and the general idea behind the manipulation of the nodes. I can figure all else out hopefully. I'm just really stuck on a general, efficient idea.
Any help is appreciated. Thank you.