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...
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
Implement function noVowel() that takes a string s as input and returns True if no char-...
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 Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10])...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT