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 screen output, by attaching it as a captured image (use CTRL+PrtSc) or printed out as a file.
#include <iostream>
#include "SinglyLinkedList.hpp"
using std::cout;
using std::endl;
int main() {
SinglyLinkedList<int> ds;
for (int i=10; i<20; i++)
ds.append(i);
Node<int> *ptr = ds.find(15);
if (ptr != nullptr)
cout << "Found: " << ptr->data << " matches " << 15 << endl;
else
cout << "15 not found" << endl;
ptr = ds.find(30);
if (ptr != nullptr)
cout << "Found: " << ptr->data << " matches " << 30 << endl;
else
cout << "30 not found" << endl;
return 0;
}
----
CODE for SinglyLinkedList:
| #pragma once | |
| #include <stdexcept> | |
| template<typename T> | |
| struct Node { | |
| T data; | |
| Node<T>* next; | |
| Node() = delete; // Intentionally no default constructor | |
| Node( const T & element ) : data( element ), next( nullptr ) {} | |
| }; | |
| template<typename T> | |
| class SinglyLinkedList { | |
| private: | |
| Node<T>* head; | |
| Node<T>* tail; | |
| public: | |
| // Constructors | |
| SinglyLinkedList(); | |
| SinglyLinkedList(const SinglyLinkedList&); | |
| SinglyLinkedList& operator=(const SinglyLinkedList&); // assignment operator | |
| ~SinglyLinkedList(); // destructor | |
| // Getters / Setters | |
| bool empty(); | |
| int size() = delete; // INTENTIONALLY NOT IMPLEMENTED !! | |
| void append( const T& ); | |
| void prepend( const T& ); | |
| void insertAfter( Node<T>*, const T& ); | |
| void removeAfter( Node<T>* ); | |
| void pop_front(); // remove element at front of list | |
| T& front(); // return list's front element | |
| T& back(); // return list's back element | |
| void clear(); | |
| }; | |
| template<typename T> | |
| SinglyLinkedList<T>::SinglyLinkedList() : head( nullptr ), tail( nullptr ) {} | |
| template<typename T> | |
| bool SinglyLinkedList<T>::empty() { | |
| return head == nullptr; | |
| } | |
| template<typename T> | |
| void SinglyLinkedList<T>::append( const T& newData ) { | |
| Node<T> * newNode = new Node<T>( newData ); // create new node | |
| if (head == nullptr) { // List empty | |
| head = newNode; | |
| tail = newNode; | |
| } | |
| else{ | |
| tail->next = newNode; | |
| tail = newNode; | |
| } | |
| } | |
| template<typename T> | |
| void SinglyLinkedList<T>::prepend( const T& newData ) { | |
| Node<T> * newNode = new Node<T>( newData ); // create new node | |
| if (head == nullptr) { // list empty | |
| head = newNode; | |
| tail = newNode; | |
| } | |
| else { | |
| newNode->next = head; | |
| head = newNode; | |
| } | |
| } | |
| template<typename T> | |
| void SinglyLinkedList<T>::insertAfter(Node<T>* curNode, const T& newData) { | |
| // Construct new node | |
| Node<T>* newNode = new Node<T>( newData ); | |
| if (head == nullptr) { // List empty | |
| head = newNode; | |
| tail = newNode; | |
| } else if (curNode == tail) { // Insert after tail | |
| tail->next = newNode; | |
| tail = newNode; | |
| } else { | |
| newNode->next = curNode->next; | |
| curNode->next = newNode; | |
| } | |
| } | |
| template<typename T> | |
| void SinglyLinkedList<T>::removeAfter(Node<T>* curNode) { | |
| if( empty() ) throw std::length_error( "empty list" ); | |
| // Special case, remove head | |
| if (curNode == nullptr && head != nullptr) { | |
| Node<T> *sucNode = head->next; | |
| head = sucNode; | |
| if (sucNode == nullptr) { // Removed last item | |
| tail = nullptr; | |
| } | |
| } | |
| else if (curNode->next != nullptr) { | |
| Node<T> *sucNode = curNode->next->next; | |
| curNode->next = sucNode; | |
| if (sucNode == nullptr) { // Removed tail | |
| tail = curNode; | |
| } | |
| } | |
| } | |
| template <typename T> | |
| void SinglyLinkedList<T>::pop_front() { | |
| removeAfter(nullptr); | |
| } | |
| template <typename T> | |
| T& SinglyLinkedList<T>::front() { | |
| if( empty() ) throw std::length_error( "empty list" ); | |
| return head->data; | |
| } | |
| template <typename T> | |
| T& SinglyLinkedList<T>::back() { | |
| if( empty() ) throw std::length_error( "empty list" ); | |
| return tail->data; | |
| } | |
| template<typename T> | |
| void SinglyLinkedList<T>::clear() { | |
| while( !empty() ) | |
| pop_front(); | |
| } | |
| template<typename T> | |
| SinglyLinkedList<T>::~SinglyLinkedList() { | |
| clear(); | |
| } | |
| template <typename T> | |
| SinglyLinkedList<T>::SinglyLinkedList( const SinglyLinkedList<T> & original ) : SinglyLinkedList() { | |
| // Walk the original list adding copies of the elements to this list maintaining order | |
| for( Node<T> * position = original.head; position != nullptr; position = position->next ) { | |
| append( position->data ); | |
| } | |
| } | |
| template <typename T> | |
| SinglyLinkedList<T> & SinglyLinkedList<T>::operator=( const SinglyLinkedList<T> & rhs ) { | |
| if( this != &rhs ) // avoid self assignment | |
| { | |
| // Release the contents of this list first | |
| clear(); // An optimization might be possible by reusing already allocated nodes | |
| // Walk the right hand side list adding copies of the elements to this list maintaining order | |
| for( Node<T> * position = rhs.head; position != nullptr; position = position->next ) { | |
| append( position->data ); | |
| } | |
| } | |
| return *this; | |
| } |
// SinglyLinkedList.hpp
#pragma once
#include <stdexcept>
template<typename T>
struct Node {
T data;
Node<T>* next;
Node() = delete; // Intentionally no default constructor
Node( const T & element ) : data( element ), next( nullptr ) {}
};
template<typename T>
class SinglyLinkedList {
private:
Node<T>* head;
Node<T>* tail;
public:
// Constructors
SinglyLinkedList();
SinglyLinkedList(const SinglyLinkedList&);
SinglyLinkedList& operator=(const SinglyLinkedList&); // assignment operator
~SinglyLinkedList(); // destructor
// Getters / Setters
bool empty();
int size() = delete; // INTENTIONALLY NOT IMPLEMENTED !!
void append( const T& );
void prepend( const T& );
void insertAfter( Node<T>*, const T& );
void removeAfter( Node<T>* );
void pop_front(); // remove element at front of list
T& front(); // return list's front element
T& back(); // return list's back element
void clear();
Node<T> *find(T data);// return the node containing the data, else nullptr if data not present
};
template<typename T>
SinglyLinkedList<T>::SinglyLinkedList() : head( nullptr ), tail( nullptr ) {}
template<typename T>
bool SinglyLinkedList<T>::empty() {
return head == nullptr;
}
template<typename T>
void SinglyLinkedList<T>::append( const T& newData ) {
Node<T> * newNode = new Node<T>( newData ); // create new node
if (head == nullptr) { // List empty
head = newNode;
tail = newNode;
}
else{
tail->next = newNode;
tail = newNode;
}
}
template<typename T>
void SinglyLinkedList<T>::prepend( const T& newData ) {
Node<T> * newNode = new Node<T>( newData ); // create new node
if (head == nullptr) { // list empty
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}
template<typename T>
void SinglyLinkedList<T>::insertAfter(Node<T>* curNode, const T& newData) {
// Construct new node
Node<T>* newNode = new Node<T>( newData );
if (head == nullptr) { // List empty
head = newNode;
tail = newNode;
} else if (curNode == tail) { // Insert after tail
tail->next = newNode;
tail = newNode;
} else {
newNode->next = curNode->next;
curNode->next = newNode;
}
}
template<typename T>
void SinglyLinkedList<T>::removeAfter(Node<T>* curNode) {
if( empty() ) throw std::length_error( "empty list" );
// Special case, remove head
if (curNode == nullptr && head != nullptr) {
Node<T> *sucNode = head->next;
head = sucNode;
if (sucNode == nullptr) { // Removed last item
tail = nullptr;
}
}
else if (curNode->next != nullptr) {
Node<T> *sucNode = curNode->next->next;
curNode->next = sucNode;
if (sucNode == nullptr) { // Removed tail
tail = curNode;
}
}
}
template <typename T>
void SinglyLinkedList<T>::pop_front() {
removeAfter(nullptr);
}
template <typename T>
T& SinglyLinkedList<T>::front() {
if( empty() ) throw std::length_error( "empty list" );
return head->data;
}
template <typename T>
T& SinglyLinkedList<T>::back() {
if( empty() ) throw std::length_error( "empty list" );
return tail->data;
}
template<typename T>
void SinglyLinkedList<T>::clear() {
while( !empty() )
pop_front();
}
template<typename T>
SinglyLinkedList<T>::~SinglyLinkedList() {
clear();
}
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList( const SinglyLinkedList<T> & original ) : SinglyLinkedList() {
// Walk the original list adding copies of the elements to this list maintaining order
for( Node<T> * position = original.head; position != nullptr; position = position->next ) {
append( position->data );
}
}
template <typename T>
SinglyLinkedList<T> & SinglyLinkedList<T>::operator=( const SinglyLinkedList<T> & rhs ) {
if( this != &rhs ) // avoid self assignment
{
// Release the contents of this list first
clear(); // An optimization might be possible by reusing already allocated nodes
// Walk the right hand side list adding copies of the elements to this list maintaining order
for( Node<T> * position = rhs.head; position != nullptr; position = position->next ) {
append( position->data );
}
}
return *this;
}
template <typename T>
Node<T>*::SinglyLinkedList<T>:: find(T data)
{
Node<T> *curr = head;
while(curr != nullptr)
{
if(curr->data == data)
return curr;
curr = curr->next;
}
return nullptr;
}
//end of SinglyLinkedList.hpp
#include <iostream>
#include "SinglyLinkedList.hpp"
using std::cout;
using std::endl;
int main() {
SinglyLinkedList<int> ds;
for (int i=10; i<20; i++)
ds.append(i);
Node<int> *ptr = ds.find(15);
if (ptr != nullptr)
cout << "Found: " << ptr->data << " matches " << 15 << endl;
else
cout << "15 not found" << endl;
ptr = ds.find(30);
if (ptr != nullptr)
cout << "Found: " << ptr->data << " matches " << 30 << endl;
else
cout << "30 not found" << endl;
return 0;
}
Output:

What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...
In this assignment, you will implement a sort method on
singly-linked and doubly-linked lists.
Implement the following sort member function on a singly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
Implement the following sort member function on a doubly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
The sort(…) methods take as a parameter a comparator function,
having a default assignment of defaultCompare, a
static function defined as follows:
template <typename T>
static bool defaultCompare(const...
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;...
The program I wrote has a few memory leaks. My assignment is to
just fix the memory leaks. Here's the code:
bool RemoveTail()
{
if (tail == nullptr)
return false;
Node *ptr = tail;
if(tail->prev != nullptr)
{
tail = tail->prev;
tail->next = nullptr;
}
else
{
tail = nullptr;
head = nullptr;
}
delete ptr;
ptr = nullptr;
length--;
return true;
}
bool RemoveAt(const unsigned int index)
{
if(index >= length || index < 0)
return false;
unsigned int...
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*...
Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {} void insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } void preOrderTraversal() const { preOrderHelper( rootPtr ); } void inOrderTraversal() const { inOrderHelper( rootPtr ); } private: TreeNode< NODETYPE > *rootPtr; void insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { if ( *ptr == nullptr ) * ptr = new...
can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...