我想以排序的方式在排序的链表中插入数据。我能够插入数据,但它没有排序。任何人都能帮我解决我做错了什么吗? 这是我的功能:
typename SortedList<T>::iterator SortedList<T>::insert(const T& data) {
if (data < front_->data_) {
Node* n = new Node(data, front_, nullptr);
//if empty
if (front_ == nullptr) {
//make back pt to node
back_ = n;
}
else {
//make front's previous point to node
front_->prev_ = n;
}
//make front point at node
front_ = n;
return SortedList<T>::iterator(n);
}
else {
Node* nn = new Node(data, nullptr, back_);
//if empty
if (front_ == nullptr) {
//make front_ pt to node
front_ = nn;
}
else {
//make back's next point to node
back_->next_ = nn;
}
//make back point at node
back_ = nn;
return SortedList<T>::iterator(nn);
}
这是我的班级
class SortedList{
struct Node {
T data_;
Node* next_;
Node* prev_;
Node(const T& data = T{}, Node* nx = nullptr, Node* pr = nullptr) {
data_ = data;
next_ = nx;
prev_ = pr;
}
};
Node* front_;
Node* back_;
int sizelist;
}
答案 0 :(得分:0)
在访问front_
之前,您没有检查nullptr
front_->data
。
但更重要的是,您并没有尝试将数据插入任何类型的排序位置。您只能插入列表的正面或背面。要在中间插入,您必须扫描列表,寻找要插入的正确位置。
尝试更像这样的东西:
class SortedList{
struct Node {
T data_;
Node* prev_;
Node* next_;
Node(const T& data = T{}, Node* pr = nullptr, Node* nx = nullptr)
: data_(data), prev_(pr), next_(nx)
{
}
};
Node* front_;
Node* back_;
int size_;
...
iterator insert(const T& data);
};
typename SortedList<T>::iterator SortedList<T>::insert(const T& data)
{
Node *nn = front_;
while ((nn) && (data >= nn->data_)) {
nn = nn->next_;
}
Node *n = new Node(data);
if (nn)
{
n->prev_ = nn->prev_;
n->next_ = nn;
if (nn->prev_) nn->prev_->next = n;
nn->prev = n;
}
else
{
n->prev_ = back_;
if (back_) back_->next_ = n;
back_ = n;
}
if (front_ == nn) front_ = n;
++size_;
return iterator(n);
}
答案 1 :(得分:0)
我没有看到任何搜索列表的代码来查找插入元素的位置。您尚未指定预期的元素数量;线性插入到排序列表中的性能是平均O(n / 2),这导致得出结论:你将总体上具有O(n ^ 2/2)[n平方超过2]这是不能容忍的如果n是,例如10,000,并且如果n <1则无关紧要。 10. 20年前我遇到了这个问题,n> 200,在8088.有一个大约为O(log2(n))的解决方案,但不知道n我不想花时间解释它,如果它不相关。
答案 2 :(得分:0)
你的代码指出你的思维不清晰。
您必须处理以下情况:
另外,你对“prev”和“next”的使用对我来说有点奇怪。当我想到一个双重链表时,我认为它是:
front -> next -> next -> next -> nullptr
nullptr <- prev <- prev <- prev <- back
您好像正在使用:
front -> prev -> prev -> prev -> nullptr
nullptr <- next <- next <- next <- back
我的回答与我认为您使用的内容相对应,第二个版本。如果这不正确,则需要在几个地方更新“next”和“prev”的用法。
<案例1在功能开始时添加以下内容:
if ( front_ == nullptr )
{
Node* n = new Node(data, nullptr, nullptr);
front_ = n;
back_ = n;
return SortedList<T>::iterator(n);
}
<案例2
你需要:
if ( data < front_->data_ )
{
Node* n = new Node(data, front_, nullptr);
front_->prev_ = n;
return SortedList<T>::iterator(n);
}
<案例3
你需要:
if ( data > back_->data_ )
{
Node* n = new Node(data, nullptr, back_);
back_->next_ = n;
return SortedList<T>::iterator(n);
}
<案例4
你需要:
// Find the place where to insert the new Node.
Node* iter = front_;
for ( ; iter != nullptr && data < iter->data_; iter = iter->prev_ );
// Insert the new Node.
Node* prev = iter->prev_
Node* n = new Node(data, iter, prev);
prev->next_ = n;
iter->prev_ = n;
return SortedList<T>::iterator(n);
typename SortedList<T>::iterator SortedList<T>::insert(const T& data)
{
// Case 1
if ( front_ == nullptr )
{
Node* n = new Node(data, nullptr, nullptr);
front_ = n;
back_ = n;
return SortedList<T>::iterator(n);
}
// Case 2
if ( data < front_->data_ )
{
Node* n = new Node(data, front_, nullptr);
front_->prev_ = n;
return SortedList<T>::iterator(n);
}
// Case 3
if ( data > back_->data_ )
{
Node* n = new Node(data, nullptr, back_);
back_->next_ = n;
return SortedList<T>::iterator(n);
}
// Case 4
// Find the place where to insert the new Node.
Node* iter = front_;
for ( ; iter != nullptr && data < iter->data_; iter = iter->prev_ );
// Insert the new Node.
Node* prev = iter->prev_
Node* n = new Node(data, iter, prev);
prev->next_ = n;
iter->prev_ = n;
return SortedList<T>::iterator(n);
}