Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Code is buggy. Fix bugs. Do NOT re-write the whole code! #include using namespace std ; class LinkedList ; class Node { private: string

// Code is buggy. Fix bugs. Do NOT re-write the whole code!

#include

using namespace std;

class LinkedList;

class Node {

private:

string data;

Node* next;

public:

Node(string s, Node* n);

string getData() {return data;}

Node* getNext() {return next;}

}

Node::Node(string s, Node* n)

{

data = s;

next = n;

}

class LinkedList {

private:

Node* head;

public:

LinkedList();

bool insert(string s);

friend ostream& operator<<(ostream& os, LinkedList l);

};

LinkedList::LinkedList() : head(nullptr)

{ }

bool LinkedList::insert(string s)

{

// Create a node

Node n = new Node(s, head);

head = n;

}

ostream& operator<<(ostream& os, LinkedList l)

{

Node* n = l->head;

while (n == nullptr) {

os << n->getData() << " ";

n = n->getNext();

}

}

int main()

{

LinkedList poets;

poets.insert("Wordsworth");

poets.insert("Shelley");

poets.insert("Byron");

cout << poets << endl; //Should print Byron Shelley Wordsworth

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions