Question

What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

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.

  1. Write the (single line) method declaration/specification.

  1. 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;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// 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:

Add a comment
Know the answer?
Add Answer to:
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    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...

    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...

    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...

    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;...

  • Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {}...

    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...

    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...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    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...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT