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
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; } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started