Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef H_Unordered #define H_Unordered #includelinkedListType.h template < class Type > class unorderedLinkedList : public linkedListType < Type > { public: bool search(const Type& searchItem) const;

#ifndef H_Unordered
#define H_Unordered
#include"linkedListType.h"
template < class Type >
class unorderedLinkedList : public linkedListType < Type > {
public:
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the list,
// otherwise the value false is returned.
void insertfirst(const Type& newItem);
//Function to insert newItem at the beginning of the list.
//Postcondition: this->first points to the new list, newItem is
// inserted at the beginning of the list, this->last points to
// the this->last node, and this->count is incremented by 1.
//
void insertlast(const Type& newItem);
//Function to insert newItem at the end of the list.
//Postcondition: this->first points to the new list, newItem is
// inserted at the end of the list, this->last points to the
// this->last node, and this->count is incremented by 1.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing deleteItem
// is deleted from the list. this->first points to the this->first
// node, this->last points to the this->last node of the updated
// list, and this->count is decremented by 1.
};
template < class Type >
bool unorderedLinkedList < Type > ::
search(const Type& searchItem) const {
nodeType < Type >* current; //pointer to traverse the list
bool found = false;
current = this->first; //set current to point to the this->first
//node in the list
while (current != NULL && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
} //end search
template < class Type >
void unorderedLinkedList < Type > ::insertfirst(const Type& newItem) {
nodeType < Type >* newNode; //pointer to create the new node
newNode = new nodeType < Type >; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = this->first; //insert newNode before this->first
this->first = newNode; //make this->first point to the actual this->first node
this->count++; //increment this->count
if (this->last == NULL) //if the list was empty, newNode is also
//the this->last node in the list
this->last = newNode;
} //end insertthis->first
template < class Type >
void unorderedLinkedList < Type > ::insertlast(const Type& newItem) {
nodeType < Type >* newNode; //pointer to create the new node
newNode = new nodeType < Type >; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = NULL; //set the link field of newNode to NULL
if (this->first == NULL) //if the list is empty, newNode is
//both the this->first and this->last node
{
this->first = newNode;
this->last = newNode;
this->count++; //increment this->count
}
else //the list is not empty, insert newNode after this->last
{
this->last->link = newNode; //insert newNode after this->last
this->last = newNode; //make this->last point to the actual
//this->last node in the list
this->count++; //increment this->count
}
} //end insertthis->last
template < class Type >
void unorderedLinkedList < Type > ::deleteNode(const Type& deleteItem) {
nodeType < Type >* current; //pointer to traverse the list
nodeType < Type >* trailCurrent; //pointer just before current
bool found;
if (this->first == NULL) //Case 1; the list is empty.
cout << "Cannot delete from an empty list." <<
endl;
else {
if (this->first->info == deleteItem) //Case 2
{
current = this->first;
this->first = this->first->link;
this->count--;
if (this->first == NULL) //the list has only one node
this->last = NULL;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = this->first; //set trailCurrent to point
//to the this->first node
current = this->first->link; //set current to point to
//the second node
while (current != NULL && !found) {
if (current->info != deleteItem) {
trailCurrent = current;
current = current->link;
}
else
found = true;
} //end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
this->count--;
if (this->last == current) //node to be deleted was the
//this->last node
this->last = trailCurrent; //update the value of this->last
delete current; //delete the node from the list
}
else
cout << "The item to be deleted is not in " <<
"the list." << endl;
} //end else
} //end else
} //end deleteNode
#endif
#ifndef H_LinkedListType
#define H_LinkedListType
#include
using namespace std;
template < class Type >
struct nodeType {
Type info;
nodeType < Type >* link;
};
template < class Type >
class linkedListType {
public:
const linkedListType < Type >& operator =
(const linkedListType < Type >&);
//Overload the assignment operator.
void initializeList();
//Initialize the list to an empty state.
//Postcondition: this->first = NULL, this->last = NULL, this->count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty, otherwise
// it returns false.
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of this->count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: this->first = NULL, this->last = NULL, this->count = 0;
Type front() const;
//Function to return the this->first element of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: If the list is empty, the program terminates;
// otherwise, the this->first element of the list is returned.
Type back() const;
//Function to return the this->last element of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the this->last
// element of the list is returned.
virtual bool search(const Type& searchItem) const = 0;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the list,
// otherwise the value false is returned.
virtual void insertfirst(const Type& newItem) = 0;
//Function to insert newItem at the beginning of the list.
//Postcondition: this->first points to the new list, newItem is
// inserted at the beginning of the list, this->last points to
// the this->last node in the list, and this->count is incremented by
// 1.
virtual void insertlast(const Type& newItem) = 0;
//Function to insert newItem at the end of the list.
//Postcondition: this->first points to the new list, newItem is
// inserted at the end of the list, this->last points to the
// this->last node in the list, and this->count is incremented by 1.
virtual void deleteNode(const Type& deleteItem) = 0;
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing deleteItem is
// deleted from the list. this->first points to the this->first node,
// this->last points to the this->last node of the updated list, and
// this->count is decremented by 1.
linkedListType();
//default constructor
//Initializes the list to an empty state.
//Postcondition: this->first = NULL, this->last = NULL, this->count = 0;
linkedListType(const linkedListType < Type >& otherList);
//copy constructor
~linkedListType();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of list elements
nodeType < Type >* first; //pointer to the this->first node of the list
nodeType < Type >* last; //pointer to the this->last node of the list
private:
void copyList(const linkedListType < 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 linkedListType < Type > ::isEmptyList() const {
return (this->first == NULL);
}
template < class Type >
linkedListType < Type > ::linkedListType() //default constructor
{
this->first = NULL;
this->last = NULL;
this->count = 0;
}
template < class Type >
void linkedListType < Type > ::destroyList() {
nodeType < Type >* temp; //pointer to deallocate the memory
//occupied by the node
while (this->first != NULL) //while there are nodes in the list
{
temp = this->first; //set temp to the current node
this->first = this->first->link; //advance this->first to the next node
delete temp; //deallocate the memory occupied by temp
}
this->last = NULL; //initialize this->last to NULL; this->first has already
//been set to NULL by the while loop
this->count = 0;
}
template < class Type >
void linkedListType < Type > ::initializeList() {
destroyList(); //if the list has any nodes, delete them
}
template < class Type >
void linkedListType < Type > ::print() const {
nodeType < Type >* current; //pointer to traverse the list
current = this->first; //set current point to the this->first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
cout<
} //end print
template < class Type >
int linkedListType < Type > ::length() const {
return this->count;
}
template < class Type >
Type linkedListType < Type > ::front() const {
assert(this->first != NULL);
return this->first->info; //return the info of the this->first node
} //end front
template < class Type >
Type linkedListType < Type > ::back() const {
assert(this->last != NULL);
return this->last->info; //return the info of the this->last node
} //end back
template < class Type >
void linkedListType < Type > ::copyList(const linkedListType < Type >& otherList) {
nodeType < Type >* newNode; //pointer to create a node
nodeType < Type >* current; //pointer to traverse the list
if (this->first != NULL) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == NULL) //otherList is empty
{
this->first = NULL;
this->last = NULL;
this->count = 0;
}
else {
current = otherList.first; //current points to the
//list to be copied
this->count = otherList.count;
//copy the this->first node
this->first = new nodeType < Type >; //create the node
this->first->info = current->info; //copy the info
this->first->link = NULL; //set the link field of the node to NULL
this->last = this->first; //make this->last point to the this->first node
current = current->link; //make current point to the next
// node
//copy the remaining list
while (current != NULL) {
newNode = new nodeType < Type >; //create a node
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of newNode to NULL
this->last->link = newNode; //attach newNode after this->last
this->last = newNode; //make this->last point to the actual this->last
//node
current = current->link; //make current point to the
//next node
} //end while
} //end else
} //end copyList
template < class Type >
linkedListType < Type > ::~linkedListType() //destructor
{
destroyList();
}
template < class Type >
linkedListType < Type > ::linkedListType(const linkedListType < Type >& otherList) {
this->first = NULL;
copyList(otherList);
} //end copy constructor
//overload the assignment operator
template < class Type >
const linkedListType < Type >& linkedListType < Type > ::operator =
(const linkedListType < Type >& otherList) {
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
} //end else
return *this;
}
#endif
#include
#include "Unordered.h"
#include "LinkedListType.h"
using namespace std;
int main()
{
unorderedLinkedList L,L1,L2;
L.insertlast(2);
L.insertlast(1);
L.insertlast(3);
L.insertlast(4);
L.insertlast(5);
L.insertlast(3);
L.insertlast(0);
cout << "List L before L.incrementByConstant(3) contains:" << endl;
L.print();
//
L.incrementByConstant(3);
cout << "List L after L.incrementByConstant(3) contains:" << endl;
L.print();
L.destroyList();
L.insertlast(2);
L.insertlast(1);
L.insertlast(3);
L.insertlast(4);
L.insertlast(5);
L.insertlast(3);
L.insertlast(0);
cout << "List L before L.sortEach2(\'a\') contains:" << endl;
L.print();
//
L.sortEach2('a');
cout << "List L after L.sortEach2(\'a\') contains:" << endl;
L.print();
L.destroyList();
L.insertlast(2);
L.insertlast(1);
L.insertlast(3);
L.insertlast(4);
L.insertlast(5);
L.insertlast(3);
L.insertlast(0);
cout << "List L before L.sortEach2(\'d\') contains:" << endl;
L.print();
//
L.sortEach2('d');
cout << "List L after L.sortEach2(\'d\') contains:" << endl;
L.print();
L.destroyList();
L.insertlast(2);
L.insertlast(1);
L.insertlast(3);
L.insertlast(4);
L.insertlast(5);
L.insertlast(3);
L.insertlast(0);
cout << "List L before L.duplicateORremove(3) contains:" << endl;
L.print();
//
L.duplicateORremove(3);
cout << "List L after L.duplicateORremove(3) contains:" << endl;
L.print();
L.destroyList();
L.insertlast(2);
L.insertlast(1);
L.insertlast(3);
L.insertlast(4);
L.insertlast(5);
L.insertlast(3);
L.insertlast(0);
cout << "List L before L.split(3,L1,L2) contains:" << endl;
L.print();
//
L.split(3,L1,L2);
cout << "List L after L.split(3,L1,L2) contains:" << endl;
L.print();
cout << "List L1 after L.split(3,L1,L2) contains:" << endl;
L1.print();
cout << "List L2 after L.split(3,L1,L2) contains:" << endl;
L2.print();
return 0;
}
Part 3: Redo Part 1 on the linkedListType.h. You cannot use any predefined member function. All your modifications should be performed inside the header file linkedListType.h that is attached with this assignment. After performing the modifications, you should run the file memberLinkedList.cpp (attached with this assignment) without modifying it without any problems and you should get the following output:
List L before L.incrementByConstant(3) contains:
2 1 3 4 5 3 0
List L after L.incrementByConstant(3) contains:
5 4 6 7 8 6 3
List L before L.sortEach2('a') contains:
2 1 3 4 5 3 0
List L after L.sortEach2('a') contains:
1 2 3 4 3 5 0
List L before L.sortEach2('d') contains:
2 1 3 4 5 3 0
List L after L.sortEach2('d') contains:
2 1 4 3 5 3 0
List L before L.duplicateORremove(3) contains:
2 1 3 4 5 3 0
List L after L.duplicateORremove(3) contains:
3 4 4 5 5 3
List L before L.split(3,L1,L2) contains:
2 1 3 4 5 3 0
List L after L.split(3,L1,L2) contains:
2 1 3 4 5 3 0
List L1 after L.split(3,L1,L2) contains:
2 1 0
List L2 after L.split(3,L1,L2) contains:
3 4 5 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sql++ For Sql Users A Tutorial

Authors: Don Chamberlin

1st Edition

0692184503, 978-0692184509

More Books

Students also viewed these Databases questions