PLEASE READ THE INSTRUCTION CAREFULLY YOU JUST HAVE TO WRITE

PLEASE READ THE INSTRUCTION CAREFULLY. YOU JUST HAVE TO WRITE THE REMAINING CODE ACCORDING TO GIVEN DIRECTIONS.

If you look in list.h, you\'ll see a pointer called \"last\". This pointer is intended to always point to the last node in the list. If it does, then you can fill in the \"add\" function so it uses that pointer to add a new node to the list very efficiently.

But more than the \"add\" function, you have to fix up \"insert\" and \"delete\" so that if the function caused the last node to change, you update \"last\" accordingly. Hint: you know whether a particular node is last by checking its \"next\" pointer--only the last pointer has a zero there. Don\'t touch any of the existing insert or delete logic--just add an \"if\" statement at the end to check the correct \"next\" pointer and update \"last\".

?ORIGINAL CODE:

?LIST.H FILE:

CURRENT OUTPUT:

Demonstrate usage of lists #include kiostream> #include

Solution

1)list.h file

#ifndef LIST_H
#define LIST_H


#include<iostream>
#include<string>
#include<fstream>
typedef std::string ElementType;

typedef struct _node
{
ElementType payload;
struct _node *next;
}Node;

class List
{
private:
Node* first;
Node* last;

public:
List();
~List();

void insert(ElementType item, int position);
void add(ElementType item);
void erase(int position);
void display(std::ostream& ostr);
};
#endif // LIST_H

2) list.cpp file

# include \"list.h\"

using namespace std;
List::List()
{
first = NULL;
last = NULL;
}
List::~List()
{
Node *travel = first->next;
while(first != last)
{
first->next = travel->next;
travel->next = NULL;
delete travel;
travel = first->next;
}
}
void List::insert(ElementType item, int position)
{
Node *new_node = new Node;
new_node->payload = item;
new_node->next = NULL;

if(position == 0)
{
new_node->next = first->next;
first = new_node;
return;
}
Node* travel = first;
for(int i=0; i < position; i++)
{
travel = travel->next;
}
new_node->next = travel->next;
travel->next = new_node;
}
void List::add(ElementType item)
{
Node *new_node = new Node;
new_node->payload = item;
new_node->next = NULL;

last->next = new_node;
last = new_node;
}
void List::erase(int position)
{
Node *temp = first;
if(position == 0)
{
first = temp->next;
delete temp;
}
else
{
for(int i = 0; i < position-1; i++)
{
temp = temp->next;
}
temp->next = temp->next->next;
delete temp->next;
}

}
void List::display(std::ostream& ostr)
{
Node* travel = first;
while(travel != last)
{
ostr<<travel->payload<<endl;
travel = travel->next;
}
ostr<<travel->payload<<endl;
}

3) main.cpp file


#include \"list.h\"
using namespace std;
int main(int argc, char* argv[])
{
if(argc != 2)
{
cerr << \"Usage:demo listdata.txt\" << endl;
return 1;
}

ifstream fp(argv[1]);
if(!fp)
{
cerr<<\"Cannot read \"<<argv[1]<<endl;
return 2;
}

List forwardList;
List backList;

string str;

fp >>str;

while(!fp.eof())
{
backList.insert(str,0);
forwardList.add(str);
fp>>str;
}
cout<<\"Forward:\"<<endl;
forwardList.display(cout);

cout<<\"Reversed: \"<<endl;
backList.display(cout);
return 0;
}

PLEASE READ THE INSTRUCTION CAREFULLY. YOU JUST HAVE TO WRITE THE REMAINING CODE ACCORDING TO GIVEN DIRECTIONS. If you look in list.h, you\'ll see a pointer cal
PLEASE READ THE INSTRUCTION CAREFULLY. YOU JUST HAVE TO WRITE THE REMAINING CODE ACCORDING TO GIVEN DIRECTIONS. If you look in list.h, you\'ll see a pointer cal
PLEASE READ THE INSTRUCTION CAREFULLY. YOU JUST HAVE TO WRITE THE REMAINING CODE ACCORDING TO GIVEN DIRECTIONS. If you look in list.h, you\'ll see a pointer cal

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site