Search code examples
c++functionlinked-listtraversal

Write a function takes in 2 parameters – the pointer head OR tail plus the direction to traverse for a linked list


Here is how I defined and initialized a linked list

struct listrec
{
    struct listrec    *prev;
    float       value;
    struct listrec    *next;
};

listrec *head, *tail;

int main() {
int number;
cin >> number;
float content = 2.0;
    for (float i = 0; i < number; i++)
    {
        if (i == 0)
        {
            head = new listrec;
            head->prev = nullptr;
            head->value = 1.0;
            head->next = nullptr;
            tail = head;
        }
        else
        {
            auto *newNode = new listrec;
            newNode->value = content++;
            newNode->next = nullptr;
            newNode->prev = tail;
            tail->next = newNode;
            tail = tail->next;
        }
    }
return 0;
}

This is how the linked list looks like

enter image description here

I need to " write a function that takes two input parameters - the pointer head OR tail plus a parameter of which direction to traverse - to traverse the linked list and return the number of elements in the list. "

I have no idea how to write a function like that…

I know, if I want to count the number of elements from the first node, then I can write a function like this:

float listSize(listrec* head)
{
    int count = 0; 
    listrec* current = head; // Initialize current  
    while (current != NULL)
    {
        count++;
        current = current->next;
    }
    return count;
}

Or, if I want to count the elements from the last element, then

float listSize2(listrec* tail)
{
    int count = 1;
    listrec* current = tail;
    while (tail->prev != NULL)
    {
        count++;
        tail = tail->prev;
    }
    return count;
}

But how can I combine these two? Any hints would be appreciated!


Solution

  • Here is the function, assuming a doubly linked list:

    enum class Direction {FORWARD, REVERSE};
    
    struct Node
    {
        Node * previous;
        Node * next;
    };
    
    unsigned int Count(Node * p_begin, Direction traverse_dir)
    {
        unsigned int node_count = 0U;
        while (p_begin != nullptr)
        {
            ++node_count;
            if (traverse_dir == FORWARD)
            {
                p_begin = p_begin->next;
            }
            else
            {
                p_begin = p_begin->previous;
            }
        }
        return node_count;
    }
    

    Per the requirement, the function takes 2 parameters, a pointer to a head or tail node, and a direction and returns the quantity of nodes traversed.

    The function starts at the pointer passed, then goes forward or reverse (depending on the direction parameter), and increments the node counter. The loop stops when a null pointer is encountered, which usually signals the beginning or end of a list.

    Since only a node class is used, you can inherit from the Node to make various list types:

    struct Integer_Node : public Node
    {
        int data;
    };
    

    The data field does not play a role in traversing the list, so it was removed from the fundamental node object.