Question: C++ Please fix my code. It does not match the output and I am not sure how to fix it. CODE #include #include using namespace

C++

Please fix my code. It does not match the output and I am not sure how to fix it.

 C++ Please fix my code. It does not match the outputand I am not sure how to fix it. CODE #include #include

CODE

#include

#include

using namespace std;

typedef struct node{

string data;

struct node *next;

}Node;

class sList{

Node *head;

int size_;

public:

sList(){

head = NULL;

size_ = 0;

}

void append(string data){

Node *temp = new Node;

temp->data = data;

temp->next = NULL;

if(size_ == 0 && head == NULL){

head = temp;

size_++;

return;

}

Node *tmp = head;

while(tmp->next != NULL){

tmp = tmp->next;

}

tmp->next = temp;

size_++;

}

void remove(string data){

if(head == NULL){

cout

}

Node *temp = head;

Node *prev = NULL;

while(temp != NULL){

if(temp->data == data){

if(prev == NULL){

head = temp->next;

delete temp;

size_--;

return;

}

prev->next = temp->next;

delete temp;

size_--;

return;

}

prev = temp;

temp = temp->next;

}

cout

}

void print(){

if(head == NULL){

cout

return;

}

Node *temp = head;

while(temp != NULL){

coutdata;

if(temp->next != NULL){

cout";

}

temp = temp->next;

}

}

};

sList* readFile(ifstream &ifs){

cout

sList *l = new sList;

string data;

ifs>>data;

while(!ifs.eof()){

l->append(data);

ifs>>data;

}

return l;

}

int main(){

ifstream ifs;

ifs.open("data2.txt");

sList *l;

if(ifs.is_open()){

l = readFile(ifs);

}else{

cout

}

cout

l->print();

cout

string choice = "y";

string d;

while(choice[0] != 'n'){

cout

cin>>d;

l->remove(d);

cout

l->print();

cout

cin>>choice;

}

return 0;

}

(2) Based on Problem P12.12 Turn the linked List of strings implementation into a singly-linked list sList: Drop the previous pointer of the nodes and the previous member function of the iterator. Reimplement the other member functions so that they have the same effect as before. Hint: In order to remove an element in constant time, iterators should store the predecessor of the current node. You can modify the List class provided in the textbook or see modify the implementation of this class posted on ccle (see sample code for lectures 17-20). . Write a program that reads the data from data2.txt into the single-linked list sList. Display the elements of the list and then prompt the user to enter an element for removal. t a loop in which the removal action is until the user requests to quit. Submit the solution as hmw.6.2.cpp. Sample input-output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!