1

Here is the question of exercise CLRS 10.3-4 I am trying to solve

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.)

Here is my soln so far

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;
}

Now , The problem is , If this is the case , We don't need linked list. If deletion is O(n) we can implement it using normal array. Secondly I have not used the array implementation of stack too . So where is the catch? How should I start?

Unbound
  • 243
  • 2
  • 12
  • Isn't that a linked list you implemented there? – Niklas B. Mar 22 '14 at 06:28
  • Yes, Its a compact Linked List implementation . – Unbound Mar 22 '14 at 06:30
  • I guess I don't understand the question then – Niklas B. Mar 22 '14 at 06:30
  • The question is to implement the Linked list in such a way that, If there are n elements and m is the size of the linked list , then the allocated objects should be from 1...n and free memory should be n+1...m .That's what I think . I really don't know about virtual memory paging. So I can't understand any better. – Unbound Mar 22 '14 at 06:33
  • You probably shouldn't make a variable called `free` since that's sort of an important word in the C (therefore C++) language. – John Zwinck Mar 22 '14 at 06:35
  • One of the main advantages of dual linked lists is ability to insert items at random locations. Having blocks of items implemented in arrays kinda kills this since arrays are good for end insertion only. However: perhaps this task requires you to just store items in contiguous blocks of memory. In this case, you have to create a separate class for the list item and then implement an allocator that would allocate one block of memory for n items and then keep track of empty spaces in such allocated blocks. So we're just talking a standard custom allocator here. – velis Mar 22 '14 at 06:42
  • But it said to use stack. So where will I be using stack? – Unbound Mar 22 '14 at 06:58

1 Answers1

-1

You don't have to shrink the list right away. Simply leave a hole and link that hole to your free list. Once you've allocated the memory, it's yours. So let's say your page size is 1K. Your initial allocated list size would then be 1K, even if the list is empty. Now you can add and remove items very effectively.

Then introduce another method to pack your list, i.e. remove all holes. Keep in mind that after calling the pack-method, all 'references' become invalid.

alzaimar
  • 4,572
  • 1
  • 16
  • 30