In: Computer Science
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 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;
}
------------------------------------------------------------------------------------Header 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
-------------------------------------------------------------------------------------------------------test file
#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;
}

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