// Code is buggy. Fix bugs. Do NOT re-write the whole code!
#include <iostream>
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
}
#include <iostream>
using namespace std;
//class LinkedList;//removed
class Node
{
public:
string data;
Node* next;
public:
Node(string s, Node* n);
string getData() {return data;}
Node* getNext() {return next;}
};//semicolon added here
Node::Node(string s, Node* n)
{
data = s;
next = n;
}
class LinkedList {
public://modified
Node* head;
public:
LinkedList();
bool insert(string s);
friend ostream& operator<<(ostream& os, LinkedList
*l);
};
LinkedList::LinkedList() //: head(nullptr)
{
head=NULL;
}
bool LinkedList::insert(string s)
{
// Create a node
Node *n = new Node(s, head);//modified
head = n;
}
ostream& operator<<(ostream& os, LinkedList
*l)//modified
{
Node* n = l->head;
while (n != NULL) //modified
{
os << n->getData() << " ";
n = n->getNext();
}
}
int main()
{
LinkedList *poets= new LinkedList();
poets->insert("Wordsworth");
poets->insert("Shelley");
poets->insert("Byron");
cout << poets<<endl; //Should print Byron Shelley
Wordsworth
return 0;
}
output:
Byron Shelley Wordsworth
Process exited with return value 3221225477
Press any key to continue . . .
// Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private: Node *head; // You may add whatever private data members or private member functions you want to this class. void printReverseRecursiveHelper(Node *temp) const; public: // default constructor LinkedList() : head(nullptr) { } // copy constructor LinkedList(const LinkedList& rhs); // Destroys all the dynamically allocated memory //...
For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
Below is the given code of implementation:
#include <string>
#include <iostream>
#include <list>
#include <cassert>
using namespace std;
class List;
class Iterator;
class Node
{
public:
/*
Constructs a node with a given data value.
@param s the data to store in this node
*/
Node(string s);
/* Destructor */
~Node() {}
private:
string data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;
};
class List
{
public:
/**
Constructs an empty list.
*/
List();
/* Destructor. Deletes...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...
// Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12 #define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'" namespace Errors { struct InvalidName { }; } class Name { public: Name ( string first_name = "", string last_name = ""); string first() const; string last() const; void set_first( string fname ); void set_last( string lname); friend ostream& operator<< ( ostream & os, Name name ); friend bool operator== (Name name1, Name...
BELOW IS THE CODE I ALREADY HAVE
Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...