dd a new public member function to the LinkedList class named reverse() which reverses the items in the list. You must take advantage of the doubly-linked list structure to do this efficiently as discussed in the videos/pdfs, i.e. swap each node’s prev/next pointers, and finally swap headPtr/tailPtr. Demonstrate your function works by creating a sample list of a few entries in main(), printing out the contents of the list, reversing the list, and then printing out the contents of the list again to show that the list has been reversed. Note: your function must actually reverse the items in the doubly-linked list, not just print them out in reverse order! Note: we won't use the copy constructor in this assignment, and as such you aren't required to update the copy constructor to work with a doubly-linked list. This is what I have so far but its not working! template void LinkedList::reverse() { Node*curPtr,*prev,*next; curPtr= NULL; prev= NULL; while(curPtr!=NULL) { next=curPtr->getNext(); curPtr->setNext(prev); curPtr->setPrev(next); prev=curPtr; curPtr=next; } tailPtr=headPtr; headPtr=prev; }
Add a new public member function to the LinkedList class named reverse() which reverses the items in the list. You must take advantage of the doubly-linked list structure to do this efficiently as discussed in the videos/pdfs, i.e. swap each node’s prev/next pointers, and finally swap headPtr/tailPtr. Demonstrate your function works by creating a sample list of a few entries in main(), printing out the contents of the list, reversing the list, and then printing out the contents of the list again to show that the list has been reversed.
Note: your function must actually reverse the items in the doubly-linked list, not just print them out in reverse order!
Note: we won't use the copy constructor in this assignment, and as such you aren't required to update the copy constructor to work with a doubly-linked list.
This is what I have so far but its not working!
template<class ItemType>
void LinkedList<ItemType>::reverse()
{
Node<ItemType>*curPtr,*prev,*next;
curPtr= NULL;
prev= NULL;
while(curPtr!=NULL)
{
next=curPtr->getNext();
curPtr->setNext(prev);
curPtr->setPrev(next);
prev=curPtr;
curPtr=next;
}
tailPtr=headPtr;
headPtr=prev;
}

Trending now
This is a popular solution!
Step by step
Solved in 2 steps









