Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

my linkedListType.h: #ifndef LINKEDLISTTYPE_H_ #define LINKEDLISTTYPE_H_ #include #include linkedListIterator.h template class linkedListType{ public: const linkedListType &operator= (const linkedListType &); //overload = operator void initializeList(); //initialize

image text in transcribed

image text in transcribed

my linkedListType.h:

#ifndef LINKEDLISTTYPE_H_

#define LINKEDLISTTYPE_H_

#include

#include "linkedListIterator.h"

template

class linkedListType{

public:

const linkedListType &operator=

(const linkedListType &);

//overload = operator

void initializeList();

//initialize list to empty

bool isEmptyList();

//function to see if list is empty

void print() const;

//function to print data from each node

int length() const;

//function to return # of nodes in list

void destroyList();

//function to delete all nodes

Type front() const;

//function to return first element of list

Type back() const;

//function to return last element of list

virtual bool search(const Type& searchItem) const = 0;

//function to see if seatchItem is in the list

virtual void insertFirst(const Type& newItem) = 0;

//function to insert newItem at beginning of list

virtual void insertLast(const Type& newItem) = 0;

//function to insert newItem at end of list

virtual void deleteNode(const Type& deleteItem) = 0;

//function to delete a node from list

linkedListIterator begin();

//function to return an iterator at the beginning of list

linkedListIterator end();

//function to return an iterator one element past the last element

linkedListType();

//default constructor

linkedListType(const linkedListType &otherList);

//copy constructor

~linkedListType();

//destructor

protected:

int count;

//variable to store number of elements in list

nodeType *first;//pointer for first node

nodeType *last;//pointer for last node

private:

void copyList(const linkedListType *otherList);

//function to make a copy of otherList

};

template

bool linkedListType::isEmptyList(){

return (first == nullptr);

}

template

linkedListType::linkedListType(){ //default constructor

first = nullptr;

last = nullptr;

count = 0;

}

template

void linkedListType::destroyList(){

nodeType *temp;

while(first != nullptr){

temp = first; //assign temp to first

first = first->next; //move first to next node

delete temp; //delete temp from memory

}

last = nullptr;

count = 0;

}

template

void linkedListType::initializeList(){

destroyList(); //if list has nodes, delete them

}

template

void linkedListType::print() const{

nodeType *current; //pointer to move through list

current = first;

while(current != nullptr){

cout info

current = current->next;

}

}

template

int linkedListType::length() const{

return count;

//end of length

}

template

Type linkedListType::front() const{

assert(first != nullptr);

return first->info; //retrrn info of first node

}

template

Type linkedListType::back() const{

assert(last != nullptr);

return last->info; //return info of last

}

template

linkedListIterator linkedListType::begin(){

linkedListIterator temp(first);

return temp;

}

template

linkedListIterator linkedListType::end(){

linkedListIterator temp(nullptr);

return last;

}

template

void linkedListType::copyList(const linkedListType* otherList){

nodeType *newNode;//pointer to create a node

nodeType *current;//pointer to move through list

if(first != nullptr){//if list is not empty

destroyList();

}

if(otherList->first == nullptr){//otherList is empty

first = nullptr;

last = nullptr;

count = 0;

}

else{

current = otherList->first; // current points tp list to be copied

count = otherList->count;

//copy first node

first = new nodeType; //create a node

first->info = current->info; // copy info

first->next = nullptr;

last = first; //make last equal to first if one node available

current = current->next; //make current point to next node

//copy remaining list

while(current != nullptr){

newNode = new nodeType; //create a node

newNode->info = current->info; //copy the info

newNode->next = nullptr; //set next pointer to null

last->next = newNode; //point last to newNode

last = newNode; //move last to newNode and newNode will be last

/ode in list

current = current->next; //move current to next node

}//end of while

}//end of else

}//end of copyList

template

linkedListType::~linkedListType(){

destroyList();

}//end destructor

template

linkedListType::linkedListType

(const linkedListType& otherList){

first = nullptr;

copyList(otherList);

}//end of copy constructor

//overload assignment operator

template

const linkedListType& linkedListType::operator=

(const linkedListType& otherList){

if(this != &otherList){ //to avoid self-copy

copyList(&otherList);

}

return *this;

}

#endif /* LINKEDLISTTYPE_H_ */

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

linkedListIterator.h:

#ifndef LINKEDLISTITERATOR_H_

#define LINKEDLISTITERATOR_H_

#include

#include

using namespace std;

template

class nodeType{

public:

Type info;

nodeType *next; //this is *link, changed it to next

//to not confuse myself

};

template

class linkedListIterator{

public:

linkedListIterator();

//default constructor

linkedListIterator(nodeType *ptr);

//constructor w/ parameters

Type operator*();

//function to overload dereferencing operator *

linkedListIterator operator++();

//function to overload pre-increment operator ++

bool operator==(const linkedListIterator &right) const;

//overload the equality operator

bool operator!=(const linkedListIterator &right) const;

//overload the not equal operator

private:

nodeType *current;

//pointer to point to current node in list

};

template

linkedListIterator::linkedListIterator(){

current = nullptr;

}

template

linkedListIterator::linkedListIterator(nodeType *ptr){

current = ptr;

}

template

Type linkedListIterator ::operator*(){

return current->info;

}

template

linkedListIterator linkedListIterator::operator++(){

current = current->next;

return *this;

}

template

bool linkedListIterator :: operator==

(const linkedListIterator &right) const{

return (current == right.current);

}

template

bool linkedListIterator::operator!=

(const linkedListIterator &right) const{

return (current != right.current);

}

#endif /* LINKEDLISTITERATOR_H_ */

HOMEWORK #23-Extending the linkedListType Class. Extend the class linkedListType by adding the following operations: 1. Find and delete the node with the smallest info in the list. Delete only the first occurrence and traverse the list only once. Use a purely virtual function: virtual void deleteSmallest()0 // Function to delete smallest element in the list // Postcondition: first points to beginning of list, last points to end of list, count decremented by 1 2. Find and delete the node with the largest info in the list. Delete only the first occurrence and traverse the list only once. Use a pure virtual function: virtual void deleteLargest () 0; // Function to delete largest element in the list // Postcondition: first points to beginning of list, last points to end of list, count decremented by 1 HOMEWORK #23-Extending the linkedListType Class. Extend the class linkedListType by adding the following operations: 1. Find and delete the node with the smallest info in the list. Delete only the first occurrence and traverse the list only once. Use a purely virtual function: virtual void deleteSmallest()0 // Function to delete smallest element in the list // Postcondition: first points to beginning of list, last points to end of list, count decremented by 1 2. Find and delete the node with the largest info in the list. Delete only the first occurrence and traverse the list only once. Use a pure virtual function: virtual void deleteLargest () 0; // Function to delete largest element in the list // Postcondition: first points to beginning of list, last points to end of list, count decremented by 1

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_2

Step: 3

blur-text-image_3

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

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions