我试图输出我的链表的值,但我似乎无法让我的运算符<<工作。当我出<< previous->键;它出现为-1,这就是HEAD_OF_LIST。在程序中,我调用函数l [2] = LLSortedPosInt(2);将列表#2设置为包含一个整数2的元素。
这是类声明:
struct Node;
typedef Node* NodePtr;
// The key value HEAD_OF_LIST is used as a "sentinal" value
const int HEAD_OF_LIST = -1;
class LLSortedPosInt {
public:
// constructors
LLSortedPosInt();
LLSortedPosInt(int key);
LLSortedPosInt(int *keys, int n); //int *keys is an array of integers
LLSortedPosInt(const LLSortedPosInt &l);
// destructor
~LLSortedPosInt();
bool containsElement (int key) const;
bool isEmpty ( ) const;
LLSortedPosInt& operator= (const LLSortedPosInt &l);
bool operator==(const LLSortedPosInt &l) const;
bool operator!=(const LLSortedPosInt &l) const;
friend LLSortedPosInt operator+ (const LLSortedPosInt &l1,
const LLSortedPosInt &l2);
friend LLSortedPosInt operator- (const LLSortedPosInt &l1,
const LLSortedPosInt &l2);
friend ostream& operator<<(ostream &out,
const LLSortedPosInt &l);
private:
void insert (int key);
void remove (int key);
NodePtr head;
};
这是createNode函数:
static NodePtr createNode(int key, NodePtr p) {
// allocate a new Node for storing the given key value
NodePtr n = new Node;
// store the key value and the next pointer
n->key = key;
n->next = p;
// return the new Node to the caller
return n;
}
这是我的运算符<<的代码
ostream& operator<< (ostream &out, const LLSortedPosInt &l) {
// an empty list will be printed as <>
// a singleton list (a list having one key value k) will be
// printed as <k>
// a list having multiple keys such as 2, 5, 7 will be printed
// as <2, 5, 7>
// print the left angle bracket
out << "<";
NodePtr previous = l.head;
NodePtr current = l.head->next;
//TEST previous AND current
out << previous->key;
out << current->key << ">" << endl << "<";
// print the values of l
while (current != NULL) {
if (current->key >= 0) {
if (current->next == NULL) {
out << current->key;
break;
}
out << current << ", ";
previous = current;
current = current->next;
}
}
// print the right angle bracket
out << ">";
return out;
}
我还有一个函数,它接受一个下面的整数输入。我很确定这是正确写的,但我不确定。
LLSortedPosInt::LLSortedPosInt(int key) {
// create the sentinal Node at the head of the list
head = createNode(HEAD_OF_LIST, nullptr);
// add the single element key, as long as it is positive
if (key > 0) {
head->next = createNode(key, nullptr);
}
}
operator<<
的定义可能是:
std::ostream& operator<<(std::ostream& out, const LLSortedPosInt& l) {
out << "<";
NodePtr current = l.head;
// check that it actually points to a Node before accessing any fields
if(current) {
out << current->key;
// loop through the links and stream the keys
while((current = current->next) != nullptr)
out << "," << current->key;
}
out << ">";
return out;
}
构造函数LLSortedPosInt(int key)
不应该创建一个HEAD_OF_LIST
节点,因为它不用于任何东西。因此,您可以通过委派更改该构造函数以使用您已拥有的构造函数列表。委派还可以使用initializer_list
轻松添加构造函数:
LLSortedPosInt(const int *keys=nullptr, size_t n=0) :
head(nullptr)
{
if(keys && n) {
while(n--) {
createNode(*keys);
++keys;
}
}
}
// single key construction
LLSortedPosInt(int key) :
LLSortedPosInt(&key, 1) // delegate
{}
// construction using an initializer list
LLSortedPosInt(std::initializer_list<int> il) :
LLSortedPosInt(il.begin(), il.size()) // delegate
{}
static createNode
界面有点奇怪。该函数的用户不需要提供指向下一个节点的指针。这就是容器类的用途。它可以成为普通的成员函数,并直接在正确的位置创建节点:
NodePtr createNode(int key) {
NodePtr current = head;
NodePtr previous = nullptr;
while(current && (current->key < key)) {
previous = current;
current = current->next;
}
if(previous) {
// insert the node between previous and previous->next
NodePtr rv = new Node(key, previous->next);
return previous->next = rv;
} else {
// insert the node first in the list
NodePtr rv = new Node(key, head);
return head = rv;
}
}
鉴于实际的Node
看起来像这样:
struct Node {
Node* next;
int key;
Node(int k, Node* n) : next(n), key(k) {}
Node(const Node&) = delete;
Node(Node&&) = delete;
Node& operator=(const Node&) = delete;
Node& operator=(Node&&) = delete;
~Node() = default;
};
using NodePtr = Node*;