Вот вопрос упражнения CLRS 10.3-4
Я пытаюсь решить
It is often desirable to keep all elements of a doubly linked list compact in storage, using, for example, the first m index locations in the multiple-array representation. (This is the case in a paged, virtual-memory computing environment.) Explain how to implement the procedures ALLOCATE OBJECT and FREE OBJECT so that the representation is compact. Assume that there are no pointers to elements of the linked list outside the list itself. (Hint: Use the array implementation of a stack.)
Вот мой раствор до сих пор
int free;
int allocate()
{
if(free == n+1)
return 0;
int tmp = free;
free = next[free];
return tmp;
}
int deallocate(int pos)
{
for(;pos[next]!=free;pos[next])
{
next[pos] = next[next[pos]];
prev[pos] = prev[next[pos]];
key[pos] = key[next[pos]];
}
int tmp = free;
free = pos;
next[free] = tmp;
}
Теперь проблема в том, что если это так, нам не нужен связанный список. Если удаление равно O(n), мы можем реализовать его, используя обычный массив. Во-вторых, я тоже не использовал реализацию массива стека. Так где подвох? Как мне начать?
free
, так как это важное слово в языке C (следовательно, C++). - person John Zwinck   schedule 22.03.2014