Your program is to create, access, and update two ordered linked lists. Using th
ID: 3589931 • Letter: Y
Question
Your program is to create, access, and update two ordered linked lists. Using the provided class implementations, your driver file should create one list using the integer type and another using the string type. After creating the two lists, use the input from the intInsert.dat and strInsert.dat files to add data to the ordered linked lists. These files have one data item per line. Insert the data items to their respective list objects. Once data has been added to the two lists, you will utilize the search function provided by the class to look for certain items in the lists. The intSearch.dat and strSearch.dat files have one data item per line. Search the respective lists for each data item in these files. Your program should display whether or not the item is found in the list. For example, if your integer list object contains 5, 15, and 25 and the search file contains 7 and 25, your program should output: 7 was NOT found in the list 25 was found in the list Similar output will be shown when searching for the string items. You are also required to write an additional function that will be added in the provided linked list class definition. Write a function that will “concatenate” all the items in the list. You can call the function concat() for short. For a list of integers, this function should return the sum of all items in the list. For a list of strings, it should return a string made up of all the items combined into a longer string. For example, if your integer list object contains 5, 15, and 25, the concat() function should return 45. If your string list object contains “hello” and “world”, the concat() function should return “helloworld”. Your program should display the results of the concatenation function call to the screen for both lists.
The programming language is C++
Explanation / Answer
CODE
linkedListType.h
#ifndef __Lab__linkedListType__
#define __Lab__linkedListType__
#include "linkedListIterator.h"
#include <cassert>
template <class Type>
class linkedListType {
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmptyList() const;
void print() const;
int length() const;
void destroyList();
Type front() const;
Type back() const;
virtual bool search(const Type& searchItem) const = 0;
virtual void insertFirst(const Type& newItem) = 0;
virtual void insertLast(const Type& newItem) = 0;
virtual void deleteNode(const Type& deleteItem) = 0;
linkedListIterator<Type> begin();
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
Type concat();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
template <class Type>
linkedListType<Type>::linkedListType() {
first = NULL;
last = NULL;
count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList() {
nodeType<Type> *temp;
while (first != NULL) {
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template <class Type>
bool linkedListType<Type>::isEmptyList() const {
return (first == NULL);
}
template <class Type>
void linkedListType<Type>::initializeList() {
destroyList();
}
template <class Type>
void linkedListType<Type>::print() const {
nodeType<Type> *current;
current = first;
while (current != NULL)
while (current != NULL)
{
{
cout << current->info << " ";
current = current->link;
}
}
template <class Type>
int linkedListType<Type>::length() const {
return count;
}
template <class Type>
Type linkedListType<Type>::front() const {
assert(first != NULL);
return first->info;
}
template <class Type>
Type linkedListType<Type>::back() const {
assert(last != NULL);
return last->info;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin() {
linkedListIterator<Type> temp(first); return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end() {
linkedListIterator<Type> temp(NULL); return temp;
}
template <class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList) {
nodeType<Type> *newNode;
nodeType<Type> *current;
if (first != NULL)
destroyList();
if (otherList.first == NULL)
{
first = NULL;
last = NULL;
count = 0;
}
else {
current = otherList.first;
count = otherList.count;
first = new nodeType<Type>;
first->info = current->info;
first->link = NULL;
last = first;
current = current->link;
while (current != NULL) {
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
template <class Type> linkedListType<Type>::~linkedListType() {
destroyList();
}
template <class Type> linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList) {
first = NULL;
copyList(otherList);
}
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList) {
if (this != &otherList)
{
copyList(otherList);
}
return *this;
}
template <class Type>
Type linkedListType<Type>::concat()
{
Type result;
nodeType<Type> *current = first;
while(current != NULL)
{
result += current->info;
current = current->link;
}
return result;
}
#endif
driver.cpp
#include "orderedLinkedList.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
orderedLinkedList<int> numbers;
orderedLinkedList<string> words;
ifstream infile("intInsert.dat");
if(!infile.is_open())
{
cout << "ERROR: intInsert.dat not found" << endl;
return 1;
}
int num;
while(infile >> num)
numbers.insert(num);
infile.close();
infile.open("strInsert.dat");
if(!infile.is_open())
{
cout << "ERROR: strInsert.dat not found" << endl;
return 1;
}
string str;
while(infile >> str)
words.insert(str);
infile.close();
infile.open("intSearch.dat");
if(!infile.is_open())
{
cout << "ERROR: intSearch.dat not found" << endl;
return 1;
}
while(infile >> num)
{
if(numbers.search(num))
cout << num << " was found in the list" << endl;
else
cout << num << " was NOT found in the list " << endl;
}
infile.close();
infile.open("strSearch.dat");
if(!infile.is_open())
{
cout << "ERROR: strSearch.dat not found" << endl;
return 1;
}
while(infile >> str)
{
if(words.search(str))
cout << str << " was found in the list" << endl;
else
cout << str << " was NOT found in the list " << endl;
}
infile.close();
cout << endl;
cout << "numbers.concat() = " << numbers.concat()<< endl;
cout << "words.concat() = " << words.concat() << endl;
}
Please get back to me if any queries in code and execution
Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.