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

Create a doubly-linked list of a length determined by user input Write a main ()

ID: 3805561 • Letter: C

Question

Create a doubly-linked list of a length determined by user input

Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested:

Assume entries in a linked list are of type struct listrec:

struct listrec
{
   struct listrec *prev;
float        value;
   struct listrec *next;  
};
listrec *head, *tail;

I have the following, I'm just not sure how to make a doubly-linked list based off a dynamic input:

#include
#include
using namespace std;

int main()
{
   struct listrec
   {
       struct listrec *prev;
       float        nbr;
       struct listrec *next;  
   };

   listrec *head, *tail;


   float userin;

   cout << " Please enter the number of nodes you would like to create "<< endl;
   cin>>userin;
   while(!(cin >> userin) || userin < 0)
   {
       cin.clear();
       cin.ignore(numeric_limits::max(), ' ');
       cout << "Invalid input, the input must be zero or greater and not a letter" << endl;
   }

   head = NULL; current = NULL;
   for(int i =1; i < userin+1; i++)
   {
       if (head==NULL)
       {
           head = new node;
           head -> nbr = i;
           head -> next = NULL;
           current = head;
       }
       else
       {
           current -> next = new node;
           current = current -> next;
           current -> tail = NULL;
           current -> nbr = i;
           current -> next = NULL;
       }
   }


}

Explanation / Answer

#include <iostream>

/*

    DOUBLY LINKED LIST


   Create a doubly-linked list of a length determined by user input
   Write a main() routine in which the user is asked the number of
   nodes to create in the list (number greater than or equal to zero)
   then create the following type of linked list (use a loop to
   initialize list) based on the number of nodes requested:

   Assume entries in a linked list are of type struct listrec:

   struct listrec
   {
       struct listrec *prev;
       float value;
       struct listrec *next;
   };
  
   listrec *head, *tail;
*/

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

listrec *head, *tail, *current, *temp;
float val;

int main()
{
   int num_nodes;
   std::cout << "How many nodes ya wanna create?: ";
   std::cin >> num_nodes;

   head    = NULL;
   tail    = NULL;
   current = NULL;
   temp    = NULL;

   for (int i = 0; i < num_nodes; ++i)
   {
       std::cout << "Enter " << i << "th value: ";
       std::cin >> val;      
       temp        = new listrec;
       temp->prev    = current;
       temp->next    = NULL;
       temp->value = val;

       if (current != NULL)
           current->next = temp;      
       current    = temp;

       // std::cout << "-current=" << current << " -temp=" << temp
       //    << " -temp->prev=" << temp->prev << " -temp->next=" << temp->next
       //    << " -temp->value=" << temp->value << std::endl;

       // It's the first iteration.
       if (head == NULL && current->prev == NULL)
           head = temp;
       // Last Iteration.
       else if (i == num_nodes - 1)
           tail = temp;
   }

   // std::cout << "-head=" << head << " -tail=" << tail
   //    << std::endl;

   std::cout << "Going onwards: " << std::endl;
   // Going forwards.
   listrec *iter;
   iter = head;
   for (int i = 0; i < num_nodes; ++i)
   {
       std::cout << iter->value << std::endl;
       iter = iter->next;
   }

   std::cout << "Reverseee: " << std::endl;
   // Reverse
   iter = tail;
   while (1)
   {
       std::cout << iter->value << std::endl;
       iter = iter->prev;
       if (iter == NULL)
           break;
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote