gpt4 book ai didi

c++类访问冲突

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:44:49 24 4
gpt4 key购买 nike

<分区>

您好,我的链表头文件中出现访问冲突读取错误。该项目采用二叉树并将其转换为有序链表。二叉树头:

 #ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H

#include "dsexceptions.h"
#include "LinkedList.h"
#include <iostream>
using namespace std;

template <typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree( ) :root( NULL )
{ }

BinarySearchTree(const BinarySearchTree & rhs) : root(NULL)
{ *this = rhs; }

~BinarySearchTree( )
{ makeEmpty( ); }

const Comparable & findMin( ) const
{
if (isEmpty( ))
throw UnderflowException( );
return findMin(root)->element;
}

const Comparable & findMax( ) const
{
if(isEmpty( ))
throw UnderflowException( );
return findMax( root )->element;
}

bool contains(const Comparable & x) const
{ return contains(x, root); }

bool isEmpty( ) const
{ return root == NULL; }

void printTree(ostream & out = cout)
{
if (isEmpty( ))
out << "Empty tree" << endl;
else
printTree(root, out);
}

void makeEmpty( )
{ makeEmpty(root); }

void insert(const Comparable & x)
{ insert(x, root); }

void remove(const Comparable & x)
{ remove(x, root); }

const BinarySearchTree & operator=(const BinarySearchTree & rhs)
{
if (this != &rhs)
{
makeEmpty( );
root = clone(rhs.root);
}
return *this;
}

void toList(linkedlist l)
{ toList(l, root); }

private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;

BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt)
: element(theElement), left(lt), right(rt) { }
};

BinaryNode *root;
void toList(linkedlist l, BinaryNode *&t)
{
if(t==NULL)
{ return; }
toList(l,t->left);
l.add(t->element);
toList(l,t->right);
}

void printTree(BinaryNode *&t, ostream & out = cout)
{
if(t==NULL)
{ return; }
printTree(t->left,out);
cout << t->element << endl;
printTree(t->right,out);
}

void insert(const Comparable & x, BinaryNode * & t)
{
if (t == NULL)
t = new BinaryNode(x, NULL, NULL);
else if (x < t->element)
insert(x, t->left);
else if (t->element < x)
insert(x, t->right);
else; // Duplicate; do nothing
}

void remove(const Comparable & x, BinaryNode * & t)
{
if (t == NULL)
return; // Item not found; do nothing
if (x < t->element)
remove(x, t->left);
else if (t->element < x)
remove(x, t->right);
else if (t->left != NULL && t->right != NULL) // Two children
{
t->element = findMin(t->right)->element;
remove(t->element, t->right);
}
else
{
BinaryNode *oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}

BinaryNode * findMin(BinaryNode *t) const
{
if (t == NULL)
return NULL;
if (t->left == NULL)
return t;
return findMin(t->left);
}

BinaryNode * findMax(BinaryNode *t) const
{
if (t != NULL)
while (t->right != NULL)
t = t->right;
return t;
}

bool contains(const Comparable & x, BinaryNode *t) const
{
if (t == NULL)
return false;
else if (x < t->element)
return contains(x, t->left);
else if (t->element < x)
return contains(x, t->right);
else
return true; // Match
}

void makeEmpty(BinaryNode * & t)
{
if (t != NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}

BinaryNode * clone(BinaryNode *t) const
{
if (t == NULL)
return NULL;
else
return new BinaryNode(t->element, clone(t->left), clone(t->right));
}
};

#endif

链表头:

#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <iostream>
using namespace std;

class linkedlist
{
private:

struct lNode{
int data;
lNode *next;
};

struct lNode *head;

public:

linkedlist()
{ struct lNode *head = new lNode;
head->next = NULL;
}

void add(int n) {
lNode *newlNode = new lNode;
newlNode->data = n;
newlNode->next = NULL;

lNode *cur = head;
while(true) {
if(cur->next == NULL)
{
cur->next = newlNode;
break;
}
cur = cur->next;
}
}

void display() {
lNode *list = head;

while(true)
{
if (list->next == NULL)
{
cout << list->data << endl;
break;
}
cout << list->data << endl;
list = list->next;
}
cout << "done" << endl;
}

};

#endif

主cpp文件:

#include <iostream>
#include "BinarySearchTree.h"
#include "LinkedList.h"
using namespace std;

int main( )
{
BinarySearchTree<int> t;
linkedlist l;
int i;

cout << "inserting nodes into tree" << endl;
t.insert(50);
t.insert(60);
t.insert(30);
t.insert(20);
t.insert(40);
t.insert(70);
t.insert(55);
t.insert(65);
t.insert(25);
t.insert(35);
t.insert(85);
t.insert(100);
t.insert(15);
t.insert(45);
t.insert(95);
t.insert(105);
t.insert(10);
t.insert(75);
t.insert(110);
t.insert(12);
t.insert(92);
t.insert(32);
t.insert(82);
t.insert(22);
t.insert(32);

t.printTree( );
t.toList(l);

cout << "Finished processing" << endl;
l.display();

return 0;
}

错误位置在链表头文件这里:if(cur->next == NULL)。我不明白这怎么可能是访问错误,因为所有内容都包含在该类中。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com