从“ orderedLinkedList <int>”到“ const int”的转换函数不存在

时间:2018-11-27 04:20:52

标签: c++

我在做家庭作业时遇到了问题,对于我的一生,我无法弄清楚我在哪里犯错。我们的任务是编写一个函数,该函数将二叉树的节点插入到有序链表中。以下是头文件和主要方法。从字面上看,现在任何帮助都将不胜感激,因为我现在正绕圈子旋转。

错误消息:不存在从“ orderedLinkedList”到“ const int”的合适转换函数 项目:Ch19_Ex9文件:Ch19_Ex9.cpp行:33:

Ch19_Ex9.cpp:

//Data
//68 43 10 56 77 82 61 82 33 56 72 66 99 88 12 6 7 21 -999

#include "binarySearchTree.h"
#include "orderedLinkedList.h"
#include "pch.h"
#include <iostream>


using namespace std;

int main()
{
binarySearchTree<int>  treeRoot;
orderedLinkedList<int> newList;

int num;

cout << "Enter numbers ending with -999" << endl;
cin >> num;

while (num != -999)
{
    treeRoot.insert(num);
    cin >> num;
}

cout << endl << "Tree nodes in inorder: ";
treeRoot.inorderTraversal();
cout << endl;
cout << "Tree Height: " << treeRoot.treeHeight()
    << endl;
treeRoot.createList(newList); //HERE IS THE LINE WITH ISSUE

cout << "newList: ";
newList.print();

cout << endl;
system("pause");

return 0;
}

binarySearchTree.h:

//Header File Binary Search Tree

#include <iostream>
#include "binaryTree.h"

using namespace std;

template <class Type>
class binarySearchTree : public binaryTreeType<Type>
{
public:
bool search(const Type& searchItem) const;
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in
// the binary search tree; otherwise,
// returns false.

void insert(const Type& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If there is no node in the binary search
// tree that has the same info as
// insertItem, a node with the info
// insertItem is created and inserted in the
// binary search tree.

void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the binary search tree
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary
// search tree.
// If the binary tree is empty or deleteItem
// is not in the binary tree, an appropriate
// message is printed.

void createList(const Type& addItem);

 private:
void deleteFromTree(nodeType<Type>* &p);
//Function to delete the node to which p points is
//deleted from the binary search tree.
//Postcondition: The node to which p points is deleted
// from the binary search tree.
};


template <class Type>
bool binarySearchTree<Type>::search
(const Type& searchItem) const
{
nodeType<Type> *current;
bool found = false;

if (root == nullptrptr)
    cout << "Cannot search an empty tree." << endl;
else
{
    current = root;

    while (current != nullptrptr && !found)
    {
        if (current->info == searchItem)
            found = true;
        else if (current->info > searchItem)
            current = current->lLink;
        else
            current = current->rLink;
    }//end while
}//end else

return found;
}//end search

template <class Type>
void binarySearchTree<Type>::insert
(const Type& insertItem)
{
nodeType<Type> *current; //pointer to traverse the tree
nodeType<Type> *trailCurrent; //pointer behind current
nodeType<Type> *newNode; //pointer to create the node

newNode = new nodeType<Type>;
newNode->info = insertItem;
newNode->lLink = nullptrptr;
newNode->rLink = nullptrptr;

if (root == nullptrptr)
    root = newNode;
else
{
    current = root;

    while (current != nullptrptr)
    {
        trailCurrent = current;

        if (current->info == insertItem)
        {
            cout << "The item to be inserted is already ";
            cout << "in the tree -- duplicates are not "
                << "allowed." << endl;
            return;
        }
        else if (current->info > insertItem)
            current = current->lLink;
        else
            current = current->rLink;
    }//end while

    if (trailCurrent->info > insertItem)
        trailCurrent->lLink = newNode;
    else
        trailCurrent->rLink = newNode;
}
}//end insert

template <class Type>
void binarySearchTree<Type>::deleteNode
(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the tree
nodeType<Type> *trailCurrent; //pointer behind current
bool found = false;

if (root == nullptr)
    cout << "Cannot delete from an empty tree."
    << endl;
else
{
    current = root;
    trailCurrent = root;

    while (current != nullptr && !found)
    {
        if (current->info == deleteItem)
            found = true;
        else
        {
            trailCurrent = current;

            if (current->info > deleteItem)
                current = current->lLink;
            else
                current = current->rLink;
        }
    }//end while

    if (current == nullptr)
        cout << "The item to be deleted is not in the tree."
        << endl;
    else if (found)
    {
        if (current == root)
            deleteFromTree(root);
        else if (trailCurrent->info > deleteItem)
            deleteFromTree(trailCurrent->lLink);
        else
            deleteFromTree(trailCurrent->rLink);
    }
    else
        cout << "The item to be deleted is not in the tree."
        << endl;
}
} //end deleteNode

template <class Type>
void binarySearchTree<Type>::deleteFromTree
(nodeType<Type>* &p)
{
nodeType<Type> *current; //pointer to traverse the tree
nodeType<Type> *trailCurrent; //pointer behind current
nodeType<Type> *temp; //pointer to delete the node

if (p == nullptr)
    cout << "Error: The node to be deleted does not exist."
    << endl;
else if (p->lLink == nullptr && p->rLink == nullptr)
{
    temp = p;
    p = nullptr;
    delete temp;
}
else if (p->lLink == nullptr)
{
    temp = p;
    p = temp->rLink;
    delete temp;
}
else if (p->rLink == nullptr)
{
    temp = p;
    p = temp->lLink;
    delete temp;
}
else
{
    current = p->lLink;
    trailCurrent = nullptr;

    while (current->rLink != nullptr)
    {
        trailCurrent = current;
        current = current->rLink;
    }//end while

    p->info = current->info;

    if (trailCurrent == nullptr) //current did not move;
                                 //current == p->lLink; adjust p
        p->lLink = current->lLink;
    else
        trailCurrent->rLink = current->lLink;

    delete current;
}//end else
} //end deleteFromTree


template <class Type>
void binarySearchTree<Type>::createList(const Type& addItem) 
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
nodeType<Type> *newNode;

newNode = new nodeType <Type>;
newNode->info = createItem;
newNode->lLink = nullptr;
newNode->rLink = nullptr;

if (root == nullptr)
{
    root = newNode;
}

}

编辑:添加orderedLinkedList.h:

 #include "linkedList.h"

 using namespace std;

 template <class Type>
 class orderedLinkedList : public linkedListType<Type>
 {
public:
bool search(const Type& searchItem) const;

void insert(const Type& newItem);

void insertFirst(const Type& newItem);

void insertLast(const Type& deleteItem);

void deleteNode(const Type& deleteItem);
 };

 template <class Type>
 bool orderedLinkedList<Type>::search(const Type& searchItem) const
 {
nodeType<Type> *current; //pointer to traverse the list
bool found = false;

current = first; //set current to point to the first 
                 //node in the list

while (current != nullptr && !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;

};

template <class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{

};

template <class Type>
void orderedLinkedList<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 = first;      //insert newNode before first
first = newNode;            //make first point to the
                            //actual first node
count++;                    //increment count

if (last == nullptr)   //if the list was empty, newNode is also 
                    //the last node in the list
    last = newNode;

};

template <class Type>
void orderedLinkedList<Type>::insertLast(const Type& deleteItem)
{
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 = nullptr;     //set the link field of newNode
                          //to nullptr

if (first == nullptr)  //if the list is empty, newNode is 
                    //both the first and last node
{
    first = newNode;
    last = newNode;
    count++;        //increment count
}
else    //the list is not empty, insert newNode after last
{
    last->link = newNode; //insert newNode after last
    last = newNode; //make last point to the actual 
                    //last node in the list
    count++;        //increment count
}

};

template <class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;

if (first == nullptr)
{
    cout << "Cannot delete from an empty list."
    << endl;
}
else
{
    if (first->info == deleteItem)
    {
        current = first;
        first = first->link;
        count--;

        if (first == nullptr)
        {
            last = nullptr;

            delete current;
        }
        else
        {
            found = false;
            trailCurrent = first;

            current = first->link;

            while (current != nullptr && !found)
            {
                if (current->info != deleteItem)
                {
                    trailCurrent = current;
                    current = current->link;
                }
                else
                {
                    found = true;
                }
            }

            if (found)
            {
                trailCurrent->link = current->link;
                count--;

                if (last == current)
                {
                    last = trailCurrent;
                }
                delete current;
            }
            else
                cout << "The item to be deleted is not in "
                << "the list." << endl;
        }
    }
}

}

0 个答案:

没有答案