Hello, I have a problem with my code and I\'m not sure how to fix it. Please hel
ID: 3731604 • Letter: H
Question
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 <iostream>
#include <cassert>
using namespace std;
template <class Type>
class nodeType{
Type info;
nodeType<Type> *next; //this is *link, changed it to next
//to not confuse myself
};
template <class Type>
class linkedListIterator{
public:
linkedListIterator();
//default constructor
linkedListIterator(nodeType<Type> *ptr);
//constructor w/ parameters
Type operator*();
//function to overload dereferencing operator *
linkedListIterator<Type> operator++;
//function to overload pre-increment operator ++
bool operator==(const linkedListIterator<Type> &right) const;
//overload the equality operator
bool operator!=(const linkedListIterator<Type> &right) const;
//overload the not equal operator
private:
nodeType<Type> *current;
//pointer to point to current node in list
};
template <class Type>
linkedListIterator<Type>::linkedListIterator(){
current = nullptr;
}
template<class Type>
linkedListIterator <Type>::linkedListIterator(node Type<Type> *ptr){
current = ptr;
}
template<class Type>
linkedListIterator <Type>::operator*(){
return current->info;
}
template<class Type>
linkedListIterator <Type> linkedListIterator<Type>::operator++{
current = current->next;
return *this;
}
template<class Type>
bool linkedListIterator <Type>:: operator==
(const linkedListIterator<Type> &right) const{
return (current == right.current);
}
bool linkedListIterator<Type>::operator!=
(const linkedListIterator<Type> &right) const{
return (current != right.current);
}
#endif /* LINKEDLISTITERATOR_H */
----------------------------------------------------------------------
linkedListType.h:
#include<cassert>
#include "linkedListIterator.h"
#ifndef LINKEDLISTTYPE_H
#define LINKEDLISTTYPE_H
template<class Type>
class linkedListType{
public:
const linkedListType<Type> &operator=
(const llinkedListType<Type> &);
//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<Type> begin();
//function to return an iterator at the beginning of list
linkedListIterator<Type> end();
//function to return an iterator one element past the last element
linkedListType();
//default constructor
linkedListType(const linkedListType<Type> &otherList);
//copy constructor
~linkedListType();
//destructor
protected:
int count;
//variable to store number of elements in list
nodeType<Type> *first;//pointer for first node
nodeType<Type> *last;//pointer for last node
private:
void copyList(const linkedListType<Type> *otherList);
//function to make a copy of otherList
};
template<class Type>
bool linkedListType<Type>::isEmptyList() const{
return (first == nullptr);
}
template<class Type>
linkedListType<Type>::linkedListType(){ //default constructor
first = nullptr;
last = nullptr;
count = 0;
}
template <calss Type>
void linkedListType::destroyList(){
nodeType<Type> *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<class Type>
void linkedListType<Type>::initializeList(){
destroyList; //if list has nodes, delete them
}
template<class Type>
void linkedListType<Type>::print() const{
nodeType<Type> *current; //pointer to move through list
current = first;
while(current != nullptr){
cout << current->info << " ";
current = current->next;
}
}
template<class Type>
int linkedListType<Type>::length() const{
return count;
//end of length
}
template<class Type>
Type linkedListType<Type>::front() const{
assert(first != nullptr);
return first->info; //retrrn info of first node
}
template<class Type>
Type linkedListType<Type>::back() const{
assert(last != nullptr);
return last->info; //return info of last
}
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(nullptr);
return last;
}
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>* otherList){
nodeType<Type> *newNode;//pointer to create a node
nodeType<Type> *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<Type>; //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<Type>; //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<class Type>
linkedListType<Type>:~linkedListType(){
destroyList();
}//end destructor
template<class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList){
first = nullptr;
copyList(otherList);
}//end of copy constructor
//overload assignment operator
template<class Type>
linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type> &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 Type>
class orderedLinkedList: public linkedListType<Type>{
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<class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const{
bool found = false;
nodeType<Type> *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<class Type>
void orderedLinkedList<Type>::insert(const Type& newItem){
nodeType<Type> *current; //pointer to move through list
nodeType<Type> *trailCurrent == nullptr; //pointer before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //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<class Type>
void orderedLinkedList<Type>::insertFirst(const Type& newItem){
insert(newItem);
}//end insertFirst
template<class Type>
void orderedLinkedList<Type>::insertLast(const Type& newItem){
insert(newItem);
}//end insertLast
template<class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem){
nodeType<Type> *current; //to move through list
nodeType<Type> *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 <iostream>
#include <cstdlib>
#include "orderedLinkedList.h"
#include "unorderedLinkedList.h"
using namespace std;
int main() {
orderedLinkedList <int> 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)
}
Explanation / Answer
// linkedListIterator.h
#ifndef LINKEDLISTITERATOR_H_
#define LINKEDLISTITERATOR_H_
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
class nodeType{
public:
Type info;
nodeType<Type> *next; //this is *link, changed it to next
//to not confuse myself
};
template <class Type>
class linkedListIterator{
public:
linkedListIterator();
//default constructor
linkedListIterator(nodeType<Type> *ptr);
//constructor w/ parameters
Type operator*();
//function to overload dereferencing operator *
linkedListIterator<Type> operator++();
//function to overload pre-increment operator ++
bool operator==(const linkedListIterator<Type> &right) const;
//overload the equality operator
bool operator!=(const linkedListIterator<Type> &right) const;
//overload the not equal operator
private:
nodeType<Type> *current;
//pointer to point to current node in list
};
template <class Type>
linkedListIterator<Type>::linkedListIterator(){
current = nullptr;
}
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->next;
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);
}
#endif /* LINKEDLISTITERATOR_H_ */
// end of linkedListIterator.h
//linkedListType.h
#ifndef LINKEDLISTTYPE_H_
#define LINKEDLISTTYPE_H_
#include<cassert>
#include "linkedListIterator.h"
template<class Type>
class linkedListType{
public:
const linkedListType<Type> &operator=
(const linkedListType<Type> &);
//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<Type> begin();
//function to return an iterator at the beginning of list
linkedListIterator<Type> end();
//function to return an iterator one element past the last element
linkedListType();
//default constructor
linkedListType(const linkedListType<Type> &otherList);
//copy constructor
~linkedListType();
//destructor
protected:
int count;
//variable to store number of elements in list
nodeType<Type> *first;//pointer for first node
nodeType<Type> *last;//pointer for last node
private:
void copyList(const linkedListType<Type> *otherList);
//function to make a copy of otherList
};
template<class Type>
bool linkedListType<Type>::isEmptyList(){
return (first == nullptr);
}
template<class Type>
linkedListType<Type>::linkedListType(){ //default constructor
first = nullptr;
last = nullptr;
count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList(){
nodeType<Type> *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<class Type>
void linkedListType<Type>::initializeList(){
destroyList(); //if list has nodes, delete them
}
template<class Type>
void linkedListType<Type>::print() const{
nodeType<Type> *current; //pointer to move through list
current = first;
while(current != nullptr){
cout << current->info << " ";
current = current->next;
}
}
template<class Type>
int linkedListType<Type>::length() const{
return count;
//end of length
}
template<class Type>
Type linkedListType<Type>::front() const{
assert(first != nullptr);
return first->info; //retrrn info of first node
}
template<class Type>
Type linkedListType<Type>::back() const{
assert(last != nullptr);
return last->info; //return info of last
}
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(nullptr);
return last;
}
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>* otherList){
nodeType<Type> *newNode;//pointer to create a node
nodeType<Type> *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<Type>; //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<Type>; //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<class Type>
linkedListType<Type>::~linkedListType(){
destroyList();
}//end destructor
template<class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList){
first = nullptr;
copyList(otherList);
}//end of copy constructor
//overload assignment operator
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList){
if(this != &otherList){ //to avoid self-copy
copyList(&otherList);
}
return *this;
}
#endif /* LINKEDLISTTYPE_H_ */
// end of linkedListType.h
//orderedLinkedList.h
#ifndef ORDEREDLINKEDLIST_H_
#define ORDEREDLINKEDLIST_H_
#include "linkedListType.h"
using namespace std;
template<class Type>
class orderedLinkedList: public linkedListType<Type>{
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<class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const{
bool found = false;
nodeType<Type> *current; //pointer to move through list
current = this->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<class Type>
void orderedLinkedList<Type>::insert(const Type& newItem){
nodeType<Type> *current; //pointer to move through list
nodeType<Type> *trailCurrent = nullptr; //pointer before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create new node
newNode->info = newItem; //store newItem in the node
newNode->next = nullptr; //set next to null
if(this->first == nullptr){ //first case: if list is empty
this->first = newNode;
this->last = newNode;
this->count++;
}
else{
current = this->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 == this->first){ //second case: if only one node available
if(current == this->first){
newNode->next = this->first;
this->first = newNode;
this->count++;
} else{ //third case: search is last node
trailCurrent->next = newNode;
newNode->next = current;
if(current == nullptr){
this->last = newNode;
}
this->count++;
}//end else
}//end if
}//end else
}//end insert
template<class Type>
void orderedLinkedList<Type>::insertFirst(const Type& newItem){
insert(newItem);
}//end insertFirst
template<class Type>
void orderedLinkedList<Type>::insertLast(const Type& newItem){
insert(newItem);
}//end insertLast
template<class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem){
nodeType<Type> *current; //to move through list
nodeType<Type> *trailCurrent;
bool found;
if(this->first == nullptr){ //first case: if list is empty
cout << "list is empty." << endl;
}
else{
current = this->first;
found = false;
while(current != nullptr && !found){ //search the list
if(current != nullptr && current->info >= 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(this->first == current){ //second case: if item is first node
this->first = this->first->next; //move first
if(this->first == nullptr){
this->last = nullptr;
}
delete current;
}
else{ //third case: if item is last node
trailCurrent->next = current->next;
if(current == this->last){
this->last = trailCurrent;
}
delete current;
}
this->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_ */
// end of orderedLinkedList.h
// main.cpp
#include <iostream>
#include <cstdlib>
#include "orderedLinkedList.h"
//#include "unorderedLinkedList.h"
using namespace std;
int main() {
orderedLinkedList <int> 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;
return 0;
}
// end of main.cpp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.