I want to create double linked cyclical list for the little game I am making. I want it to be a list because of the speed it offers when it comes to adding and deleting new elements. If i wanted to use a dynamical table, any kind of adding/deleting operation would require me to rewrite entire table and that would slow down the program severely (atleast from my understanding). The only problem with that solution is the fact, that I do not fully understand how to do such a list ;)
struct parts{
char name;
parts *next, *prev;
parts *connected;
};
void add_part(struct parts **head, struct parts **tail, char name)
{
struct parts *a;
a = (struct parts*)malloc(sizeof(struct parts));
a->name = name;
if ((*head) == NULL && (*tail) == NULL)
{
*head = a;
*tail = a;
a->next = NULL;
a->prev = NULL;
}
else
{
a->next = *head;
a->prev = NULL;
}
}
void display(parts *head) {
parts *temp = head;
while (temp != NULL){
cout << temp->name << endl;
temp = temp->next;
}
}
int main ()
{
char names[] = "ABCDEFGHIJKLMNOPRSTUWXYZ";
segmenty *head = NULL;
segmenty *tail = NULL;
int count_parts;
cin >>count_parts;
for (int i=0; i < count_parts && i<24; i++){
add_part(&head, &tail, names[i]);
}
display(head);
return 0;
}
What I want user to be able to do is to type in the amount of elements he wants and then I want to name each element with a letter from the alphabet and put them in my list so that every element is connected to elements before and after it and tail is connected to head(I want the list to be cyclical). Unfortunately my pointer skills are kind of lacking... *connected is a pointer i want to use for elements that are currently on the ground (only one element can touch the ground at once) for uses such as deleting or adding the new element e.g. if the element hits the trap i want to delete that one specific element, not any other one.