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; the output should print 'Test List' and then (on another line) 'My List'.
We will eventually construct a function that outputs the node values in your linked list; however, first we should overload the operator<< for your Node struct (this way makes things down the road 'easier').
Add the following declaration directly after your Node
struct declaration (i.e., outside of it):
std::ostream& operator<<(std::ostream& os, const
Node& n);
and then provide the following definition in your Node.cpp
file:
std::ostream& operator<<(std::ostream& os,
const Node& n) {
os << n.value;
return os;
}
Note: You may need to #include<iostream> in Node.cpp.
In the public access specifier in your LinkedList header
file, add
void print(char, std::ostream& os =
std::cout);
If this was an exam, could you explain the different parts comprising the declaration provided above? If not, take a moment to think about it; things will be clearer once you look at the definition (below).
Then, add the following definition for print to your LinkedList cpp file:
void LinkedList::print(char c, ostream& os) {
Node* cur = head;
while (cur != nullptr) {
os << *cur;
if (cur != tail)
os << c;
if (cur == tail)
os << endl;
cur = cur->next;
}
}
Let's test our print function now by adding the following lines to Main.cpp
cout << endl << "ll.print('\\t');" <<
endl;
ll.print('\t');
cout << endl << "ll.print('\\n');" << endl;
ll.print('\n');
LinkedList.cpp --------
#include <iostream>
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList() : head(nullptr), tail(nullptr) {}
LinkedList::LinkedList(string name) : head(nullptr), tail(nullptr), name(name) {}
void LinkedList::make_test_list() {
/* again, not the real way to create a list, but it will give us
something to
use for testing parts until we learn how.
*/
Node* newNode = new Node(7);
head = newNode;
newNode = new Node(3);
head->next = newNode;
newNode = new Node(12);
head->next->next = newNode;
tail = newNode;
}
string get_name() const {
return name;
}
void set_name(std::string){
name = name;
}
void LinkedList::print(char c, ostream& os) {
Node* cur = head;
while (cur != nullptr) {
os << *cur;
if (cur != tail)
os << c;
if (cur == tail)
os << endl;
cur = cur->next;
}
}
LinkedList.h----------
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <string>
#include "Node.h"
class LinkedList {
Node* head;
Node* tail;
std::string name;
public:
LinkedList();
LinkedList(std::string);
/* not the real way to create a list, but it will give us
something to
use for testing parts until we learn how.
*/
void make_test_list();
std::string get_name() const;
void set_name(std::string );
void print(char, std::ostream& os =
std::cout);
};
#endif
Node.cpp----------
#include "Node.h"
#include <iostream>
std::ostream& operator<<(std::ostream& os, const
Node& n) {
os << n.value;
return os;
}
Node.h-----------
#ifndef NODE_H
#define NODE_H
#include <iostream>
struct Node
{
int value;
Node* next;
Node(int value) : value(value), next(nullptr) {}
Node() : value(0), next(nullptr) {}
};
std::ostream& operator<<(std::ostream& os, const Node& n);
#endif
Main.cpp-------------
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main() {
cout << endl << "Linked list print (traversal) lab"
<< endl;
LinkedList ll("Test List");
ll.make_test_list();
// fills with { 7, 3, 12 }
cout << ll.get_name() << endl;
ll.make_test_list();
ll.set_name("My List");
cout << ll.get_name() << endl;
cout << endl << "ll.print('\\t');" <<
endl;
ll.print('\t');
cout << endl << "ll.print('\\n');" << endl;
ll.print('\n');
}
Here i am providing the answer. Hope it helps. please give me a like. it helps me a lot.
#include <iostream>
#include <string>
using namespace std;
#ifndef CDOG_H
#define CDOG_H
class CDog
{
private:
string name();
int weight();
bool rabid();
public:
CDog();
~CDog();
// Attributes
void setRabid(bool x);
bool getRabid();
void setName(string x);
string getName();
void setWeight(int x);
int getWeight();
// Behaviours
void growl();
void eat();
};
#endif // CDOG_H
#include <iostream>
#include <string>
#include "CDog.h"
using namespace std;
CDog::CDog(int x, string x)
{
rabid = false;
weight = x;
name = x;
}
CDog::~CDog()
{
}
void CDog::setRabid(bool myboolean)
{
name = myboolean;
}
bool CDog::getRabid();
{
return(rabid);
}
void CDog::setWeight(int y)
{
weight = y;
}
int CDog::getWeight();
{
return(weight);
}
void CDog::setName(string y)
{
name = y;
}
string CDog::getName();
{
return(name);
}
// Behaviours
void CDog::eat()
{
cout << name << "is now eating" <<
weight++ << endl;
}
void CDog::growl()
{
cout << "Grrr" << endl;
}
Thank you. please upvote.
For the LinkedList class, create a getter and setter for the private member 'name', constructing your...
Fill the code in list.cpp app.cpp: #include "list.hpp" int main() { LinkedList aList; bool success = false; aList.evensFrontOddsEnd(3); aList.evensFrontOddsEnd(10); aList.evensFrontOddsEnd(6); aList.evensFrontOddsEnd(9); aList.evensFrontOddsEnd(17); aList.evensFrontOddsEnd(12); aList.printForward(); // output should be: 12 6 10 3 9 17 aList.printBackward(); // output should be: 17 9 3 10 6 12 success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl; success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode { public: T data; ListNode<T>* next; ListNode(T data) { this->data = data;...
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,...
// Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...
// 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) {...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
**HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...