Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

doub Assume a doubly linked list with a list head as shown. The list head contai

ID: 672416 • Letter: D

Question

doub Assume a doubly linked list with a list head as shown. The list head contains information on the flight including the flight number (FLT) The other nodes in the list represent customers Write an algorithm to insert a new customer pointed to by IP in the list to the right of any specified node You must locate the specified node to perform the insertion. If the specified customer does not exist, print an appropriate message Be sure your algorithm covers all possible cases including the empty list.

Explanation / Answer

c++ function for the same.

void insert(int value, int position)

{

    if (start == NULL)

    {

        cout<<"List is Empty"<<endl;

        return;

    }

    struct node *temp, *q;

    int i;

    q = start;

    for (i = 0;i < position - 1;i++)

    {

        q = q->next;

        if (q == NULL)

        {

            cout<<"Not Possible. List has lesser elements. It contains ";

            cout<<position<<" elements."<<endl;

            return;

        }

    }

    temp = new(struct node);

    temp->info = value;

    if (q->next == NULL)

    {

        q->next = temp;

        temp->next = NULL;

        temp->prev = q;    

    }

    else

    {

        temp->next = q->next;

        temp->next->prev = temp;

        q->next = temp;

        temp->prev = q;

    }

    cout<<"value added to the list"<<endl;

}

The idea here is to check whether list is empty or not and whether it has sufficient space to place the number in the list.