T
Simple.Type struct Data
{
int value; // Данные, словом
int next, prev; // ИНДЕКСЫ следующего и предыдущего элементов списка,
// скажем, -1 - в роли NULL
};
That's it. That's how it works. Each structure is related to the previous and the following, not the indexes, but their indices In this body.Don't you need to get any details?Here's a demonstration program (with very little functionality):#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Node_
{
int value;
int prev, next;
int occupied;
} Node;
int getNode(Node*list, int size) // Возврат первого свободного
{
for(int i = 0; i < size; ++i)
if (!list[i].occupied)
{
list[i].occupied = 1;
return i;
}
return -1;
}
void freeNode(Node*list, int index)
{
list[index].occupied = 0;
}
// Добавление в список, возврат индекса
int addToList(inthead, Node list, int size, int value)
{
int idx = getNode(list,size);
if (idx >= 0)
{
list[idx].value = value;
list[idx].next = list[idx].prev = -1;
if (*head < 0) // Пустой список
{
*head = idx;
}
else
{
// Ищем последний
int curr = *head;
while(list[curr].next >= 0)
{
curr = list[curr].next;
}
list[curr].next = idx;
list[idx].prev = curr;
}
}
return idx;
}
void showList(int head, Node* list)
{
// Идем по списку и выводим
int curr = head;
while(curr >= 0)
{
printf("%d ",list[curr].value);
curr = list[curr].next;
}
puts("");
}
int main(int argc, const char * argv[])
{
Node list[100] = {{0,0,0,0}};
int head = -1;
addToList(&head,list,100,1);
addToList(&head,list,100,2);
addToList(&head,list,100,3);
showList(head,list);
}
As you can see, it's just that the indexes play a role. What's an index, after all? Show me where the information is interesting. He shows up in a cell with that number.Here, I found the article, and I couldn't torture you. https://rsdn.org/article/alg/list.xml