Does not need to compile....just need an idea as to how I would populate a doubl
ID: 3880765 • Letter: D
Question
Does not need to compile....just need an idea as to how I would populate a doubly linked list like this
Homework Write the code for the following function 2. template void File2List (istream ∈, T stopval 1// Precondition: in is connected to a file // containing values of type T, and stopval // is a value used to indicate end of input; ll stopval is guaranteed to appear in the // input file Postcondition: h and t are the header and // trailer pointers, respectively, of a // doubly-linked list containing the values // in the input file in the order they appear // in the fileExplanation / Answer
// assuming setNext() function sets the next pointer
// and setPrev() sets the prev pointer of the node
template<class T>
void File2List(istream &in, T stopval, Dnode* &h, Dnode* &t)
{
// trav point to the right most node in te list
Dnode *trav = NULL;
// loop untill file does not end
while(!in.eof())
{
T temp;
// scan the element in the file
in>>temp;
// if the current value is the value to be stopped at
if(temp == stopval)
return;
// create a new node
Dnode *new_node = new Dnode(temp);
// set the pointers to NULL
new_node->setNext(NULL);
new_node->setPrev(NULL);
// if the list is empty
if(h == NULL)
{
// make new_node the new head of the list
h = new_node;
// make new_node the new tail of the list
t = new_node;
// point trav to the head of the list
trav = h;
}
// if the list is not empty
else
{
// add new node to the right of the last node
trav->setNext(new_node);
// make the pev pointer point to trav
new_node->setPrev(trav);
// make new_node the new tail of the list
t = new_node;
// point trav to new_node
trav = new_node
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.