16.2)<in C++>Splitting a linked list at a given node into two sublists. a. (belo
ID: 3833918 • Letter: 1
Question
16.2)<in C++>Splitting a linked list at a given node into two sublists.
a. (below)Add the following operation to the class linkedListType:
void divideAt(linkedListType &secondList,
const Type& item);
/* Divide the list at the node with the info item into two sublists.
* Postcondition: first and last point to the first and
* last nodes of the first sublist.
* secondList.first and secondList.last
* point to the first and last nodes of the
* second sublist.
*/
Consider the following statements:
unorderedLinkedList myList;
unorderedLinkedList otherList;
Suppose myList points to the list with the elements 34 65 18 39 27 89 12 (in this order). The statement: myList.divideAt(otherList, 18); divides myList into two sublists: myList points to the list with the elements 34 65 while otherList points to the sublist with the elements 18 39 27 89 12.
b. Write the definition of the function template to implement the operation divideAt. Also, write a program to test your function. The header files linkedList.h and unorderedLinkedList.h are supplied.
Your test program should produce output similar to this:
Enter numbers ending with -999
22 34 56 2 89 90 0 14 56 11 43 55 -999
Enter the number at which to split list: 0
list and otherList after splitting at 0
list: 22 34 56 2 89 90
Length of list: 6
otherList: 0 14 56 11 43 55
Length of subList: 6
Turn in:
All your template files
Your test program.
The results of running your test program
(having trouble with the divideAt function working correctly in the main file)
// UnorderedLinkedList.h
#ifndef H_UnorderedLinkedList
#define H_UnorderedLinkedList
#include "linkedList.h"
using namespace std;
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
};
template <class Type>
bool unorderedLinkedList<Type>::
search(const Type& searchItem) const
{
nodeType<Type> *current;
bool found = false;
current = this->first;
while (current != NULL && !found)
if (current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = this->first;
this->first = newNode;
this->count++;
if (this->last == NULL)
this->last = newNode;
}
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = NULL;
if (this->first == NULL)
{
this->first = newNode;
this->last = newNode;
this->count++;
}
else
{
this->last->link = newNode;
this->last = newNode;
this->count++;
}
}
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if (this->first == NULL)
cout << "Cannot delete from an empty list."
<< endl;
else
{
if (this->first->info == deleteItem)
{
current = this->first;
this->first = this->first->link;
this->count--;
if (this->first == NULL)
this->last = NULL;
delete current;
}
else
{
found = false;
trailCurrent = this->first;
current = this->first->link;
while (current != NULL && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}
if (found)
{
trailCurrent->link = current->link;
this->count--;
if (this->last == current)
this->last = trailCurrent;
delete current;
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}
}
}
#endif
//linkedList.h
#pragma once
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public:
linkedListIterator();
linkedListIterator(nodeType<Type> *ptr);
Type operator*();
linkedListIterator<Type> operator++();
bool operator==(const linkedListIterator<Type>& right) const;
bool operator!=(const linkedListIterator<Type>& right) const;
nodeType<Type> *getCurrent() const;
~linkedListIterator();
private:
nodeType<Type> *current;
};
template <class Type>
linkedListIterator<Type>::~linkedListIterator() {
current = NULL;
}
template <class Type>
nodeType<Type> *linkedListIterator<Type>::getCurrent() const {
return this->current;
}
template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
current = NULL;
}
template <class Type>
linkedListIterator<Type>::
linkedListIterator(nodeType<Type> *ptr)
{
current = ptr;
}
template <class Type>
Type linkedListIterator<Type>::operator*()
{
return current->info;
}
template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
current = current->link;
return *this;
}
template <class Type>
bool linkedListIterator<Type>::operator==
(const linkedListIterator<Type>& right) const
{
return (current == right.current);
}
template <class Type>
bool linkedListIterator<Type>::operator!=
(const linkedListIterator<Type>& right) const
{
return (current != right.current);
}
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;
void divideMid(linkedListType<Type> &sublist);
void divideAt(linkedListType<Type> &secondList, const Type& item);
linkedListIterator<Type> begin();
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
return(first == NULL);
}
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;
}
last = NULL; /
count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current;
current = first;
while (current != last)
{
cout << current->info << " ";
current = current->link;
}
cout << current->info << " ";
}
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; d
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>
void linkedListType<Type>::divideMid(linkedListType<Type> &sublist) {
linkedListIterator<Type> itr = this->begin();
int mainLength = this->length();
if ((mainLength % 2) == 0) {
for (int i = 0; i <= (mainLength / 2) - 2; i++) {
++itr;
}
sublist.last = this->last;
this->last = itr.getCurrent();
++itr;
sublist.first = itr.getCurrent();
}
else {
for (int i = 0; i <= (int)(mainLength / 2) - 2; i++) {
++itr;
}
sublist.last = this->last;
this->last = itr.getCurrent();
++itr;
sublist.first = itr.getCurrent();
}
}
template <class Type>
void linkedListType<Type>::divideAt(linkedListType<Type> &secondList, const Type& item) {
linkedListIterator<Type> itr = this->begin();
int reCount = 0;
while (itr.getCurrent()->info != item) {
++itr;
}
secondList.last = this->last;
this->last = itr.getCurrent();
++itr;
secondList.first = itr.getCurrent();
linkedListIterator<Type> itr2 = this->begin();
while (itr2.getCurrent() != this->last) {
reCount++;
++itr2;
}
this->count = reCount + 1;
reCount = 0;
linkedListIterator<Type> itr3 = secondList.begin();
while (itr3.getCurrent() != secondList.last) {
reCount++;
++itr3;
}
secondList.count = reCount + 1;
reCount = 0;
}
#endif
//Source.cpp
#include "unorderedLinkedList.h"
#include "linkedList.h"
int main() {
unorderedLinkedList<int> list1 = unorderedLinkedList<int>();
unorderedLinkedList<int> list2 = unorderedLinkedList<int>();
int u = 0;
int s = 0;
cout << "Enter numbers ending with -999 " << endl;
while (u != -999) {
cin >> u;
if (u != -999)
list1.insertLast(u);
}
cout << "Enter the number at which to split list: ";
cin >> s;
cout << endl;
//list1.divideAt(list2, s);
cout << "List and other list after splitting at " << s << endl;
cout << "List: ";
list1.print();
cout << endl;
cout << "Length of list: " << list1.length() << endl;
cout << "other list: ";
list2.print();
cout << endl;
cout << "Length of sublist: " << list2.length() << endl;
list1.initializeList();
list2.initializeList();
return 0;
}
Explanation / Answer
#ifndef myList
#define myList
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template<class Type>
class myList
{
friend ostream& operator<< <Type> (ostream&, const myList<Type>&);
public:
const myList<Type>& operator=
(const myList<Type>&);
//Overload the assignment operator.
void initializeList();
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL,
bool isEmptyList();
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
int length();
//Function to return the number of nodes in the
//list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = NULL, last = NULL,
Type front();
//Function to return the first element of the list.
//Precondition: The list must exist and must not be
//empty.
//Postcondition: If the list is empty, then the
// program terminates; otherwise,
// the first element of the list is
// returned.
Type back();
//Function to return the last element of the
//list.
//Precondition: The list must exist and must not
//be empty.
//Postcondition: If the list is empty, then the
// program terminates; otherwise,
// the last element of the list is
// returned.
bool search(const Type& searchItem);
//Function to determine whether searchItem is in
//the list.
//Postcondition: Returns true if searchItem is found
// in the list; otherwise, it returns
// false.
void insertFirst(const Type& newItem);
//Function to insert newItem in the list.
//Postcondition: first points to the new list
// and newItem is inserted at the
// beginning of the list.
void insertLast(const Type& newItem);
//Function to return newItem at the end of the
//list.
//Postcondition: first points to the new list,
// newItem is inserted at the end
// of the list, and last points to
// the last node in the list.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the
// list, first points to the first
// node, and last points to the last
// node of the updated list.
void divideMid(myList<Type> &sublist);
//This operation splits the given list in two sublists of
//(almost) equal size.
//Post: first points the first node and last
// points to the last node of the first sublist
// sublist.first points to the first node and sublist.last
// points to the last node of the second sub list.
myList();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL,
// count = 0
myList(const myList<Type>& otherList);
//copy constructor
~ myList();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of
//elements in the list
nodeType<Type> *first; //pointer to the first node of
//the list
nodeType<Type> *last; //pointer to the last node of
//the list
private:
void copyList(const myList<Type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created
// and assigned to this list.
};
template<class Type>
bool myList<Type>::isEmptyList()
{
return(first == NULL);
}
template<class Type>
myList<Type>:: myList() // default constructor
{
first = NULL;
last = NULL;
count = 0;
}
#ifndef myList
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.