Error message

  • Deprecated function: Return type of DatabaseStatementBase::execute($args = [], $options = []) should either be compatible with PDOStatement::execute(?array $params = null): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2244 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in drupal_random_bytes() (line 2268 of /home2/psicolog/public_html/feliponcho/includes/bootstrap.inc).
  • Deprecated function: rtrim(): Passing null to parameter #1 ($string) of type string is deprecated in url() (line 2349 of /home2/psicolog/public_html/feliponcho/includes/common.inc).
  • Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in url_is_external() (line 2393 of /home2/psicolog/public_html/feliponcho/includes/common.inc).
  • Deprecated function: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in url_is_external() (line 2395 of /home2/psicolog/public_html/feliponcho/includes/common.inc).
  • Deprecated function: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in url() (line 2311 of /home2/psicolog/public_html/feliponcho/includes/common.inc).

Two reference Single Linked List

An easier way to add elements to the end of a list (enqueue) without having to walkthrough all the list to find the last element is by keeping not only a reference to the head of the list (first element) but also to the tail of the list (last element). 

Here is a proposal to the data structures needed for this purpose

struct Two_ref_list_node {
    int val;
    struct Two_ref_list_node *next;
};

typedef struct Two_ref_list_node node;

struct Two_ref_list_headers
{
    node *head;
    node *tail;
};

typedef struct Two_ref_list_headers headers;

This reduces search time, but it also adds complexity to the algorithms as we need to make sure we update both reference correctly when we add nodes and specially when we remove nodes. 

Another problem we still have is that we still need to look for the node located previous to the last node, but at least we don't have to look for the last node because we already have a reference to it (tail), so we are basically saving a search.

void enqueue(headers *list, int data)
{
    node *curr = create_node(data);
    if(!(list->tail))
    {
        list->head = list->tail = curr;        //notice that when we are inserting the first node to the list, tail and head must point to the same node.
    }
    else
    {
        list->tail->next = curr;
        list->tail = curr;        
    }
}

/* we still need this function to find the node previous to the last */
/* an interesting approach would be to have a third reference to the "previous-to-tail" node, but that will imply extra work in the algorithms to update the reference */
static node *previous_to_node(headers list, node *findee)
{
    while(list.head)
    {
        if(list.head->next == findee)
            return list.head;
        list.head=list.head->next;
    }
    return list.head;
}

int dequeue(headers *list)
{
    int ret = 0;
    if(list->tail)
    {        
        node *previous = previous_to_node(*list,list->tail);
        ret = list->tail->val;
        free(list->tail);
        if(previous)
            previous->next = NULL;
        else
            list->head = NULL;
        list->tail = previous;
    }
    return ret;
}