Question

In: Computer Science

Add the following methods to the singly list implementation below. int size(); // Returns the number...

Add the following methods to the singly list implementation below.

int size(); // Returns the number of nodes in the linked list
bool search(string query); // Returns if the query is present in the list
void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// slist.cpp

#include <string>
#include "slist.h"

using namespace std;

Node::Node(string element) : data{element}, next{nullptr} {}

List::List() : first{nullptr} {}

// Adds to the front of the list
void List::pushFront(string element) {
Node* new_node = new Node(element);
if (first == nullptr) {// List is empty
first = new_node;
} else {
new_node->next = first;
first = new_node;
}
}

Iterator List::begin() {
Iterator iter;
iter.position = first;
iter.container = this;
return iter;
}

Iterator List::end() {
Iterator iter;
iter.position = nullptr;
iter.container = this;
return iter;
}

// Returns number of elements in the list
int List::size() {
// Q1: Your code here


}

// Returns if query is present in list (true/false)
bool List::search(string query) {
// Q2: Your code here


}

// Adds elements of input list to front of "this" list
void List::add(List& l) {
// Q3. Your code here

}

Iterator::Iterator() {
position = nullptr;
container = nullptr;
}

string Iterator::get() const {
return position->data;
}

void Iterator::next() {
position = position->next;
}

bool Iterator::equals(Iterator other) const {
return position == other.position;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use the following header file, and test program (not to be modified or uploaded!) to verify that your methods works correctly. The expected output is indicated slist.cpp

Note:

1. Please make sure to implement one method at a time (compile, and test). Comment out the unimplemented methods as you work along.

2. Please be sure to check that the code uploaded is indeed the one you intended to upload.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// slist.h file

/* Singly linked list */
#ifndef LIST_H
#define LIST_H

#include <string>

using namespace std;

class List;
class Iterator;

class Node
{
public:
Node(string element);
private:
string data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;
};

class List
{
public:
List();
void pushFront(string element);
Iterator begin();
Iterator end();
int size();
bool search(string query);
void add(List& l);
private:
Node* first;
friend class Iterator;
};

class Iterator
{
public:

Iterator();
string get() const;
void next();
bool equals(Iterator other) const;
private:
Node* position;
List* container;
friend class List;
};

#endif

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// slist_test.cpp

#include <string>
#include <iostream>
#include "slist.h"

using namespace std;

int main()
{
List names1;

names1.pushFront("Alice");
names1.pushFront("Bob");
names1.pushFront("Carol");
names1.pushFront("David");

// names1 is now - David Carol Bob Alice

int numele = names1.size(); // Q1: TO BE COMPLETED
cout << "Number of elements in the list: " << numele << endl;

string query = "Eve";
bool present = names1.search(query); // Q2: TO BE COMPLETED
if (present) {
cout << query << " is present" << endl;
} else {
cout << query << " is absent" << endl;
}


List names2;
names2.pushFront("Eve");
names2.pushFront("Fred");

// names2 is now - Fred Eve

// Insert each element of input list (names1) to front of calling list (names2)
names2.add(names1); // Q3: TO BE COMPLETED

// Print extended list
// Should print - Alice Bob Carol David Fred Eve
for (Iterator pos = names2.begin(); !pos.equals(names2.end()); pos.next()) {
cout << pos.get() << " ";
}
cout << endl;
return 0;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thank you for your time and help

Solutions

Expert Solution

#######################################
            main.cpp
#######################################
#include <string>

#include <iostream>

#include "slist.h"

using namespace std;

int main()

{

List names1;

names1.pushFront("Alice");

names1.pushFront("Bob");

names1.pushFront("Carol");

names1.pushFront("David");

// names1 is now - David Carol Bob Alice

int numele = names1.size(); // Q1: TO BE COMPLETED

cout << "Number of elements in the list: " << numele << endl;

string query = "Eve";

bool present = names1.search(query); // Q2: TO BE COMPLETED

if (present) {

cout << query << " is present" << endl;

} else {

cout << query << " is absent" << endl;

}

List names2;

names2.pushFront("Eve");

names2.pushFront("Fred");

// names2 is now - Fred Eve

// Insert each element of input list (names1) to front of calling list (names2)

names2.add(names1); // Q3: TO BE COMPLETED

// Print extended list

// Should print - Alice Bob Carol David Fred Eve

for (Iterator pos = names2.begin(); !pos.equals(names2.end()); pos.next()) {

cout << pos.get() << " ";

}

cout << endl;

return 0;

}



#######################################
           slist.cpp
#######################################
#include <string>

#include "slist.h"

using namespace std;

Node::Node(string element) : data(element), next(nullptr) {}

List::List() : first(nullptr) {}

// Adds to the front of the list

void List::pushFront(string element) {

Node* new_node = new Node(element);

if (first == nullptr) {// List is empty

first = new_node;

} else {

new_node->next = first;

first = new_node;

}

}

Iterator List::begin() {

Iterator iter;

iter.position = first;

iter.container = this;

return iter;

}

Iterator List::end() {

Iterator iter;

iter.position = nullptr;

iter.container = this;

return iter;

}

// Returns number of elements in the list

int List::size() {
        int count = 0;
        Node *start = first;
        while(start != nullptr) {
                count++;
                start = start->next;
        }
        return count;
}

// Returns if query is present in list (true/false)
bool List::search(string query) {
        Node *start = first;
        while(start != nullptr) {
                if(start->data == query) {
                        return true;
                }
                start = start->next;
        }
        return false;
}

// Adds elements of input list to front of "this" list
void List::add(List& l) {
        Node *start = l.first;
        while(start != nullptr) {
                pushFront(start->data);
                start = start->next;
        }
}

Iterator::Iterator() {

position = nullptr;

container = nullptr;

}

string Iterator::get() const {

return position->data;

}

void Iterator::next() {

position = position->next;

}

bool Iterator::equals(Iterator other) const {

return position == other.position;

}



#######################################
             slist.h
#######################################
/* Singly linked list */
#ifndef LIST_H
#define LIST_H

#include <string>

using namespace std;

class List;
class Iterator;

class Node {

public:

Node(string element);

private:

string data;

Node* previous;

Node* next;

friend class List;

friend class Iterator;

};

class List

{

public:

List();

void pushFront(string element);

Iterator begin();

Iterator end();

int size();

bool search(string query);

void add(List& l);

private:

Node* first;

friend class Iterator;

};

class Iterator

{

public:

Iterator();

string get() const;

void next();

bool equals(Iterator other) const;

private:

Node* position;

List* container;

friend class List;

};

#endif


**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Python Add your own implementation of the function rearrange_list rearrange_list(linkedlist, number) returns the result of rearranging...
Python Add your own implementation of the function rearrange_list rearrange_list(linkedlist, number) returns the result of rearranging the nodes in linkedlist so that: all the nodes whose values are less than number are at the front all the nodes whose values are greater than number are at the rear any nodes whose values are equal to number are in the middle For example, given the linked list represented by linkedlist = -2 -> -3 -> -1 -> 2 -> 3 ->...
Add a copy constructor for the linked list implementation below: ---------------------------------------------------------------------------------------------------------------------------------------------------- // list.cpp file #include <string>
Add a copy constructor for the linked list implementation below: ---------------------------------------------------------------------------------------------------------------------------------------------------- // list.cpp file #include <string> #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs) // Copy constructor - homework { // Your code here    } void List::push_back(string element) { Node* new_node = new Node(element); if (last == nullptr) // List is empty { first = new_node; last...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some missing functionality. It contains a templated Node class and a templated LinkedList class. Do not modify the class definitions. The linked list class contains the following methods: • LinkedList – Constructor of a linked list with a head pointing to null (implemented). • ~LinkedList – Destructor of a linked list. • deleteFromHead – Removes and returns content of the first node of the list....
Give a complete implementation of the queue ADT using a singly linked list that includes a...
Give a complete implementation of the queue ADT using a singly linked list that includes a header sentinel (in Python).
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT