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

QUESTION: If you look at the code, you\'ll see that we keep two lists, one to pr

ID: 3778423 • Letter: Q

Question

QUESTION: If you look at the code, you'll see that we keep two lists, one to print the text in forward order, and one to print the text in reverse order. The forward one is using our empty add() function, so nothing gets put into it. You'll fix that, as well as modifiying insert() and erase(). The reverse on works by inserting always at the head of the list, like a Stack.Functions insert(), erase(), and add() must be modified so that if the last node of the list changes, List::last must change, as well. YOU JUST HAVE TO COMPLETE THE LIST.CPP FILE SO THAT IT PRINTS THE OUTPUT IN THE FORWARD ORDER. FINISH THE CODE ONLY IN LIST.CPP FILE AND OTHER FILES ARE JUST FOR REFERENCE. MORE INFORMATION AND HINTS ARE GIVEN IN THE COMMENTS IN LIST.CPP FILE.

HINT: AFTER the existing code runs, there's an easy way to know if you just changed the last node in the list; if you did, it's time to update the "last" pointer. And remember that 'add'-ing the first item is a special case. We could make a Linked List behave a lot like a Queue if it were really efficient to add a node at the end of the list, that is, without having to chase links from the head to the tail. So we will add another pointer to our List class that will always point at the last item in the list. Read the existing code carefully and understand how it works. The "add" function is now empty. If you run the file you should get an output that starts out like this:

?CURRENT LIST.CPP:

CURRENT OUTPUT:

MAIN.CPP FILE:

LIST.H FILE:

include list include

Explanation / Answer

PROGRAM CODE:

/*
* List.cpp
*
* Created on: 29-Nov-2016
* Author: kasturi
*/

#include "List.h"

#include <string>
#include <ostream>
#include <iostream>

using namespace std;

void List::insert(ElemetType item, int position)
{
   Node * predptr = first;
   for(int i=1; i< position && predptr && predptr->next; i++)
   {
       predptr = predptr->next;
   }

   Node * newptr = new Node;
   newptr->payload = item;

   if(!predptr)
   {
       newptr->next = 0;
       first = newptr;
   }
   else if(position == 0)
   {
       newptr->next = first;
       first = newptr;
   }
   else
   {
       newptr->next = predptr->next;
       predptr->next = newptr;
   }
}

void List::erase(int position)
{
   Node * predptr = first;
   for(int i=1; i<position-1 && predptr->next; i++)
   {
       predptr = predptr->next;
   }
   if(!predptr)
   {

   }
   else if(position == 1)
   {
       first = predptr->next;
       delete predptr;
   }
   else
   {
       Node *ptr = predptr->next;
       predptr->next = ptr->next;
       delete ptr;
   }
}

void List::display(ostream& ostr)
{
   Node * ptr = last; //changed the display function to print the first
   while(ptr)
   {
       ostr<<ptr->payload<<endl;
       ptr = ptr->next;
   }
}

List::List()
{
   first = 0;
   last = 0;
}

List::~List()
{
   while(first)
   {
       Node *nxt = first->next;
       delete first;
       first = nxt;
   }
}

void List::add(ElemetType item)
{
   //added code to add values into the last node
   Node * predptr = last;
   Node *newptr = new Node;
   newptr->payload= item;
   while( predptr && predptr->next)
   {
       predptr = predptr->next;
   }
   if(!predptr)
   {
       newptr->next = 0;
       last = newptr;
   }
   else
   {
       newptr->next = predptr->next;
       predptr->next = newptr;
   }
}

OUTPUT:

Forward:

The

quick

brown

fox

jumped

over

the

back

of

the

lazy

dog.

Reversed: