Introduction to Algorithms 10.3-4: Compact bidirectional linked list __ algorithm

Source: Internet
Author: User
Tags compact int size prev

topic:
We tend to want all elements of a two-way list to remain compact in memory, for example, in most group representations, to occupy the top m subscript position. (This is the case in the computational environment of page-type virtual storage.) Assuming that there are no pointers to the list element except the pointer to the table itself, try to show how to implement the process Allocate-object and free-object to keep the table compact. (Hint: Use an array of stacks to implement.)
Answer:
In the answer to an introduction to an algorithm in Daniel, allocating a list element position from the top of the array to the final allocation of space, if you delete an element, and the element in the storage list of the array is not the last element, You need to move all the list elements at the right end of the array to the left. This method of deleting a single operation consumes linear time.
The policies used in this article are different. When assigning a linked list element position, it is also allocated backwards from the top of the array of stored linked lists. The following conventions are: All arrays after the location of the
L->free are empty and contain l->free locations that are both available for allocation.
Each time you delete a linked list element except the l->free-1 location, move the element of the l->free-1 position to the location of the deleted element and adjust the prev and next points of the element before and after the element, and then subtract the L->free by 1. All of the array elements that are still l->free and thereafter remain in the pending state at this time.
The declarations of some functions and structs are as follows:

typedef struct TIGHT_DOUBLE_ARRAY_DOUBLE_LINKED_LIST
{
    int size;
    int count;
    int head;
    int free;
    item_t * KEY;
    int * NEXT;
    int * prev;
} Tdadll;
void Tdadll_traverse (Tdadll * L);
tdadll* tdadll_init (int size);
item_t* tdadll_get_object (Tdadll * L, int location);
int Tdadll_get_next (Tdadll * L, int location);
void Tdadll_set_next (Tdadll * L, int location, int next);
int Tdadll_get_prev (Tdadll * L, int location);
void Tdadll_set_prev (Tdadll * L, int location, int prev);
void Tdadll_move (Tdadll * L, int src, int dest);
int Tdadll_free (Tdadll * L, int location);
int Tdadll_allocate (Tdadll * L);
int Tdadll_insert (Tdadll * L, item_t item);
int Tdadll_search (Tdadll * L, item_t item);
int Tdadll_delete (Tdadll * L, item_t item);

Implemented as follows:

Functions for tight static double array double linked list void Tdadll_traverse (Tdadll * l) {if (L = = NULL) {
        fprintf (stderr, "not initialized.\n");
    Return
    } if (L->head = = 0) {fprintf (stderr, "Empty linked list.\n");
    int location = l->head; printf ("Total elements number is%3d. Linked list size is%3d.
    Linked list head is%d\n ", \ L->count, l->size, L->head); while (location!= 0) {printf ("%3d Item prev are%3d, location is%3d, Next is%3d.\n", \ Tdadll_get_o
        Bject (l, location)->key, Tdadll_get_prev (l, location), location, Tdadll_get_next (l, location));
    Location = Tdadll_get_next (L, location);
printf ("Free spaces start postion is%3d\n", l->free);
    } tdadll* tdadll_init (int size) {Tdadll * L = (tdadll*) malloc (sizeof (Tdadll)); if (!
        L) {fprintf (stderr, "tight static double-array double linked list init fail.\n"); return NULL;
    } l->size = size;
    L->count = 0;
    L->head = 0;
    L->free = 1;
    L->key = (item_t*) calloc (size, sizeof (item_t));
    L->prev = (int*) calloc (size, sizeof (int));
    L->next = (int*) calloc (size, sizeof (int)); if (! L->key | | ! L->next | | !
        L->prev) {fprintf (stderr, "Tighe static double array double linked list content init fail.\n");
    return NULL;
return L; } item_t* Tdadll_get_object (Tdadll * L, int location) {return l->key + location-1;} int Tdadll_get_next (Tdadll
    * l, int location) {return l->next[location-1];} void Tdadll_set_next (Tdadll * l, int location, int next) {
L->next[location-1] = next; int Tdadll_get_prev (Tdadll * l, int location) {return l->prev[location-1];} void Tdadll_set_prev (Tdadll * L, int location, int prev) {l->prev[location-1] = prev;} void Tdadll_move (Tdadll * L, int src, int dest) {if (src <= 0 | | src > L->size | | dest <= 0 | |
        Dest > L->size) {fprintf (stderr, "Move out of range.\n");
    Return
    } L->next[dest-1] = l->next[src-1];
    L-&GT;PREV[DEST-1] = l->prev[src-1];
L-&GT;KEY[DEST-1] = l->key[src-1]; int Tdadll_free (Tdadll * L, int location) {if (location <= 0 | | location > L->size) {fprintf (stde
        RR, "Free%d out of range.\n", location);
    return 0; } if (location >= l->free && l->free!= 0) {fprintf (stderr, "free%d out of range.\n", Locat
        ION);
    return 0;
        } if (L->free = = 0) {tdadll_move (L, l->size, location);
        L->free = l->size;
    if (l->size = = l->head) L->head = location;
        else if (l->free!= location + 1) {Tdadll_move (L, l->free-1, location);
        L->free = l->free-1;
    if (L->free = = l->head) L->head = location; else {l->free--;
    return 1;
    }//printf ("Location:%d free:%d\n", location, L->free);
    int next = Tdadll_get_next (L, location);
    int prev = Tdadll_get_prev (L, location);
    if (next!= 0) {Tdadll_set_prev (L, Next, location);
    } if (prev!= 0) {tdadll_set_next (L, prev, location);
return 1;
        int Tdadll_allocate (Tdadll * L) {if (L->free = 0) {fprintf (stderr, "Allocatet out of range.\n");
    return 0;
    int location = l->free;
    if (L->free = = l->size) {l->free = 0;
    else {l->free++;
return location;
        int Tdadll_insert (Tdadll * l, item_t Item) {if (L = = NULL) {fprintf (stderr, "not initialized.\n");
    return 0;
    int location = Tdadll_allocate (L);
        if (location = = 0) {fprintf (stderr, "Allocate location fail.\n");
    return location;
    } tdadll_set_next (L, location, l->head);
Tdadll_set_prev (L, location, 0);    *tdadll_get_object (L, location) = Item;
    if (l->head!= 0) {Tdadll_set_prev (L, L->head, location);
    } l->count++;
    L->head = location;
return location;
        int Tdadll_search (Tdadll * l, item_t Item) {if (L = = NULL) {fprintf (stderr, "not initialized.\n");
    return 0;
        } if (L->head = = 0) {fprintf (stderr, "Empty linked list.\n");
    return 0;
    int location = l->head;
        while (location!= 0) {if (Tdadll_get_object (L, location)->key = = Item.key) {return location;
    } location = Tdadll_get_next (L, location);
    } fprintf (stderr, "Item%d cannot be found.\n", item.key);
return 0;
    int Tdadll_delete (Tdadll * l, item_t item) {int location = Tdadll_search (L, item);
        if (location = = 0) {fprintf (stderr, "Delete%d fail.\n", item.key);
    return 0;
    int next = Tdadll_get_next (L, location); int prev = Tdadll_get_prev (L, Location);
    printf ("location:%d next:%d prev:%d\n", location, Next, prev);
    if (next!= 0) {Tdadll_set_prev (L, Next, prev);
    } if (prev!= 0) {tdadll_set_next (L, Prev, next);
    } if (location = = l->head) {l->head = next;
    } l->count--;
    Tdadll_traverse (L);
Return Tdadll_free (L, location); }
//--------------------------------------------------------------------------

The

can be tested with the following code:

void Test_for_tdadll () {Tdadll * L = Tdadll_init (SIZE);
    item_t item = {0, NULL};
        for (int i = 0; i < SIZE; ++i) {item.key = i;
    Tdadll_insert (L, item);
    } tdadll_traverse (L);
        for (int i = 0; i < ++i) {Item.key = i + 10;
    Tdadll_delete (L, item);
    } tdadll_traverse (L);
        for (int i = 0; i < SIZE; ++i) {item.key = i;
    Tdadll_delete (L, item);
    } tdadll_traverse (L);
    printf ("-------------------------------------------------------------------\ n");
        for (int i = 0; i < SIZE; ++i) {item.key = i;
    Tdadll_insert (L, item);
    } tdadll_traverse (L);
        for (int i = 0; i < ++i) {Item.key = i + 10;
    Tdadll_delete (L, item);
    } tdadll_traverse (L);
        for (int i = SIZE; I >=-1; i.) {item.key = i;
    Tdadll_delete (L, item);
} tdadll_traverse (L); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.