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.
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started