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

use Visual Studio. Complete the three functions, removeSecond, firstToThird, and

ID: 3833783 • Letter: U

Question

use Visual Studio.


Complete the three functions, removeSecond, firstToThird, and lastToFirst.

#include <iostream>

using namespace std;


// A very simple minimal Node
class Node
{
public:
double _entry;
Node * _nextNode;
};


// Construct a nullptr terminated linear linked list of size
//   Nodes. The entries are 1.1, 2.1, 3.1, . . .
// Return a pointer to the first Node of the linked list.
Node *
createList(size_t size);


// Print, to outStream, the entries in the linked list
//   targeted by ptr.
void
printList(Node * ptr, ostream & outStream);


// pre: the linked list targeted by aList has at least two
//         Nodes
// post: the second Node in the linked list targeted by aList
//         has been removed and returned to the Memory Pool
void
removeSecond(Node * aList);


// pre: the linked list targeted by aList has at least three
//         Nodes
// post: the first Node in the linked list targeted by aList
//         has been moved to the third position in the list
void
firstToThird(Node * & aList);


// pre: the linked list targeted by aList has at least two
//         Nodes
// post: the last Node in the linked list targeted by aList
//         has been moved to the first position in the list
void
lastToFirst(Node * & aList);


//==================================
//
//==================================
int
main()
{
Node * aList = createList(5);
cout << "list is                ";
printList(aList, cout);
cout << endl;

removeSecond(aList);
cout << "remove second list is ";
printList(aList, cout);
cout << endl;

firstToThird(aList);
cout << "first to third list is ";
printList(aList, cout);
cout << endl;

lastToFirst(aList);
cout << "last to first list is ";
printList(aList, cout);
cout << endl;
}

// Construct a nullptr terminated linear linked list of size
//   Nodes. The entries are 1.1, 2.1, 3.1, . . .
// Return a pointer to the first Node of the linked list.
Node *
createList(size_t size)
{
if (size == 0)
{
  return nullptr;
}

Node * first = new Node;
Node * ptr = first;
ptr->_entry = 1.1;
for (size_t i = 2; i <= size; ++i)
{
  ptr->_nextNode = new Node;
  ptr = ptr->_nextNode;
  ptr->_entry = i + 0.1;
}
ptr->_nextNode = nullptr;
return first;
}

// Print, to outStream, the entries in the linked list
//   targeted by ptr.
void
printList(Node * ptr, ostream & outStream)
{
outStream << '[';
if (ptr != nullptr)
{
  outStream << ' ' << ptr->_entry;
  ptr = ptr->_nextNode;
}
while (ptr != nullptr)
{
  outStream << ", " << ptr->_entry;
  ptr = ptr->_nextNode;
}
outStream << " ]";
}


// pre: the linked list targeted by aList has at least two
//         Nodes
// post: the second Node in the linked list targeted by aList
//         has been removed and returned to the Memory Pool
void
removeSecond(Node * aList)
{}


// pre: the linked list targeted by aList has at least three
//         Nodes
// post: the first Node in the linked list targeted by aList
//         has been moved to the third position in the list
void
firstToThird(Node * & aList)
{}


// pre: the linked list targeted by aList has at least two
//         Nodes
// post: the last Node in the linked list targeted by aList
//         has been moved to the first position in the list
void
lastToFirst(Node * & aList)
{}

Explanation / Answer

Please find my implementation.


// pre: the linked list targeted by aList has at least two
// Nodes
// post: the second Node in the linked list targeted by aList
// has been removed and returned to the Memory Pool
void removeSecond(Node * aList)
{
   Node* t = aList->_nextNode;
   aList->_nextNode = r->_nextNode;
   delete t;
}

// pre: the linked list targeted by aList has at least three
// Nodes
// post: the first Node in the linked list targeted by aList
// has been moved to the third position in the list
void firstToThird(Node * & aList)
{
   Node* t = aList;
   aList = aList->_nextNode; // alist is not pointing to second
   t->_nextNode = aList->_nextNode;// now t points to third node in original list
   aList->_nextNode = t; // now t moved to third position
}

// pre: the linked list targeted by aList has at least two
// Nodes
// post: the last Node in the linked list targeted by aList
// has been moved to the first position in the list
void lastToFirst(Node * & aList)
{
   Node *t = aList;

   // make t points to second last
   while(t->_nextNode->_nextNode != NULL){
       t = t->_nextNode;
   }

   // last node
   Node *last = t->_nextNode;

   // make second last node point to NULL
   t->_nextNode= NULL;

   // point last to first
   last->_nextNode = aList;

   aList = last; // last becomes first
}