Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have a problem with my code and I'm not sure how to fix it. Please help. linkedListIterator.h: #ifndef LINKEDLISTITERATOR_H #define LINKEDLISTITERATOR_H #include #include

Hello, I have a problem with my code and I'm not sure how to fix it. Please help.

linkedListIterator.h:

#ifndef LINKEDLISTITERATOR_H #define LINKEDLISTITERATOR_H

#include #include using namespace std;

template class nodeType{ 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(node Type *ptr){ current = ptr; }

template 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); }

bool linkedListIterator::operator!= (const linkedListIterator &right) const{ return (current != right.current); }

#endif /* LINKEDLISTITERATOR_H */

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

linkedListType.h:

#include #include "linkedListIterator.h"

#ifndef LINKEDLISTTYPE_H #define LINKEDLISTTYPE_H

template class linkedListType{ public: const linkedListType &operator= (const llinkedListType &); //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() const{ 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 << current->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 //node 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 linkedListType& linkedListType::operator= (const linkedListType &otherList){ if(this != &otherList){ //to avoid self-copy copyList(otherList); } return *this; }

#endif /* LINKEDLISTTYPE_H */

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

orderedLinkedList.h:

#include "linkedListType.h" using namespace std;

#ifndef ORDEREDLINKEDLIST_H #define ORDEREDLINKEDLIST_H

template class orderedLinkedList: public linkedListType{ public: bool search(const Type& searchItem) const; //function to see if searchItem is in list. void insert(const Type& newItem); //function to insert newItem in list void insertFirst(const Type& newItem); //function to insert newItem as first node properly void insertLast(const Type& newItem); //function to insert newItem as last node properly void deleteNode(const Type& deleteItem); //function to delete deleteItem from list };

template bool orderedLinkedList::search(const Type& searchItem) const{ bool found = false; nodeType *current; //pointer to move through list current = first; //start search from first node while(current != nullptr && !found){ if(current->info >= searchItem){ found = true; } else{ current = current->next; //if not found, move current } }//end while if(found){ found = (current->info == searchItem); //test for equality } return found; }//end search

template void orderedLinkedList::insert(const Type& newItem){ nodeType *current; //pointer to move through list nodeType *trailCurrent == nullptr; //pointer before current nodeType *newNode; //pointer to create a node bool found; newNode = new nodeType; //create new node newNode->info = newItem; //store newItem in the node newNode->next = nullptr; //set next to null if(first == nullptr){ //first case: if list is empty first = newNode; last = newNode; count+; } else{ current = first; found = false; while(current != nullptr && !found){ //searching list if(current->info >= newItem){ found = true; } else{ trailCurrent = current; //move trailCurrent to current current = current->next; //move current ahead of trailCurrent } }//end of while if(current == first){ //second case: if only one node available if(current == first){ newNode->next = first; first = newNode; count++; } else{ //third case: search is last node trailCurrent->next = newNode; newNode->next = current; if(current == nullptr){ last = newNode; } count++; }//end else }//end if }//end else }//end insert

template void orderedLinkedList::insertFirst(const Type& newItem){ insert(newItem); }//end insertFirst

template void orderedLinkedList::insertLast(const Type& newItem){ insert(newItem); }//end insertLast

template void orderedLinkedList::deleteNode(const Type& deleteItem){ nodeType *current; //to move through list nodeType *trailCurrent; bool found; if(first == nullptr){ //first case: if list is empty cout << "list is empty." << endl; } else{ current = first; found = false; while(current != nullptr && !found){ //search the list if(current != nullptr >= deleteItem){ found = true; } else{ trailCurrent = current; current = current->next; } }//end while if(current == nullptr){ //forth case: if item is not in list cout << "item is not in the list" << endl; } else{ if(current->info == deleteItem){ //item to delete is in list if(first == current){ //second case: if item is first node first = first->next; //move first if(first == nullptr){ last = nullptr; } delete current; } else{ //third case: if item is last node trailCurrent->next = current->next; if(current == last){ last = trailCurrent; } delete current; } count--; } else{ //fourth case: item is not in list cout << "item is not in the list." << endl; } }//end else }//end else }//end deleteNode

#endif /* ORDEREDLINKEDLIST_H */

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

main.cpp:

#include #include #include "orderedLinkedList.h" #include "unorderedLinkedList.h" using namespace std;

int main() { orderedLinkedList list1, list2; int num; cout << "enter number -999 to end" << endl; cin >> num; while (num != -999){ list1.insert(num); cin >> num; } cout << endl; cout << "list1: "; list1.print(); cout << endl; list2 = list1; cout << "list2: "; list2.print(); cout << endl; cout << "enter a number to be deleted: "; cin >> num; cout << endl; list2.deleteNode(num); cout << "print list2 after deleting the number: "; list2.print(); cout << endl; exit(EXIT_SUCCESS) }

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

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions

Question

Solve the following equations. 3x + 5y = 11 2x- y=16

Answered: 1 week ago

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago