Question

In: Computer Science

C++ ONLY! Implement the find function for the List class. It takes a string as an...

C++ ONLY!

Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator.

#include <iostream>
#include <cstddef>
#include <string>

using Item = std::string;

class List {
private:

class ListNode {
public:
Item item;
ListNode * next;
ListNode(Item i, ListNode *n=nullptr) {
item = i;
next = n;
}
};
  
ListNode * head;
ListNode * tail;
  
public:
class iterator {
ListNode *node;
public:
iterator(ListNode *n = nullptr) {
node = n;
}
Item& getItem() { return node->item; }
void next() { node = node->next; }
bool end() { return node==nullptr; }

friend class List;
};

public:
List() {
// list is empty
head = nullptr;
tail = nullptr;
}

bool empty() {
return head==nullptr;
}
  
// Only declared, here, implemented
// in List.cpp
void append(Item a);
bool remove (Item &copy);

void insertAfter(iterator, Item);
void removeAfter(iterator, Item&);

iterator begin() {
return iterator(head);
}

iterator find(Item);
};


void List::append(Item a) {
ListNode *node = new ListNode(a);
if ( head == nullptr ) {
// empty list
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}

bool List::remove(Item &copy)
{
if (!empty()) {
copy = head->item;
ListNode *tmp = head->next;
delete head;
head = tmp;
if (head==nullptr)
tail = nullptr;
return true;
}
return false;
}

void List::insertAfter(iterator it, Item item)
{
if (it.node == nullptr)
{
// insert at head
ListNode *tmp = new ListNode(item,head);
// if list is empty, set tail to new node
if (tail==nullptr) {
   tail = tmp;
}
// set head to new node
head = tmp;
}
else
{
ListNode *tmp = new ListNode(item,it.node->next);  
it.node->next = tmp;
// could be a new tail, if so update tail
if (tail==it.node) {
   tail = tmp;
}   
}
}

void List::removeAfter(iterator it, Item& item)
{
// emtpy list or at tail, just return
if (it.node == tail) return;
  
if (it.node == nullptr)
{
ListNode * tmp = head;
head = head->next;
delete tmp;
if (head==nullptr)
   tail = nullptr;
}
else
{
ListNode *tmp = it.node->next;
it.node->next = tmp->next;
delete tmp;
// could be that it.node is the new nullptr
if (it.node->next == nullptr)
   tail = it.node;
}
}

List::iterator List::find(Item item)
{
//YOUR CODE HERE

return iterator();
}

int main()
{
List l;
l.append("one");
l.append("two");
l.append("three");
l.append("four");

auto it = l.find("one");
if (!it.end())
std::cout << "Should be one: " << it.getItem() << std::endl;
else
std::cout << "Iterator should not have been null." << std::endl;
  
auto no_it = l.find("zero");
if (no_it.end())
std::cout << "As expected, zero not found." << std::endl;
else
std::cout << "Oops! should not have found zero." << std::endl;
  
return 0;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <cstddef>

#include <string>

using Item = std::string;

class List

{

private:

    class ListNode

    {

    public:

        Item item;

        ListNode *next;

        ListNode(Item i, ListNode *n = nullptr)

        {

            item = i;

            next = n;

        }

    };

    ListNode *head;

    ListNode *tail;

public:

    class iterator

    {

        ListNode *node;

    public:

        iterator(ListNode *n = nullptr)

        {

            node = n;

        }

        Item &getItem() { return node->item; }

        void next() { node = node->next; }

        bool end() { return node == nullptr; }

        friend class List;

    };

public:

    List()

    {

        // list is empty

        head = nullptr;

        tail = nullptr;

    }

    bool empty()

    {

        return head == nullptr;

    }

    // Only declared, here, implemented

    // in List.cpp

    void append(Item a);

    bool remove(Item &copy);

    void insertAfter(iterator, Item);

    void removeAfter(iterator, Item &);

    iterator begin()

    {

        return iterator(head);

    }

    iterator find(Item);

};

void List::append(Item a)

{

    ListNode *node = new ListNode(a);

    if (head == nullptr)

    {

        // empty list

        head = node;

        tail = node;

    }

    else

    {

        tail->next = node;

        tail = node;

    }

}

bool List::remove(Item &copy)

{

    if (!empty())

    {

        copy = head->item;

        ListNode *tmp = head->next;

        delete head;

        head = tmp;

        if (head == nullptr)

            tail = nullptr;

        return true;

    }

    return false;

}

void List::insertAfter(iterator it, Item item)

{

    if (it.node == nullptr)

    {

        // insert at head

        ListNode *tmp = new ListNode(item, head);

        // if list is empty, set tail to new node

        if (tail == nullptr)

        {

            tail = tmp;

        }

        // set head to new node

        head = tmp;

    }

    else

    {

        ListNode *tmp = new ListNode(item, it.node->next);

        it.node->next = tmp;

        // could be a new tail, if so update tail

        if (tail == it.node)

        {

            tail = tmp;

        }

    }

}

void List::removeAfter(iterator it, Item &item)

{

    // emtpy list or at tail, just return

    if (it.node == tail)

        return;

    if (it.node == nullptr)

    {

        ListNode *tmp = head;

        head = head->next;

        delete tmp;

        if (head == nullptr)

            tail = nullptr;

    }

    else

    {

        ListNode *tmp = it.node->next;

        it.node->next = tmp->next;

        delete tmp;

        // could be that it.node is the new nullptr

        if (it.node->next == nullptr)

            tail = it.node;

    }

}

List::iterator List::find(Item item)

{

   iterator it = iterator(head);

    while(it.node->next!=NULL){

        if(it.getItem() ==item)

            return it;

        it = it.node->next;

    }

    return iterator(NULL);

}

int main()

{

    List l;

    l.append("one");

    l.append("two");

    l.append("three");

    l.append("four");

    auto it = l.find("one");

    if (!it.end())

        std::cout << "Should be one: " << it.getItem() << std::endl;

    else

        std::cout << "Iterator should not have been null." << std::endl;

    auto no_it = l.find("zero");

    if (no_it.end())

        std::cout << "As expected, zero not found." << std::endl;

    else

        std::cout << "Oops! should not have found zero." << std::endl;

    return 0;

}


Related Solutions

Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer minimum score value and a string output file name as a parameters. The function will read the student scores and names from the file and output the names of the students with scores greater than or equal to the value given. This function returns the integer number of entries read from the file. If the input file cannot be opened, return -1 and do...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter...
5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter letter (as a string) and returns a list of indexes at which the letter occurs in the word. >>> indexes('mississippi', 's') [2, 3, 5, 6] >>> indexes('mississippi', 'i') [1, 4, 7, 10] >>> indexes('mississippi', 'a') []
4.31 Implement function duplicate() that takes as input the name (a string) of a file in...
4.31 Implement function duplicate() that takes as input the name (a string) of a file in the current directory and returns True if the file contains duplicate words and False otherwise. duplicate('Duplicates.txt') True duplicate('noDuplicates.txt') False Please solve using Python Language and without using str.maketrans please. Just simple programming, Thank youuuuu!!!!!
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will...
P-3.40 Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase...
P-3.40 Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) You should derive the decoding map from the forward version. P-3.41 Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous problem. P-3.42 Design a RandomCipher...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT