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

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

ID: 3729073 • Letter: C

Question

C++

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

CODE

#include<iostream>

#include<fstream>

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<<" List is Empty";

}

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<<" There is no such Item";

}

  

void print(){

if(head == NULL){

cout<<" List is empty";

return;

}

Node *temp = head;

while(temp != NULL){

cout<<temp->data;

if(temp->next != NULL){

cout<<"->";

}

temp = temp->next;

}

}

};

sList* readFile(ifstream &ifs){

cout<<" Iterating through the list in data2.txt ";

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<<" File opening error check input file";

}

cout<<endl;

l->print();

cout<<" Testing Removal";

string choice = "y";

string d;

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

cout<<" Enter element to remove : ";

cin>>d;

l->remove(d);

cout<<" Updated List : ";

l->print();

cout<<" Continue (y/n) : ";

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:

Explanation / Answer

UPDATED PROGRAM

#include<iostream>

#include<fstream>

using namespace std;

// Create structure Node with string element

typedef struct node{

string data;

struct node *next;

}Node;

// Create class sList

class sList{

Node *head;

int size_;

  

public:

sList(){ // constructor with null values

head = NULL;

size_ = 0;

}

// implement append() function for adding all strings

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_++;

}

// Create and implement remove() method and return integer value

int remove(string data){

if(head == NULL){

cout<<" List is Empty";

}

Node *temp = head;

Node *prev = NULL;

int flag=0;

while(temp != NULL){

if(temp->data == data){ flag=1; // the string is equal then flag=1

if(prev == NULL){

head = temp->next;

delete temp;

size_--;

return 1; // return 1

}

prev->next = temp->next;

delete temp;

size_--;

return 1; // return 1

}

else flag= 0; // the string are not equal then flag=0

  

prev = temp;

temp = temp->next;

}

  

return flag;

}

  

// Create and implemnt print() method to print in linked list format

void print(){

if(head == NULL){

cout<<" List is empty";

return;

}

Node *temp = head;

while(temp != NULL){

cout<<temp->data;

if(temp->next != NULL){

cout<<"->";

}

temp = temp->next;

}

}

};

// Create and implment readFile() the file read

sList* readFile(ifstream &ifs){

cout<<" Iterating through the list in data2.txt ";

sList *l = new sList;

string data;

//ifs>>data;

while(ifs>>data){ // read the strings "data" until null

l->append(data); // append all the strings

}

  

return l;

}

int main(){

ifstream ifs;

ifs.open("f:\data2.txt"); // read file from the directory (Note: I am using 'f' Direcotory)

sList *l; // create pointer object l

if(ifs.is_open()){

l = readFile(ifs);

}else{

cout<<" File opening error check input file";

}

cout<<endl;

l->print();

cout<<" Testing Removal.";

string choice = "y";

string d;

// create loop until choice is 'n'

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

  

cout<<" Enter the element to remove : ";

cin>>d;

  

int t= l->remove(d); // call method remove() and return integer variable t

if(t==1){ // check condition if the string is found then print updated list

  

cout<<" Updated List : ";

l->print();

}

else

cout<<" There is no such element in the list.";

cout<<" Continue (y/n)? "; // ask choice

cin>>choice;

  

}

  

return 0;

}

OUTPUT:

Iterating through the list in data2.txt

Hello->World->Again->Tomorrow->Iowa

Testing Removal.

Enter the element to remove : Hello

Updated List : World->Again->Tomorrow->Iowa

Continue (y/n)? y


Enter the element to remove : World

Updated List : Again->Tomorrow->Iowa

Continue (y/n)? y


Enter the element to remove : Monday

There is no such element in the list.

Continue (y/n)? y


Enter the element to remove : Iowa

Updated List : Again->Tomorrow

Continue (y/n)? n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote